NickG21 Posted July 10, 2010 Share Posted July 10, 2010 Hey everyone, my problem is that Im trying to get variables to increment inside of a nested while loop that is displaying formatted MySQL results. Getting stuck on something this simple makes me feel a little ridiculous but I need to. Here is the code I have right now, but the for() loop isn't incrementing the value of $i by 1 after each ind. product is displayed, just remains as a value of 1 for every iteration. to shorten this up and make it easier i just included the query strings and the results im trying to pass, nothing else really matters. $CartSQL = "SELECT * from tblCart Where CartSessionID = '" . $_SESSION['SessID'] . "'"; $rsCartSQL = mysql_query($CartSQL); for($i = 1; $i < mysql_num_rows($rsCartSQL); $i++){ while($PrIDs = mysql_fetch_array($rsCartSQL)){ $sql = "SELECT * from tblProduct where PrID='" . $PrIDs['CartPrID'] . "'"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)){ echo "<input type='hidden' name='L_NAME$i' value='$row[PrDispName]'>"; echo "<input type='hidden' name='L_AMT$i' value='$row[PrPrice]'>"; echo "<input type='hidden' name='L_QTY$i' value='$PrIDs[CartPrQty]'>"; echo "<input type='hidden' name='L_DESC$i' value='$row[PrDesc]'>"; echo '<tr><td width="65%"><a href="products.php?id=' . $row['PrID'] . '"> ' . $row['PrDispName'] . '</a></td><td><img src="' . $row['PrImage'] . '" alt=' . $row['PrName'] . '" height="50px" width="50px"></td>'; echo '<td>$' . $row['PrPrice'] . '</td>'; echo '<td>Qty:' . $PrIDs['CartPrQty'] . '</td>'; echo '<tr><td colspan="4"><hr/></td></tr>'; echo '</tr>'; $total = $row['PrPrice'] * $PrIDs['CartPrQty']; } $_SESSION['total'] += $total; } } as you can see im trying to increment for the hidden values, which eventually will be Name-Value pairs sent to a secure server for transaction, right now with this code they are being sent as LNAME1 LNAME1 LDESC1 LDESC1 no incrementing. thanks for any help guys, it's appreciated. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 10, 2010 Share Posted July 10, 2010 First of all, are you sure that mysql_num_rows($rsCartSQL) is returning a positive integer higher than the loop starting point? Secondly, why are you starting the loop at 1? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 10, 2010 Share Posted July 10, 2010 You don't need the for loop at all. remove the for loop completely. You just need to setup a simple counter in your secound while loop $i = 1; while($row = mysql_fetch_array($result)){ echo "<input type='hidden' name='L_NAME$i' value='$row[PrDispName]'>"; echo "<input type='hidden' name='L_AMT$i' value='$row[PrPrice]'>"; echo "<input type='hidden' name='L_QTY$i' value='$PrIDs[CartPrQty]'>"; echo "<input type='hidden' name='L_DESC$i' value='$row[PrDesc]'>"; echo '<tr><td width="65%"><a href="products.php?id=' . $row['PrID'] . '"> ' . $row['PrDispName'] . '</a></td><td><img src="' . $row['PrImage'] . '" alt=' . $row['PrName'] . '" height="50px" width="50px"></td>'; echo '<td>$' . $row['PrPrice'] . '</td>'; echo '<td>Qty:' . $PrIDs['CartPrQty'] . '</td>'; echo '<tr><td colspan="4"><hr/></td></tr>'; echo '</tr>'; $total = $row['PrPrice'] * $PrIDs['CartPrQty']; $i++ } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 10, 2010 Share Posted July 10, 2010 You should use HTML arrays - http://www.php.net/manual/en/faq.html.php#faq.html.arrays Using a series of numbered variables will make your form processing code more complicated and slower. Quote Link to comment Share on other sites More sharing options...
NickG21 Posted July 10, 2010 Author Share Posted July 10, 2010 hey guys thanks for the responses wolphie, yes the integer was positive, i had echoed the final values and have been messing with the db's as well wildteen, i tried your suggestion and continue to get the same results, with $i being equal to 0, or 1, depending on what i set the value to. Also tried using $i += 1; to make sure that it wasn't just trying to increment by 0, and got the same results. Quote Link to comment Share on other sites More sharing options...
NickG21 Posted July 10, 2010 Author Share Posted July 10, 2010 hey thanks for the suggestion with using arrays, if i was to echo the hidden inputs such as this echo "<input type='hidden' name='LDESC[]' value='blah'>"; inside of the while loop iterating through the resultset, does it automatically assign the proper array id values such as LDESC[0] etc.. for each result? thanks again for the input Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 10, 2010 Share Posted July 10, 2010 Are the incorrect results in your form processing code or did you actually look at the 'view source' of the form in your browser? As to the code question, you would assign a specific index value using the $i variable - echo "<input type='hidden' name='LDESC[$i]' value='blah'>"; Quote Link to comment Share on other sites More sharing options...
NickG21 Posted July 10, 2010 Author Share Posted July 10, 2010 i've actually went through and viewed the source after every reload and change i made to the file and they remain the same. i have also been echoing the values of $i, which increment correctly, but when viewed through the source, i can see that everything is echoing as follows: value of i is 0 ----all hidden values for all results----- value of i is 1 value of i is 2 etc.... so it is obviously in the syntax of everything im just unsure of where my mistakes are being made, and where to go through and increment these values. thanks for the help Quote Link to comment 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.