Jump to content

[SOLVED] For loop making me loopy


Skor

Recommended Posts

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>";
}
}

Link to comment
https://forums.phpfreaks.com/topic/86256-solved-for-loop-making-me-loopy/
Share on other sites

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>";
}
?>

Archived

This topic is now archived and is closed to further replies.

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