Tags: function

Sort by: Date / Title /

  1. 10 years ago by spirit
    Great circle distance
    Great circle distance
    Find the distance in kilometres between two points on the surface of the earth. This is just the sort of problem stored functions were made for. For a first order approximation, ignore deviations of the earth's surface from the perfectly spherical. Then the distance in radians is given by a number of trigonometric formulas. ACOS and COS behave reasonably:
    
                 COS(lat1-lat2)*(1+COS(lon1-lon2)) - COS(lat1+lat2)*(1-COS(lon1-lon2))
    rads = ACOS( --------------------------------------------------------------------- )
                                                  2
    
    We need to convert degrees latitude and longitude to radians, and we need to know the length in km of one radian on the earth's surface, which is 6378.388. The function:
    
    set log_bin_trust_function_creators=TRUE;
    
    DROP FUNCTION IF EXISTS GeoDistKM;
    DELIMITER |
    CREATE FUNCTION GeoDistKM( lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT ) RETURNS float
    BEGIN
      DECLARE pi, q1, q2, q3 FLOAT;
      DECLARE rads FLOAT DEFAULT 0;
      SET pi = PI();
      SET lat1 = lat1 * pi / 180;
      SET lon1 = lon1 * pi / 180;
      SET lat2 = lat2 * pi / 180;
      SET lon2 = lon2 * pi / 180;
      SET q1 = COS(lon1-lon2);
      SET q2 = COS(lat1-lat2);
      SET q3 = COS(lat1+lat2);
      SET rads = ACOS( 0.5*((1.0+q1)*q2 - (1.0-q1)*q3) ); 
      RETURN 6378.388 * rads;
    END;
    |
    DELIMITER ;
    
    -- toronto to montreal (505km):
    select geodistkm(43.6667,-79.4167,45.5000,-73.5833);
    +----------------------------------------------+
    | geodistkm(43.6667,-79.4167,45.5000,-73.5833) |
    +----------------------------------------------+
    |                           505.38836669921875 |
    +----------------------------------------------+
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/1555"></script>
  2. 11 years ago by spirit
    1. var myfunc = function(optional) {
    2.   if (typeof optional == "undefined") {
    3.     optional = "default value";
    4.   }
    5.   alert(optional);
    6. }
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/691"></script>
  3. 11 years ago by xillon
    1. function selectTable($nomtable, $where = NULL, $order = NULL)
    2.          {
    3.             $sql = "SELECT * FROM $nomtable";
    4.             if ( $where !== NULL ) $sql .= " WHERE $where";
    5.             if ($order !== NULL)$sql.=" ORDER BY $order ";
    6.            
    7.             $query=mysql_query($sql);
    8.            
    9.             $ret = array();
    10.             while ( $donnee = mysql_fetch_assoc($query))
    11.             {
    12.                $ret[] = $donnee;
    13.             }
    14.             return $ret;
    15.          }
    16.          
    17.          function sqlSelect($sqlDonnee, $nomCol, $valCol)
    18.          {
    19.             for($i = 0 ; $i<count($sqlDonnee) ; $i++)
    20.             {
    21.                $selectDonee["name"][$i] = $sqlDonnee[$i][$nomCol];
    22.                $selectDonee["value"][$i] = $sqlDonnee[$i][$valCol];
    23.             }
    24.          return $selectDonee;
    25.          }
    26.          
    27.          function htmlSelect($nom, $options, $selected = NULL, $css = NULL)
    28.          {
    29.             $select  = "<select name=\"".htmlentities($nom)."\"";
    30.             if ( $css !== NULL )
    31.                $select .= " $css>";
    32.             else
    33.                $select .= ">";
    34.  
    35.             for($i = 0 ; $i<count($options["name"]) ; $i++)
    36.             {
    37.                if ( $selected !== NULL && $selected == $options["value"][$i] )
    38.                   $select .= "<option value=\"".$options["value"][$i]."\" selected=\"selected\">".$options["name"][$i]."</option>\n";
    39.                else
    40.                   $select .= "<option value=\"".$options["value"][$i]."\">".$options["name"][$i]."</option>\n";
    41.             }
    42.             $select .= "</select>\n";
    43.  
    44.             return $select;
    45.          }
    46.  
    47. /*** Exemple utilisation ***/
    48. /* <option selected> par defaut pour le pays ayant l'id 72 */
    49.          $paysSelect = isset($_POST["pays"]) ? ($_POST["pays"]) : ("72");
    50.          
    51.          $selectPays=selectTable("pays");
    52.          $pays=sqlSelect($selectPays,"nom","id");         
    53.          echo htmlSelect("id", $pays, $paysSelect); // on construit notre <select>
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/218"></script>
  4. sponsorised links

First / Previous / Next / Last / Page 1 of 1 (3 posteets)