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); } Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/ Share on other sites More sharing options...
aebstract Posted January 28, 2009 Author Share Posted January 28, 2009 bump Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/#findComment-748739 Share on other sites More sharing options...
aebstract Posted January 29, 2009 Author Share Posted January 29, 2009 bump Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/#findComment-749549 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 Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/#findComment-749741 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); } Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/#findComment-749907 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. Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/#findComment-749912 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. Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/#findComment-749925 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. Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/#findComment-749929 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. Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/#findComment-749961 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?. Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/#findComment-749965 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. Link to comment https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/#findComment-749984 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.