...

View Full Version : loops



robojob
11-24-2007, 07:43 PM
Hi,
i want to use a while loop to add info from the db into variables named item1, item2 etc.

i have the below query which i know wont work because it doesnt like the variable names $item$i... how can i get it to automatically create and name the variables for each $i?



$query1 = "SELECT * from quotes where quote_id = $quote_id";
$result1 = @mysql_query ($query1); //run the query
while ($row1 = mysql_fetch_array
($result1, MYSQL_ASSOC)) {
$i = 0;
$item$i = $row1['item$i'];
$i = $i + 1;
}

CFMaBiSmAd
11-24-2007, 07:52 PM
Make your life easier and use an array for $item. Using sequentially named variables will require you to keep track of how many of them there are. With an array, you can use count() to find out how many or you can simply use a foreach() loop it iterate over all elements of the array.

robojob
11-24-2007, 08:01 PM
i already know taht there are only 20...

timgolding
11-24-2007, 08:35 PM
You need to use an array or assign the values for each one. here is the array answere



<?PHP
$query1 = "SELECT * from quotes where quote_id = $quote_id";
$item=array();
$result1 = @mysql_query ($query1); //run the query
while ($row1 = mysql_fetch_array
($result1, MYSQL_ASSOC)) {
$item[sizeof($item)]= $row1;
}
?>


This will infact return a multidimensional array

Inigoesdr
11-24-2007, 09:57 PM
No it won't. Why would you use sizeof() anyway, instead of simply appending the value to the array and letting PHP assign the key?


$arr = array();
$arr[] = $row1;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum