Skor Posted January 16, 2008 Share Posted January 16, 2008 I haven't done many for loops, but this one's making me a little nutty. What I'm trying to do is increment the field names, starting at 1, up to the number of rows that are returned from the query. I've gotten to return either two rows (which is correct), the the $i variable stays at 1 for both entries. Or, I've gotten four rows to return (which is wrong) and the $i variable, goes 1-2 1-2. Not what I want either. I suspect(or hope) that I've made a simple error and just need a more experienced set of eyes. Thanks in advance. $result = mysql_query($query) or die('Query not successfully processed: ' . mysql_error()); $num_rows = mysql_num_rows($result); //echo "Number of rows: $num_rows"; // get each row while($row = mysql_fetch_assoc($result)) { $cat = $row["cat_code"]; $pid = $row["pid"]; $name = $row["name"]; $price = $row["price"]; $units = $row["units"]; $qty = $row["qty"]; $itemname = $name.'('.$cat.$pid.')'; echo "<tr valign=\"top\">"; for($i=1; $i <= $num_rows; $i++) --is this where the problem is? { echo "<td>$name<input name=\"product$i\" type=\"hidden\" value=\"$itemname\"></td>"; echo "<td> $$price<input name=\"price$i\" type=\"hidden\" value=\"$price\"></td>"; echo "<td><select name=\"qty$i\"> <option value=\"0\" selected=\"selected\">Select quantity</option> <option value=\"1\">1</option> <option value=\"2\">2</option> <option value=\"3\">3</option> </select>"; echo "<input name=\"scode$i\" type=\"hidden\" value=\"$pid\"> <input name=\"units$i\" type=\"hidden\" value=\"$units\"></td></tr>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/86256-solved-for-loop-making-me-loopy/ Share on other sites More sharing options...
trq Posted January 16, 2008 Share Posted January 16, 2008 You need to understand that mysql_fetch_assoc returns one array, containing one row, made up of all the fields in your table. What your doing doesn't make sense. Quote Link to comment https://forums.phpfreaks.com/topic/86256-solved-for-loop-making-me-loopy/#findComment-440607 Share on other sites More sharing options...
Skor Posted January 16, 2008 Author Share Posted January 16, 2008 Is there a better way to retrieve the results of my query as well as iterate over the form? Quote Link to comment https://forums.phpfreaks.com/topic/86256-solved-for-loop-making-me-loopy/#findComment-440609 Share on other sites More sharing options...
Xeoncross Posted January 16, 2008 Share Posted January 16, 2008 You need to learn some Mysql/PHP as you are way off on your logic. <?php <?php $result = mysql_query($query) or die('Query not successfully processed: ' . mysql_error()); //$num_rows = mysql_num_rows($result); //echo "Number of rows: $num_rows"; // get each row while($row = mysql_fetch_assoc($result)) { $cat = $row["cat_code"]; $pid = $row["pid"]; $name = $row["name"]; $price = $row["price"]; $units = $row["units"]; $qty = $row["qty"]; $itemname = $name.'('.$cat.$pid.')'; echo "<tr valign=\"top\">"; echo "<td>$name<input name=\"product$i\" type=\"hidden\" value=\"$itemname\"></td>"; echo "<td> $$price<input name=\"price$i\" type=\"hidden\" value=\"$price\"></td>"; echo "<td><select name=\"qty$i\"> <option value=\"0\" selected=\"selected\">Select quantity</option> <option value=\"1\">1</option> <option value=\"2\">2</option> <option value=\"3\">3</option> </select>"; echo "<input name=\"scode$i\" type=\"hidden\" value=\"$pid\"> <input name=\"units$i\" type=\"hidden\" value=\"$units\"></td></tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86256-solved-for-loop-making-me-loopy/#findComment-440610 Share on other sites More sharing options...
Skor Posted January 16, 2008 Author Share Posted January 16, 2008 This doesn't account for the $i variable which I need to increment for each row returned. Quote Link to comment https://forums.phpfreaks.com/topic/86256-solved-for-loop-making-me-loopy/#findComment-440617 Share on other sites More sharing options...
thomashw Posted January 16, 2008 Share Posted January 16, 2008 The 'while' loop will keep looping until there are no more rows left, and then it will move on. Quote Link to comment https://forums.phpfreaks.com/topic/86256-solved-for-loop-making-me-loopy/#findComment-440621 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.