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 asked
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,

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','','');
   exit("Connection Failed: " . $conn);

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



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

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

echo "</table>";

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


$strGMarker = strGMarker;

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

$markers[0] = new GMarker($strGMarker,

Watch Question

Hello fdcol63x,

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

$mcount = 0;
$rec = array();

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



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");


$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);



Open in new window

Hope this will Help you.

Thank You.

Most Valuable Expert 2011
Top Expert 2016

I'm new to PHP, and trying to...  Please read this brief article.

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:

There is an article on Google maps here:

And an article on proximity calculations here:

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



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




Thank you for your information, too!  Very helpful!