Jump to content

Problem with incrementing variables in a loop...


jaxdevil

Recommended Posts

for ($i = 1; $i < 21; $i++) { // loop for the number of $item_number
$model_number = "model_number_".$i;
$model_number = $$model_number;
   if ($model_number != '') {
$q = "SELECT `weight` FROM `products_ship` WHERE `model_number`='$model_number'";
$result = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
while($row = mysql_fetch_array($result)){
$rez = "$w".$i;
$$rez = $row[weight];
}
}
}

 

I am trying to make it so for all the posts (some could be left blank so it will ony do those that are posted) that are posted, it will make their weight variables be $w1, $w2, $w3, etc. so I can total all of the togethers to get the total weight of all of them. I can't seem to find the way there at the end:

 

$rez = "$w".$i;
$$rez = $row[weight];

 

To make the part on the left of the = increment with each loop ( i.e. $w1, $w2, $w3, etc.) How can I do this?

Link to comment
Share on other sites

Not really sure what you're doing before the query, but if I read your intention correctly, why can't you do something like this?

 

$q = "SELECT sum(`weight`) as totalweight FROM `products_ship` WHERE `model_number`='$model_number'";
$result = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
$row = mysql_fetch_array($result);
echo $row['totalweight'];

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.