Jump to content

[SOLVED] PHP loop


PHPNS

Recommended Posts

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.