We help IT Professionals succeed at work.

Trying to populate PHP code with variable data from DB query / GMap, Google Maps with Multiple markers

fdcol63x
fdcol63x asked
on
Hello all.  I'm new to PHP, and trying to populate some PHP code with variable data from a database query.  Also trying to use GMap 2.3 to get a Google map with multiple markers for each company returned by this query.

Basically, my problem is that I can return the query results that I want, but I don't know how to use "iCount" or "row" identifiers in PHP so that I can populate this particular section of the larger code below, with the appropriate Company, Address, and Website data for the GMap markers.

$markers[0] = new GMarker($strGMarker,
                          $Company,
                          $Website);

Any help would be greatly appreciated!

=========== CODE ==============

<?php require_once('_gmap/gmap.php');

$asplUserID = $_GET["ID"];
$State = $_GET["State"];
$sqlQry = 'Company Query';
$MemType = $_GET["MemType"];
$CID = $_GET["CID"];

$iCount = 1;
$rowCount = 1;

$conn = odbc_connect('myDB','','');
   if(!$conn)
   exit("Connection Failed: " . $conn);

$sql = "SELECT * FROM $sqlQry WHERE MailingAddressState = '$State' AND ActiveMember = '$MemType' ORDER BY Company ASC";
        $rs = odbc_exec($conn, $sql);
        if(!$rs)
        exit("Error in SQL");
  echo "<table><tr>";

while(odbc_fetch_row($rs)){

$Company=odbc_result($rs,"Company");
$Website=odbc_result($rs,"Website");
$MailingAddress=odbc_result($rs,"MailingAddress");
$MailingAddressCity=odbc_result($rs,"MailingAddressCity");
$MailingAddressState=odbc_result($rs,"MailingAddressState");
$MailingAddressPostalCode=odbc_result($rs,"MailingAddressPostalCode");

echo "<td>$iCount. $Company</td></tr>";

$iCount = $iCount + 1;
$rowCount = $iCount - 1;
 }

odbc_close($conn);
echo "</table>";

define("strGMarker","$MailingAddress $MailingAddressCity, $MailingAddressState $MailingAddressPostalCode");

define("strWebsite","http://$Website");

$strGMarker = strGMarker;

$gmap = new GMap();
$gmap->map_type = 'map';
$gmap->zoom = 15;

$markers[0] = new GMarker($strGMarker,
                          $Company,
                          $Website);      

$gmap->showMap($markers);
?>
Comment
Watch Question

Hello fdcol63x,

It seems that you need to put your markers array in loop as following

 
$mcount = 0;
$rec = array();
while(odbc_fetch_row($rs)){ 

$rec[$mcount]['Company'] = odbc_result($rs,"Company"); 
$rec[$mcount]['Website'] = odbc_result($rs,"Website");
$rec[$mcount]['MailingAddress']=odbc_result($rs,"MailingAddress");
$rec[$mcount]['MailingAddressCity']=odbc_result($rs,"MailingAddressCity");
$rec[$mcount]['MailingAddressState']=odbc_result($rs,"MailingAddressState");
$rec[$mcount]['MailingAddressPostalCode']=odbc_result($rs,"MailingAddressPostalCode");

 $mcount++;                        

}

Open in new window


After above, you can retrieve all as following

 
$gmap = new GMap();
$gmap->map_type = 'map';
$gmap->zoom = 15; 

for($i = 0; $i < count($rec); $i++)
{
  define("strGMarker","$MailingAddress $MailingAddressCity, $MailingAddressState $MailingAddressPostalCode");

define("strWebsite","http://$Website");

$strGMarker = $rec[$i]['MailingAddress'] . ' ' . $rec[$i]['MailingAddressCity'] . ' ' . $rec[$i]['MailingAddressState'] . ' ' . $rec[$i]['MailingAddressPostalCode'];

$strWebsite = "http://" . $rec[$i]['Website'];

$markers[$i] = new GMarker($strGMarker, $rec[$i]['Company'], $strWebsite);


}

$gmap->showMap($markers);

Open in new window


Hope this will Help you.

Thank You.

Amar.
Most Valuable Expert 2011
Top Expert 2016

Commented:
I'm new to PHP, and trying to...  Please read this brief article.
http://norvig.com/21-days.html

Copying PHP code that someone else wrote is a little like looking at an apple pie and trying to guess the recipe.  You know you need apples, but what else?  In the instant case, I would not assign such a task to a new PHP programmer.  There are just too many complicated moving parts.  A good place to start in PHP is with simple, familiar patterns.  This book will show you some of the basics:
http://www.sitepoint.com/books/phpmysql4/

There is an article on Google maps here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3350-Using-the-Google-Maps-API-in-PHP.html

And an article on proximity calculations here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_4276-What-is-near-me-Proximity-calculations-using-PHP-and-MySQL.html

There is no design pattern in PHP that would define() a constant from existing variables in order to use the values in another variable.  Just use the variables.  Also, you might want to learn a little about filtering external input.  The values in $_GET came from somewhere else.  How do you know these string values are safe to use in a query?  You don't!  So examine these strings and be sure they contain acceptable data.  The general mantra is "Accept only known good values."  And while we cannot begin to teach you PHP in a question and answer forum, we can tell you a few useful things to help you get started.

Use error_reporting(E_ALL) in all of your scripts.
All the PHP functions are documented in the online man pages.  Example: http://php.net/manual/en/function.var-dump.php
Make error messages meaningful (not "SQL Error" - find the error code and print it).
Data visualization is your friend, so print out lots of data as you develop your program.
Coding standards matter - choose one and use it (Google "Zend Coding Standards").
Read the online man pages, especially the "Return Values" and user-contributed notes.
Understand every line of code before you use it.  Don't copy without looking up the functions.

It takes some time to become proficient with PHP, but if you follow these guidelines, you will shorten that time line a great deal, I promise.  Good luck with your project, ~Ray

Author

Commented:
Amar,

That worked!  It's exactly what I was trying to do.  Thanks!

============
Frank

Author

Commented:
Ray,

Thank you for your information, too!  Very helpful!