phpgurl Posted August 24, 2007 Share Posted August 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/66463-displaying-only-output-from-one-db-record-need-more/ Share on other sites More sharing options...
trq Posted August 24, 2007 Share Posted August 24, 2007 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');"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66463-displaying-only-output-from-one-db-record-need-more/#findComment-332778 Share on other sites More sharing options...
phpgurl Posted August 24, 2007 Author Share Posted August 24, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/66463-displaying-only-output-from-one-db-record-need-more/#findComment-332790 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.