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

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.