PHPNS Posted June 11, 2007 Share Posted June 11, 2007 Hi, I seem to struggle with loops in PHP. Can someone please look at the following code and help me... PLEASE. $query = "SELECT sh_us_overnight FROM tbl_shipping_us"; $result = mysql_query($query); for ($i = 0; $i < $result; $i++) { while ($row = mysql_fetch_array($result)) { $rate = $row[$i]; } } echo "$rate[0]"; echo "$rate[1]"; Hopefully you geniuses can tell what I'm trying to do here. If not, I'm simply trying to loop through multiple rows returned so that I can assign each row a to a rate variable. As in, rate1 equals something (row1), rate2 equals something (row2), etc. I'm getting a weird result here, where each rate array is giving me the next digit in the final row selected. Obviously, not what I want. Link to comment https://forums.phpfreaks.com/topic/55154-solved-php-loop/ Share on other sites More sharing options...
simcoweb Posted June 11, 2007 Share Posted June 11, 2007 This part: for ($i = 0; $i < $result; $i++) { doesn't make any sense since it's checking if $i is < (less than) $results. The $results contains the data from your query, not an integer. Link to comment https://forums.phpfreaks.com/topic/55154-solved-php-loop/#findComment-272644 Share on other sites More sharing options...
B34ST Posted June 11, 2007 Share Posted June 11, 2007 <?php $query = "SELECT sh_us_overnight FROM tbl_shipping_us"; $result = mysql_query($query); $i = 0; while ($row = mysql_fetch_array($result)) { $rate[$i] = $row[$i]; $i++; } foreach $rate as $value { echo $value; } ?> Link to comment https://forums.phpfreaks.com/topic/55154-solved-php-loop/#findComment-272646 Share on other sites More sharing options...
PHPNS Posted June 11, 2007 Author Share Posted June 11, 2007 This code is still not returning an array. Still only the last row received by the initial query. Plus I think you forgot the brackets (in the for loop). Any other suggestions Link to comment https://forums.phpfreaks.com/topic/55154-solved-php-loop/#findComment-272674 Share on other sites More sharing options...
B34ST Posted June 11, 2007 Share Posted June 11, 2007 try: <?php $query = "SELECT sh_us_overnight FROM tbl_shipping_us"; $result = mysql_query($query); $i = 0; while ($row = mysql_fetch_array($result)) { $rate[$i] = $row[$i]; $i++; } foreach ($rate as $r => $value ) { echo $rate[$r]; } ?> Link to comment https://forums.phpfreaks.com/topic/55154-solved-php-loop/#findComment-272676 Share on other sites More sharing options...
PHPNS Posted June 11, 2007 Author Share Posted June 11, 2007 Still no go. Am I missing something. I think (in my limited knowledge) that this last change is really no different than the previous. I do appreciate the help though! Any other takers? Link to comment https://forums.phpfreaks.com/topic/55154-solved-php-loop/#findComment-272680 Share on other sites More sharing options...
PHPNS Posted June 11, 2007 Author Share Posted June 11, 2007 Hey B34ST, This worked! $query = "SELECT sh_us_overnight FROM tbl_shipping_us"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { $rate[] .= $row[0]; } Ah sweet victory!! Link to comment https://forums.phpfreaks.com/topic/55154-solved-php-loop/#findComment-272686 Share on other sites More sharing options...
B34ST Posted June 11, 2007 Share Posted June 11, 2007 dont forget to mark this topic as solved Link to comment https://forums.phpfreaks.com/topic/55154-solved-php-loop/#findComment-272691 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.