Update of /cvsroot/phpweather/phpweather
In directory usw-pr-cvs1:/tmp/cvs-serv4546

Modified Files:
        base_object.php data_retrieval.php db_dba.php db_mysql.php 
        index.php 
Log Message:
There's now a fine station-selector on the page. So far, it only works
with the MySQL database (and perhaps with PostgreSQL, if anybody adds
support).


Index: base_object.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/base_object.php,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -3 -r1.6 -r1.7
--- base_object.php     2001/05/19 16:16:24     1.6
+++ base_object.php     2001/05/21 10:43:46     1.7
@@ -49,22 +49,20 @@
    * @param  $msg  string  The error-message.
    */
   function error($msg) {
-    if ('DEBUG') {
-      echo "<p><b>Error</b>: $msg</p>";
-    }
+    echo "<p><b>Error</b>: $msg</p>\n";
   }
   
   /**
    * Prints a notice.
    *
    * You should supply it with a string as the argument, and it will 
-   * then print the string, prefixed with the word 'Notice' in bold.
+   * then print the string, prefixed with the word 'Debug' in bold.
    *
    * @param  $msg  string  The notice.
    */
-  function notice($msg) {
+  function debug($msg) {
     if (defined('DEBUG')) {
-      echo "<p><b>Notice</b>: $msg</p>";
+      echo "<p><b>Debug</b>: $msg</p>\n";
     }
   }  
 

Index: data_retrieval.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/data_retrieval.php,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -3 -r1.8 -r1.9
--- data_retrieval.php  2001/05/20 13:07:16     1.8
+++ data_retrieval.php  2001/05/21 10:43:46     1.9
@@ -67,6 +67,9 @@
    * @access   public
    */
   function set_icao($new_icao) {
+    /* We start by adding slashes, since $new_icao might come directly
+       from the user. */
+    $new_icao = addslashes($new_icao);
     if ($new_icao != $this->get_icao()) {
       $this->properties['icao'] = strtoupper($new_icao);
       $this->location = '';
@@ -87,10 +90,10 @@
   function get_metar() {
     if (empty($this->metar)) {
       /* The METAR is not set - we try to load it */
-      $this->notice('The METAR is not set, I\'ll try to find the METAR in the 
database.');
+      $this->debug('The METAR is not set, I\'ll try to find the METAR in the 
+database.');
       return $this->get_metar_from_db();
     } else {
-      $this->notice('The METAR was set - I\'ll just use that.');
+      $this->debug('The METAR was set - I\'ll just use that.');
       return $this->metar;
     }
   }
@@ -112,13 +115,16 @@
    * @access   public
    */
   function get_metar_from_db() {
+    if ($this->db->is_connected) {
+      $this->debug('get_metar_from_db(): Connected to the database, 
+$this->db->is_connected == true, before connect()');
+    }
 
     if (!$this->db->connect()) {
       return false;
     }
     
     if ($data = $this->db->get_metar($this->get_icao())) { /* found station */
-      $this->notice('Found the METAR in the database');
+      $this->debug('get_metar_from_db(): Found the METAR in the database');
       list($metar, $timestamp) = $data;
       /* We set the METAR right away, and then count on
        * get_metar_from_web() to set it to something else, if
@@ -130,16 +136,16 @@
         * still fresh. Either way - we return the METAR we found in
         * the database.
         */
-       $this->notice('Using previously cached METAR for <code>' . 
$this->get_location() . '</code>. The METAR expires in ' . ($timestamp + 3600 - 
time()) . ' seconds.');
+       $this->debug('get_metar_from_db(): Using previously cached METAR for <code>' . 
+$this->get_location() . '</code>. The METAR expires in ' . ($timestamp + 3600 - 
+time()) . ' seconds.');
        return $metar;
       } else {
        /* The METAR is too old, so we fetch a new */
-       $this->notice('The METAR for <code>' . $this->get_location() . '</code> was ' 
. (time() - 3600 - $timestamp) . ' seconds too old.');
+       $this->debug('get_metar_from_db(): The METAR for <code>' . 
+$this->get_location() . '</code> was ' . (time() - 3600 - $timestamp) . ' seconds too 
+old.');
        return $this->get_metar_from_web(false);
       }
     } else {
       /* We need to get a new METAR from the web. */
-      $this->notice('New station <code>' . $this->get_location() . '</code> - 
fetching a new METAR.');
+      $this->debug('get_metar_from_db(): New station <code>' . $this->get_location() 
+. '</code> - fetching a new METAR.');
       return $this->get_metar_from_web(true);
     }
   }
@@ -218,10 +224,10 @@
     
     /* We then cache the METAR in our database */
     if ($new_station) {
-      $this->notice('Inserting new METAR for <code>' . $this->get_location() . 
'</code>');
+      $this->debug('get_metar_from_web(): Inserting new METAR for <code>' . 
+$this->get_location() . '</code>');
       $this->db->insert_metar($icao, $metar, $timestamp);
     } else {
-      $this->notice('Updating METAR for <code>' . $this->get_location() . '</code>');
+      $this->debug('get_metar_from_web(): Updating METAR for <code>' . 
+$this->get_location() . '</code>');
       $this->db->update_metar($icao, $metar, $timestamp);
     }
     /* We update and return the METAR */
@@ -231,9 +237,13 @@
 
   function get_location() {
     if (!empty($this->location)) {
+      $this->debug('get_location(): Using old location: ' . $this->location);
       return $this->location;
-    } else {
+    } elseif ($this->db->connect()) {
+      $this->debug('get_location(): Looking for location in the database');
       return $this->location = $this->db->lookup_icao($this->get_icao());
+    } else {
+      return false;
     }
   }
 

Index: db_dba.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/db_dba.php,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -3 -r1.7 -r1.8
--- db_dba.php  2001/05/20 13:07:16     1.7
+++ db_dba.php  2001/05/21 10:43:46     1.8
@@ -135,7 +135,7 @@
   function get_metar($station) {
     if (dba_exists($station, $this->link_id)) {
       $row = dba_fetch($station, $this->link_id);
-      $this->notice("Returning this row from the DBA database:<br><code>$row</code>");
+      $this->debug("Returning this row from the DBA database:<br><code>$row</code>");
       return explode(':', $row);
     } else {
       return false;

Index: db_mysql.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/db_mysql.php,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -3 -r1.6 -r1.7
--- db_mysql.php        2001/05/20 13:07:16     1.6
+++ db_mysql.php        2001/05/21 10:43:46     1.7
@@ -113,6 +113,7 @@
    * @access  public
    */
   function fetch_row() {
+    $this->debug('fetch_row(): MySQL said: ' . mysql_error());
     return mysql_fetch_row($this->result_id);
   }
 
@@ -145,8 +146,8 @@
    * @access  public
    * @see update_metar()
    */
-  function insert_metar($station, $metar, $timestamp) {
-    $this->query("INSERT INTO metars SET station = '$station', metar = '$metar', 
timestamp = FROM_UNIXTIME($timestamp)");
+  function insert_metar($icao, $metar, $timestamp) {
+    $this->query("INSERT INTO metars SET icao = '$icao', metar = '$metar', timestamp 
+= FROM_UNIXTIME($timestamp)");
   }
 
 
@@ -159,8 +160,8 @@
    * @access  public
    * @see insert_metar()
    */
-  function update_metar($station, $metar, $timestamp) {
-    $this->query("UPDATE metars SET metar = '$metar', timestamp = 
FROM_UNIXTIME($timestamp) WHERE station = '$station'");
+  function update_metar($icao, $metar, $timestamp) {
+    $this->query("UPDATE metars SET metar = '$metar', timestamp = 
+FROM_UNIXTIME($timestamp) WHERE icao = '$icao'");
   }
 
   /**
@@ -170,8 +171,8 @@
    * @return  string  The raw METAR as an array from the database.
    * @access  public
    */
-  function get_metar($station) {
-    $this->query("SELECT metar, UNIX_TIMESTAMP(timestamp) FROM metars WHERE station = 
'$station'");
+  function get_metar($icao) {
+    $this->query("SELECT metar, UNIX_TIMESTAMP(timestamp) FROM metars WHERE icao = 
+'$icao'");
     return $this->fetch_row();
   }
 
@@ -180,11 +181,11 @@
       /* First we make a table for the METARs */
       $this->query('DROP TABLE IF EXISTS ' . $this->properties['db_metars']);
       $this->query('CREATE TABLE ' . $this->properties['db_metars'] . '(
+   icao varchar(4) NOT NULL,
    metar varchar(255) NOT NULL,
    timestamp timestamp(14),
-   station varchar(4) NOT NULL,
-   PRIMARY KEY (station),
-   UNIQUE station (station)
+   PRIMARY KEY (icao),
+   UNIQUE station (icao)
 )');
 
       /* Then we make a table for the stations. */
@@ -215,7 +216,7 @@
    */
   function lookup_icao($icao) {
     $this->query('SELECT name, country FROM ' . $this->properties['db_stations'] .
-                "WHERE icao = '$icao'");
+                " WHERE icao = '$icao'");
     if ($this->num_rows()) {
       $row = $this->fetch_row();
       return "$row[0], $row[1]";
@@ -224,12 +225,37 @@
     }
   }
 
+
   function insert_station($icao, $name, $country) {
-    $sql = 'INSERT INTO ' . $this->properties['db_stations'] .
-      " VALUES('$icao', '$name', '$country')";
-    return $this->query($sql);
+    return $this->query('INSERT INTO ' . $this->properties['db_stations'] .
+                       " VALUES('$icao', '$name', '$country')");
+  }
+
+
+  function get_countries() {
+    if (!$this->connect()) {
+      return false;
+    }
+
+    $this->query('SELECT DISTINCT country FROM ' . $this->properties['db_stations'] . 
+' ORDER BY country');
+    while($row = $this->fetch_row()) {
+      $rows[] = $row[0];
+    }
+    return $rows;
+  }
+
+  function get_icaos($country) {
+    if (!$this->connect()) {
+      return false;
+    }
+
+    $country = addslashes($country);
+    $this->query('SELECT icao, name FROM ' . $this->properties['db_stations'] . " 
+WHERE country = '$country' ORDER BY name");
+    while(list($icao, $name) = $this->fetch_row()) {
+      $rows[$icao] = $name;
+    }
+    return $rows;
   }
-  
 }
 
 ?>

Index: index.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/index.php,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -3 -r1.10 -r1.11
--- index.php   2001/05/20 13:07:16     1.10
+++ index.php   2001/05/21 10:43:46     1.11
@@ -19,58 +19,54 @@
 
 //define('DEBUG', true);
 
+ $obj = new phpweather(array());
 
-$properties = array('station'    => '_AA_'); // A non-existing station.
-                   
-/* Checkpoint, we store the time */
-$time[] = array(microtime(), 'files-inclusion');
-
-/* Here we make an object for the weather */
-$obj = new phpweather($properties);
-
-echo "<pre>\n";
-print_r($obj->properties);
-echo "</pre>\n";
-
-/* Checkpoint, we store the time */
-$time[] = array(microtime(), 'object-creation');
-
-$obj->get_metar();
-
-/* Checkpoint, we store the time */
-$time[] = array(microtime(), 'METAR retrieval');
-
-$obj->decode_metar();
-
-
-/* Checkpoint, we store the time */
-$time[] = array(microtime(), 'decoding');
-
-$obj->print_pretty();
-
-/* Checkpoint, we store the time */
-$time[] = array(microtime(), 'pretty-print finished');
-
-$stations = array('FNLU', 'LOWW', 'YMML', 'TNCA',
-                 'TXKF', 'CYOD', 'FJDG', 'MWCR',
-                 'MROC', 'HECA', 'ZSAM', 'YPCC',
-                 'LFPG', 'EDDV', 'LDZA', 'LGSR',
-                 'HKNW', 'OKBK', 'VHHH', 'EPWA'
-                 );
-
-
-$num_stations = count($stations);
-
-for ($i = 0; $i < $num_stations; $i++) {
-  echo "<h1>$stations[$i]</h1>\n";
-  $obj->set_icao($stations[$i]);
+if (empty($action)) {
+  /* No action - we display a form from which the user can select a
+     country. */
+
+  echo '
+<p>Select a country:</p>
+<form action="index.php" method="post">
+<input type="hidden" name="action" value="show_stations">
+<select name="country" size="10">
+';
+   
+ $countries =  $obj->db->get_countries();
+ $num_countries = count($countries);
+ for ($i = 0; $i < $num_countries; $i++) {
+   echo "<option name=\"$countries[$i]\">$countries[$i]</option>\n";
+ }
+ echo '</select>
+<p><input type="submit"></p>
+</form>
+';
+
+} elseif ($action == 'show_stations') {
+  /* A country has just been selected - we make a form with all
+     stations in that country. */
+  echo '<form action="index.php" method="post">
+<input type="hidden" name="action" value="show_weather">
+<p>Select a station from ' . $country . ':</p>
+<select name="icao" size="10">
+';
+
+  $icaos = $obj->db->get_icaos($country);
+  while (list($icao, $name) = each($icaos)) {
+    echo "<option value=\"$icao\">$name</option>\n";
+  }
+  echo '</select>
+<p><input type="submit"></p>
+</form>
+';
+
+} elseif ($action == 'show_weather') {
+  /* A station has just been selected - we print the weather. */
+  $obj->set_icao($icao);
   $obj->print_pretty();
-  echo '<blockquote><code>' . $obj->metar . "</code></blockquote>";
 }
-
-/* Checkpoint, we store the time */
-$time[] = array(microtime(), '20 stations displayed');
 
+$time[] = array(microtime(), 'end');
 
 $max = count($time) - 1;
 for ($i = 0; $i < $max; $i++) {


_______________________________________________
PHPWeather-checkins mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/phpweather-checkins

Reply via email to