aebstract Posted January 28, 2009 Share Posted January 28, 2009 Having trouble with this and don't really know what to call it. I've got my session set as something like: 4,4,5;10,3,5 Which is fine, and when I run it through to my shopping cart, I get my results displayed where the number = id in database. However, for the item 5;10, it just displays the information for the item above it and doesn't do anything for it. What I'm trying to do is check to see if the "id" has a ";" in it and if it does, explode that in to 2 parts, "5" and "10" and set the 5 as the id and use the 10 as "$size". Here is what I have: function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">'; foreach ($contents as $id=>$qty) { if(!strrpos($id, ";")){ list ($id, $size) = explode (";", $id); } $sql = 'SELECT * FROM p_products WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<table width="590" style="border-top: 1px solid #000;">'; $output[] = '<tr>'; $output[] = '<td width="100"><img src="products/' .$partnumber. '-cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>'; $output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size - '.$size.'</td>'; $output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td width="80">$'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '</tr>'; $output[] = '</table>'; } $output[] = '<p align="right">Grand total: $'.$total.'</p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } Quote Link to comment Share on other sites More sharing options...
aebstract Posted January 28, 2009 Author Share Posted January 28, 2009 bump Quote Link to comment Share on other sites More sharing options...
aebstract Posted January 29, 2009 Author Share Posted January 29, 2009 bump Quote Link to comment Share on other sites More sharing options...
milesap Posted January 29, 2009 Share Posted January 29, 2009 list($id, $size) = explode(';', $cart); If there is no ; then $size will return as null, and $id will carry the full $cart variable Quote Link to comment Share on other sites More sharing options...
aebstract Posted January 29, 2009 Author Share Posted January 29, 2009 Okay I took the entire if statement out and replaced it with what you stated. Here is what I am getting: ID is 8 and there is no size! ID is 4 and Size is 1.5 this is with: 8,4;1.5 which is correct, but this isn't part of my query dealing with the db. the part with the query is display this: [broken img]$ - size - 1.5 [broken img]$ - size - 1.5 The broken img, is suppose to be /products/$partnumber-cart.jpg it grabs that partnumber from the database based on id, it is outputting /products/-cart.jpg Here is the code: function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">'; foreach ($contents as $id=>$qty) { list($id, $size) = explode(';', $cart); $sql = 'SELECT * FROM p_products WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<table width="590" style="border-top: 1px solid #000;">'; $output[] = '<tr>'; $output[] = '<td width="100"><img src="products/' .$partnumber. '-cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>'; $output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size - '.$size.'</td>'; $output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td width="80">$'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '</tr>'; $output[] = '</table>'; } $output[] = '<p align="right">Grand total: $'.$total.'</p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 29, 2009 Share Posted January 29, 2009 where the id cumming from . is it set. Quote Link to comment Share on other sites More sharing options...
aebstract Posted January 29, 2009 Author Share Posted January 29, 2009 The id is coming from that array which I explained. The session variable is something like 4,4,4,4,etc and it explodes apart by the , and turns those in to the id's. Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 29, 2009 Share Posted January 29, 2009 slap or die mysql_error around the sql statement's. echo out the query's see if it what expected. Quote Link to comment Share on other sites More sharing options...
aebstract Posted January 29, 2009 Author Share Posted January 29, 2009 function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">'; foreach ($contents as $id=>$qty) { list($id, $size) = explode(';', $cart); $sql = mysql_query("SELECT * FROM p_products WHERE id = '.$id.'") or die(mysql_error());; echo "$sql"; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<table width="590" style="border-top: 1px solid #000;">'; $output[] = '<tr>'; $output[] = '<td width="100"><img src="products/' .$partnumber. '-cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>'; $output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size - '.$size.'</td>'; $output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td width="80">$'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '</tr>'; $output[] = '</table>'; } $output[] = '<p align="right">Grand total: $'.$total.'</p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } I added one item to my cart, without a ; just a single item to make it easy and now it's not even doing that correctly. Resource id #5, however it is actually set as 8. Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 29, 2009 Share Posted January 29, 2009 the only thing i see. is that you got no name for the submit button and post insist uppercase POST. what is the actual problem? and what does it not do please?. Quote Link to comment Share on other sites More sharing options...
aebstract Posted January 29, 2009 Author Share Posted January 29, 2009 Well for starters, look at what I said in my last reply: "Resource id #5, however it is actually set as 8. " I selected a part with id 8, the session has it set as id 8, though it is reading as "Resource id #5". Also read what I said about the images, the $id is not being set correctly with this. 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.