Page 1 of 1

Confused about how to effectively iterate through an array

Posted: Wed Jul 19, 2006 8:12 am
by hydroxide
I'm totally at a loss of how to implement what I'm looking for. I'm trying to create markers for each value in the arrays for client locations and I don't know how to effectively do this. I was thinking to automatically create however many vars were needed for each address and that many functions in the load() function, but this doesn't seem like a good idea any more. I'm really not sure how I can get it to create markers for the 80 something elements in the array. Any help would be greatly appreciated.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <?php

		//sets user filter var for everything
		global $ufilter;
		global $st_addr;
		global $city;
		global $state;
		global $zip;
		global $array_string;
		global $forInfo;
		global $x;
		global $varString;
		gloabl $addString;

		//mysql connect
		$link = mysql_connect('', '', '');
		if (!$link) {
   		die('Could not connect: ' . mysql_error());
		}
		$db_selected = mysql_select_db('tiki', $link);
		if (!$db_selected) {
   			die ('Can\'t use tiki : ' . mysql_error());
		}

		//init sales_code temporarily
		$sales_code_query = "
		SELECT
		els_contact.sales_code		
		FROM els_contact		
		WHERE els_contact.username = 'jbutler'
		LIMIT 1";
	
		//returns the result set 
		$sales_code_ret = mysql_query($sales_code_query);
		if (!$sales_code_ret) {
   			die('Invalid query: ' . mysql_error());
		}
		//parses the array and assigns a result to a new variable 
		while ($newArray = mysql_fetch_assoc($sales_code_ret)) {
			$ufilter = $newArray['sales_code'];
   		}

		$client_address_query = "
		SELECT
		els_client_data.address1,
		els_client_data.city,
		els_client_data.state,
		els_client_data.zip
		FROM els_client_data
		WHERE els_client_data.sales_code = '047'";
		
		
		//returns the result set 
		$client_addr_ret = mysql_query($client_address_query);
		if (!$client_addr_ret) {
   			die('Invalid query: ' . mysql_error());
		}
		//parses the array and assigns a result to a new variable 
		while ($address_array = mysql_fetch_assoc($client_addr_ret)) {
			$st_addr[] = $address_array['address1'];
			$zip[] = $address_array['zip'];
			$state[] = $address_array['state'];
			$city[] = $address_array['city'];
			$varString = "var address1 =<?php print '$st_addr[$x], $city[$x], $state[$x], $zip[$x]';?>";
			$addString = 'address'.$x;
			$x = $x+1;
   		}
   		//more array manipulation
   		$x = 0;
   		$forInfo = count($st_addr);
   		
	?>
	
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Map Points</title>
    <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAApRphKLi-UMuIcsRzk_R2lhRi_j0U6kJrkFvY4-OX2XYmEAa76BQ_x0ZBELZ5jM4CBA8l_aIEatdzlQ" type="text/javascript"></script>
   <script type="text/javascript">
   //<![CDATA[

    var map = null;
    var geocoder = null;
	'<?php echo $varString;?>;'
    
    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        geocoder = new GClientGeocoder();
        markAddress(<?php echo $addString;?>);
      }
    }

    function markAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 7);
              var marker = new GMarker(point);
              map.addOverlay(marker);
            }
          }
        );
      }
    }
    //]]>
    </script>
  </head>

  <body onload="load()" onunload="GUnload()">
    <div id="map" style="width: 500px; height: 300px"></div>
  </body>
</html>

Posted: Wed Jul 19, 2006 11:22 am
by hydroxide
Fixed with this, in case anyone cares.

Code: Select all

<?php
	// sets user filter var for everything
	global $ufilter;
	global $st_addr;
	global $city;
	global $state;
	global $zip;
	global $array_string;
	global $forInfo;
	global $x;
	global $varString;

	$googlekey = "ABQIAAAApRphKLi-UMuIcsRzk_R2lhQ5zKf0btzt0igWTsX3gDpiKzjrthQd-7Wu8Rc539eM0oGgm9Fi5CnE1g";
	
	//mysql connect
	$link = mysql_connect('localhost', 'root', '');
	if (!$link) {
  		die('Could not connect: ' . mysql_error());
	}

	$db_selected = mysql_select_db('tiki', $link);

	if (!$db_selected) {
		die ('Can\'t use tiki : ' . mysql_error());
	}

	//init sales_code temporarily
	$sales_code_query = "
	SELECT
	els_contact.sales_code		
	FROM els_contact		
	WHERE els_contact.username = 'jbutler'
	LIMIT 1";
	
	//returns the result set 
	$sales_code_ret = mysql_query($sales_code_query);
	if (!$sales_code_ret) {
		die('Invalid query: ' . mysql_error());
	}

	//parses the array and assigns a result to a new variable 
	while ($newArray = mysql_fetch_assoc($sales_code_ret)) {
		$ufilter = $newArray['sales_code'];
	}

	$client_address_query = "
	SELECT
	els_client_data.address1,
	els_client_data.city,
	els_client_data.state,
	els_client_data.zip
	FROM els_client_data
	WHERE els_client_data.sales_code = '047'";
		
	//returns the result set 
	$client_addr_ret = mysql_query($client_address_query);
	if (!$client_addr_ret) {
		die('Invalid query: ' . mysql_error());
	}
		
	$results = Array();
	//parses the array and assigns a result to a new variable
	while ($address_array = mysql_fetch_assoc($client_addr_ret)) {
		$results[$i++] = $address_array;
	}

	//more array manipulation
	$x = 0;
	$forInfo = count($st_addr);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&v=2&key=<? echo $googlekey; ?>" type="text/javascript"></script>
    <script type="text/javascript">

	//<![CDATA[

    var map = null;
    var geocoder = null;
	<?php
		$key = 0;
		foreach($results as $key => $contact) {
			$results[$key][javavarname] = "address$key";
			$varString = "var address$key = \"$contact[address1], $contact[city], $contact[state], $contact[zip]\";\n";
			echo $varString;
			$key++;
		}
	?>

    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        geocoder = new GClientGeocoder();
<?
		foreach($results as $contact) {
			echo "markAddress(" . $contact[javavarname] . ");\n";
		}
?>
      }
    }

    function markAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
            // alert(address + " not found");
            } else {
              map.setCenter(point, 7);
              var marker = new GMarker(point);
              map.addOverlay(marker);
            }
          }
        );
      }
    }
    //]]>
    </script>
  </head>
  <body onload="load()" onunload="GUnload()">
    <div id="map" style="width: 500px; height: 300px"></div>
  </body>
</html>

Posted: Wed Jul 19, 2006 11:47 am
by Christopher
That's a ton of code for volunteers to look through to find a problem. Can you edit your posts to just show the loop that is not working?

Posted: Wed Jul 19, 2006 11:49 am
by hydroxide
Oh, no, the second post is the working code. The first bit was the broken one, and I wanted to show the community how I fixed my problem.

Posted: Wed Jul 19, 2006 11:55 am
by Luke
arborint wrote:That's a ton of code for volunteers to look through to find a problem. Can you edit your posts to just show the loop that is not working?
that's what I thought too... then I realized he already figured it out :oops:

Posted: Wed Jul 19, 2006 12:03 pm
by hydroxide
it wasn't so much that one piece of my code wasn't working, I was just looking for an idea as to how to implement the loop to achieve my goal. Perhaps I should have been more clear... sorry :oops: