/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Simple Increment in While Loops not working

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.

[code=php]

$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;
}
}[/code]

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.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@dk_zero-coolJul 10.2010 — There is nothing wrong with that for loop. And I'm not sure what you want to do?

The reason why the output prints 1 every time, is that you are looping more than once pr "for loop". The "for" loops one time giving you the value "1". Then the second "loop", in this case a "while", loops the second part, which is still during the first loops first run, then for each of them you run a third loop, still during the first loops first run.

Better example. Lets say we have two arrays.
[code=php]$array1 = array(1, 2);
$array2 = array(10, 11, 12);[/code]


Now we loop the first one, and then the second inside the first.
[code=php]foreach($array1 as $value1) {
foreach($array2 as $value2) {
echo $value1." - ".$value2;
}
}[/code]


This will output:

1 - 10

1 - 11

1 - 12

2 - 10

2 - 11

2 - 12

Now, I'm not sure what you want, but I think that you want a second counting variable inside the third loop.

Something like this, using the above example.
[code=php]$count2 = 0;
foreach($array1 as $value1) {
foreach($array2 as $value2) {
$count2 += 1;
echo $count2 ." - ".$value2;
}
}[/code]


This will output:

1 - 10

2 - 11

3 - 12

4 - 10

5 - 11

6 - 12
Copy linkTweet thisAlerts:
@tirnaJul 11.2010 — I think your FOR loop and one of your WHILE loops might be redundant.

Looking at your 2 queries it looks to me you might be able to combine the 2 queries into 1 and then just use 1 while loop to display the query results.

It looks like both your tblCart and tblProduct have a product_id column

If that is the case you should be able to use a query something like:

$query = 'select * from tblProduct p, tblCart c
where c.prod_id = p.prod_id
and CartSessionID = "'.$_SESSION['SessID'].'"';
and then

<i>
</i>$rs = mysql_query($query, $conn);

$count = 0;

while($row=mysql_fetch_assoc($rs)) {

$count = $count + 1; //this is for your L_NAME etc parameters

//display your query results

}

$rs should contain all the info in tblProduct for each prod_id in your cart for a given cartSessionID and so you would only need 1 query and 1 while loop to do what 2 queries, 1 for loop and 2 while loops are aiming to do.
×

Success!

Help @NickG21 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...