Jump to content

Incrementing Through ResultSet While Loop


NickG21

Recommended Posts

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.

Link to comment
Share on other sites

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++
                }

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.