Getting Array Results outside a While Loop

Discussion in 'PHP' started by kampbell411, May 22, 2010.

  1. #1
    I've been going crazy on how to figure this out. I'm trying to get array results outside of a while loop. Im basically trying to put my results into an array and then set those array values as a variable to use outside of while loop. Below is my code:

    
    
    $sql = "SELECT zip, state, city FROM $tbl_name WHERE state = 'value';
    	$result = mysql_query($sql);
    	while($row = mysql_fetch_array($result))
    {
    $zip = $row->zip;	
    $state = $row->state;
    $city = $row->city;	
    
    $newzip= $row['zip'];
    
    $addresses = array(
        array("address"=>$newzip),
           );
    }
    echo $addresses;  <---would like to do something like that with all the values put in $addresses array
    
    PHP:

     
    kampbell411, May 22, 2010 IP
  2. Fervid

    Fervid Well-Known Member

    Messages:
    161
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    120
    #2
    You'd need a foreach or a while loop to do that. Why are you trying to avoid it?
     
    Fervid, May 22, 2010 IP
  3. kampbell411

    kampbell411 Peon

    Messages:
    82
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Im trying to load multiple points on a google map and I'm using a jquery plugin to do that. So when i get my array of addresses, I have to load them in javascript. Thats why I wanted a variable with all my addresses. So I can just put one simple variable to define the addresses in the map. So basically I wanted to uses the $addresses variable outside the loop because I can't put that jquery inside that loop of course. Do you have any suggestions? May be another way to load results like that?
     
    kampbell411, May 22, 2010 IP
  4. Fervid

    Fervid Well-Known Member

    Messages:
    161
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Ahh, I see what you're after. Without knowing exactly what the javascript looks like, I imagine there's a javascript array that will hold the points to be displayed. Since PHP is server side and javascript is client side PHP has no way to directly access that array. You will probably just need to echo each point into whatever format your javascript array requires.
     
    Fervid, May 22, 2010 IP
  5. Trikun3

    Trikun3 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <?php
    
    // $link = mysql_connect($host, $user, $pass);
    // mysql_select_db($db_name, $link)
    
    // Set the SQL command variables.
    $tbl_name = 'places'
    $state = 'florida';
    
    // Make the SQL query and run it.
    $sql = sprintf("SELECT `zip`, `state`, `city` FROM `%s` WHERE `state` = '%s';", $tbl_name, $state);
    $result = mysql_query($sql, $link);
    
    // Make the container for addresses.
    $addresses = array();
    
    // While row = mysql object
    while($row = mysql_fetch_object($result))
    {
    	// $zip = $row->zip;
    	// $state = $row->state;
    	// $city = $row->city;
    	
    	// Create a new entry in the addresses array.
    	$addresses[] = array(
    		'zip' => $row->zip,
    		'state' => $row->state,
    		'city' => $row->city
    	);
    }
    
    // Show the contents.
    print_r($addresses);
    Code (markup):
    $addresses will contain the zip, state, and city of each address found. You may access them like:

    // If you want the first result's city.
    $addresses['0']['city'];
    Code (markup):
     
    Trikun3, May 22, 2010 IP
  6. kampbell411

    kampbell411 Peon

    Messages:
    82
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ah Perfect. Thanks for the help Fervid and Trikun3.
     
    kampbell411, May 22, 2010 IP