Jump to content

Getting Array values and using outside While loop


kampbell411

Recommended Posts

I've been going crazy on how to figure this out. Kinda new to php. 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. Im trying to load addresses into a jquery plugin that plots multiple points. I wanted to get the addresses from DB then store in variable with the addresses and use that variable in my javascript.

 

$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 the array

 

Try something like this:

<?php
$sql = "SELECT zip, state, city FROM $tbl_name WHERE state = 'value'";
$result = mysql_query($sql);
$addresses = array();
while($row = mysql_fetch_assoc($result))
{
     $addresses[] = $row['city'] . ', ' . $row['state'] . ' ' . $row['zip'];
}
echo implode("<br>\n", $addresses);  // print what's in the array
//
//  or
//
echo '<pre>' . print_r($addresses,true) . '</pre>';  // just dump them
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.