Forum Moderators: coopster

Message Too Old, No Replies

turning results into an array

         

mylungsarempty

11:38 pm on Jan 17, 2004 (gmt 0)

10+ Year Member



my code currently looks as follows:

$find_zips="SELECT * FROM zipcodes WHERE longitude > '$north' AND longitude < '$south' AND latitude > '$west' AND latitude < '$east'";
$find_all_zips = mysql_query($find_zips);
if (!$find_all_zips) {
die('<p>error retrieving zipcodes<br>' . 'Error: ' . mysql_error() . '</p>');
}

$all_zips = mysql_fetch_array($find_all_zips);

echo $all_zips['0'];
echo $all_zips['1'];
echo $all_zips['2'];
echo $all_zips['3'];
echo $all_zips['4'];

that is my closest guess as to how to convert the results of $find_all_zips into an array. However, this is evidently how the proper way of going about things. How would i convert those results into an array?

figment88

11:49 pm on Jan 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mysql_fetch_array only gets one record, you need to iterate over the result set.

//Get number of records
$N=mysql_num_rows($find_all_zips);

//iterate N times
for($i=0; $i<$N; $i++) {
$row=mysql_fetch_assoc($find_all_zips);
$all_zips[]=$row['zip'];
}

Now $all_zips will be an array with all of the zipcodes (I assuming here your field name is zip).

BitBanger

1:56 am on Jan 18, 2004 (gmt 0)

10+ Year Member



This will print out all returned rows from your mysql_query() call.

while ($all_zips = mysql_fetch_array($find_all_zips)) {
foreach ($all_zips as $col_name => $col_data) {
echo $col_name . ' = ' . $col_data . '<br />';
}
echo '<br />';
}

You probably don't want all of the columns output, so instead of the 'foreach' loop you could replace it with.


echo $all_zips['desired_SQL_field_name'] . '<br />';

mylungsarempty

12:47 am on Jan 19, 2004 (gmt 0)

10+ Year Member



Well i got that last part working, but now what confuses me is the following code only prints one user per zipcode. I cannot seem to figure out how to print every user for each zipcode. Any ideas?

include("database.php");

mysql_select_db($db, $con);

foreach ($all_zips as $col_name => $col_data) {

$col_data=sprintf("%05d",$col_data);

$find_users="SELECT * FROM musicians WHERE '$col_data'=zip";
$all_users=mysql_query($find_users);

if (!$all_users) {
die('<p>error retrieving latitude<br>' . 'Error: ' . mysql_error() . '</p>');
}

while ($result = mysql_fetch_array($all_users)) {
$newid = $result['ID'];
$newname = $result['username'];
$newlisting = $result['listing'];
$newgear = $result['gear'];
}

echo '<U><B><A HREF="userpage.php"><p align="justify"><font color="red">' . $newname . '</font><font color="black"></B></U><br /> ' . $newgear . '</A></font><hr><br />';

unset($newid);
unset($newname);
unset($newlisting);
unset($newgear);

}

figment88

12:56 am on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the stuff you echo also needs to be in the loop. otherwise it will only get executed one time.

try moving your close brace down.

mylungsarempty

1:06 am on Jan 19, 2004 (gmt 0)

10+ Year Member



wow thanks. heh, how simple that was.