jaxdevil Posted October 17, 2008 Share Posted October 17, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/128876-problem-with-incrementing-variables-in-a-loop/ Share on other sites More sharing options...
rhodesa Posted October 17, 2008 Share Posted October 17, 2008 not really sure what's going on here, but it's usually MUCH easier to use arrays then variable variables EDIT: aka - make a variable $w, that is an array, and say $w[$i] = $row['weight']; Quote Link to comment https://forums.phpfreaks.com/topic/128876-problem-with-incrementing-variables-in-a-loop/#findComment-668135 Share on other sites More sharing options...
sasa Posted October 17, 2008 Share Posted October 17, 2008 change $rez = "$w".$i; to $rez = '$w'.$i; Quote Link to comment https://forums.phpfreaks.com/topic/128876-problem-with-incrementing-variables-in-a-loop/#findComment-668173 Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 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']; Quote Link to comment https://forums.phpfreaks.com/topic/128876-problem-with-incrementing-variables-in-a-loop/#findComment-668184 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.