Jump to content

displaying only output from one db record - need more


phpgurl

Recommended Posts

I'm trying to display several addresses in my google map, but succeeded in only showing the last one in the database.  So something is wrong in the way I'm configuring this, I think.

 

I think I need to go through each record some how and echo each one, but I don't know how.  Right now I have this:

 

<?php $connection = mysql_connect("localhost", "user", "pw");

if (!$connection){

die("Could not connect to the database: <br/>" . mysql_error());

}

 

 

$db_select = mysql_select_db("database");

if (!$db_select) {

 

die ("could not select the database: <br />". mysql_error());

}

    $query = "SELECT * FROM location";

    $result = mysql_query( $query );

if (!$result){

die ("Could not query the database: <br />". mysql_error());

}

 

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

    $street=$row["street"];

    $city=$row["city"];

    $stateabbr=$row["stateabbr"];

    $zipcode=$row["zipcode"];

}

?>

 

  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>

    <title>Google Maps API Example - Geocoding API</title>

    <script src="http://maps.google.com/maps?file=api&v=2.x&key=xxxxxx" type="text/javascript"></script>

    <script type="text/javascript">

    //<![CDATA[

 

    var map = null;

    var geocoder = null;

 

    function load() {

      if (GBrowserIsCompatible()) {

        map = new GMap2(document.getElementById("map"));

        map.setCenter(new GLatLng(37.4419, -122.1419), 13);

        geocoder = new GClientGeocoder();

 

      <?php

        echo "showAddress('$street, $city, $stateabbr $zipcode');";

        ?>

      }

    }

 

 

So this is apparently going through my database and displaying the last one only.  How can I show all the records?

Link to comment
Share on other sites

Remove this...

 

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
       $street=$row["street"];
       $city=$row["city"];
       $stateabbr=$row["stateabbr"];
       $zipcode=$row["zipcode"];
}

 

from where it currently is and replace this....

 

<?php
        echo "showAddress('$street, $city, $stateabbr $zipcode');";
?>

 

with....

 

<?php
  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $street=$row["street"];
    $city=$row["city"];
    $stateabbr=$row["stateabbr"];
    $zipcode=$row["zipcode"];
    echo "showAddress('$street, $city, $stateabbr $zipcode');";
  }
?>

Link to comment
Share on other sites

Hi and *thank you* - I made the changes - so now at the top is only the connection to the database.  Now, I'm only showing one - but a different record!! (??)  hmmmm...  I'm starting to wonder if it's not possible to do what i'm asking - since my database has addresses throughout the usa - but it seems the map wants to center in one area.  Maybe I need a broader area displayed - or a zoom feature?  (or maybe it is a php issue) - 

 

 

<head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>

    <title>Google Maps API Example - Geocoding API</title>

    <script src="http://maps.google.com/maps?file=api&v=2.x&key=xxxxx" type="text/javascript"></script>

    <script type="text/javascript">

    //<![CDATA[

 

    var map = null;

    var geocoder = null;

 

    function load() {

      if (GBrowserIsCompatible()) {

        map = new GMap2(document.getElementById("map"));

        map.setCenter(new GLatLng(37.4419, -122.1419), 13);

        geocoder = new GClientGeocoder();

 

      <?php

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

    $street=$row["street"];

    $city=$row["city"];

    $stateabbr=$row["stateabbr"];

    $zipcode=$row["zipcode"];

    echo "showAddress('$street, $city, $stateabbr $zipcode');";

  }

?>

 

      }

    }

 

    function showAddress(address) {

      if (geocoder) {

        geocoder.getLatLng(

          address,

          function(point) {

            if (!point) {

              alert(address + " not found");

            } else {

              map.setCenter(point, 13);

              var marker = new GMarker(point);

              map.addOverlay(marker);

              marker.openInfoWindowHtml(address);

            }

          }

        );

      }

    }

    //]]>

    </script>

  </head>

 

  <body onload="load()" onunload="GUnload()">

 

      <div id="map" style="width: 500px; height: 300px"></div>

 

  <?php mysql_close($connection);

?>

  </body>

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.