...

View Full Version : While Loop, Inside While Loop only displays once.



the-dream
10-10-2009, 12:48 PM
Hi guys...

I'm building a shopping cart... and cannot understand why the second while loop only runs once, is should runs twice (the second time the first loop does). All the correct data is in the MySQL, i've checked, double checked, and triple checked it, but cannot figure out what's wrong, any help would be appreciated, code below.


<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td class="in"><b>Item Name</b></td>
<td class="ip"><b>Item Price</b></td>
<td class="sk"><b>SKU</b></td>
<td class="qt"><b>Quantity</b></td>
<td class="qt"><b>Sub-Total</b></td>
<td class="st"><b>Actions</b></td>
</tr>
<?php
$runningTotal = 0.00;
while($c = mysql_fetch_array($cart)) {
$pid = $c['product_id'];
$productInfo = mysql_query("SELECT * FROM catalouge1 WHERE id = '$pid'");
$pinf = mysql_fetch_array($productInfo);
?>
<tr>
<td class="in_i" align="left" valign="middle"><?php echo $pinf['item_name']; ?> (<?php echo $pid; ?>)</td>
<td class="ip_i" align="left" valign="middle">&pound;<?php echo $pinf['sale_price']; ?></td>
<td class="sk_i" align="left" valign="middle"><?php echo $pinf['sku']; ?></td>
<td class="qt_i" align="left" valign="middle"><input type="text" style="width: 30px;" name="<?php echo $c['id']; ?>" value="<?php echo $c['qty']; ?>" /> (Max <?php echo $pinf['in_stock']; ?>)</td>
<td class="qt_i" align="left" valign="middle" width="96">&pound;<?php echo number_format($pinf['sale_price'] * $c['qty'], 2, ".", ","); ?></td>
<td class="st_i" align="left" valign="middle" width="95" align="center"><a href="/deletefromcart/<?php echo $c['id']; ?>/">Remove Item</a></td>
</tr>
<?php
if(mysql_num_rows($ccart) > 0) {
$for_product = $pid;
while($cc = mysql_fetch_array($ccart)) {
if($cc['for_product'] == $for_product) {
$cid = $cc['chain_id'];
$chainInfo = mysql_query("SELECT * FROM chain_details WHERE id = '$cid'");
$cinf = mysql_fetch_array($chainInfo);
$chain_style = $cinf['style_id'];
$chainStyleInfo = mysql_query("SELECT * FROM chains WHERE id = '$chain_style'");
$csinf = mysql_fetch_array($chainStyleInfo);
?>
<tr>
<td class="in_i" align="left" valign="middle"><?php echo $csinf['name']; ?> Chain - <?php echo $cinf['length']; ?>"</td>
<td class="ip_i" align="left" valign="middle">&pound;<?php echo $cinf['sale_price']; ?></td>
<td class="sk_i" align="left" valign="middle"><?php echo $cinf['sku']; ?></td>
<td class="qt_i" align="left" valign="middle" height="22"><?php echo $cc['qty']; ?></td>
<td class="qt_i" align="left" valign="middle" width="96">&pound;<?php echo number_format($cinf['sale_price'] * $cc['qty'], 2, ".", ","); ?></td>
<td class="st_i" align="left" valign="middle" width="95" align="center"><a href="/deletefromcart/chain/<?php echo $cc['id']; ?>/">Remove Item</a></td>
</tr>

<?php
}
$runningTotal = $runningTotal + ($cinf['sale_price'] * $cc['qty']);
}
}
?>
<?php
$runningTotal = $runningTotal + ($pinf['sale_price'] * $c['qty']);
}
?>
</table>
<div align="right">
<table cellpadding="0" cellspacing="0" border="0" style="padding: 4px; border-left: 1px solid #333333; border-right: 1px solid #333333; border-bottom: 1px solid #333333;" width="210">
<tr>
<td>
<b>Made Changes? &nbsp;</b>
<input type="submit" value="Update Cart!" />
</td>
</tr>
</table>

Thanks,
~ Christian

Eran
10-10-2009, 07:18 PM
first, well I'm sorry you are too messing up with the code sql within sql.
I can't read this without trying the code myself.

if it was my script the thing that I would do is to debug my-self. just print step-by-step the info you want to retrieve and find whats wrong..

second, read here: http://www.w3schools.com/Sql/sql_join.asp about sql joins it's will improve 10 times your work (and it will be readable to all).

good luck!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum