aebstract Posted January 26, 2009 Share Posted January 26, 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) { $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 - $'.$price.'</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); } Okay, to add to the session is something like action=add&id=#. Which then this script will take the session and divide up all the items. So the array may be like 8,8,8,4,6,6,3,2,7,8. Which with this script will display having 4 of item 8, 2 of item 6, etc. I need to add a special thing to this and I about have it figured out but kinda lost. So I am going to give the option of a "size", the way I am thinking of formatting this is to add a ;# on to my numbers I have. So add&id=#;# = 8;1.5. Then in my array it would be: 8;1.5,8;1.5,8;1.2,4,6,6,3;1.4,2,7,8 Something like that, and explode the results of the first explode by ; to separate the first number from the second, then I need to set the first number as $id and would like to set the second number as $size. Can anyone point me in the right direction? I've already got it so it adds the ;# in the session, I just need to figure out how to explode and set the variable ;( Quote Link to comment https://forums.phpfreaks.com/topic/142513-solved-help-with-explode-array-and-cart/ Share on other sites More sharing options...
aebstract Posted January 26, 2009 Author Share Posted January 26, 2009 The reason I am looking for some guidance is cause I really suck with arrays. I don't think this should be too hard to implement. Quote Link to comment https://forums.phpfreaks.com/topic/142513-solved-help-with-explode-array-and-cart/#findComment-746841 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 <?php $var = explode(';',$explodedVar); $cnt = count($var); for ($i=0; $i<$cnt; $i++) { list($id, $size) = explode(", ", $var[$i]); echo "ID is $id and Size is $size <br />"; } ?> There is an example of how it will be done. Quote Link to comment https://forums.phpfreaks.com/topic/142513-solved-help-with-explode-array-and-cart/#findComment-746855 Share on other sites More sharing options...
aebstract Posted January 27, 2009 Author Share Posted January 27, 2009 I switched around the , and the ; since it needs to explode by , first. Here it is: function showCart2() { $cart = $_SESSION['cart']; $var = explode(',',$cart); $cnt = count($var); for ($i=0; $i<$cnt; $i++) { list($id, $size) = explode("; ", $var[$i]); echo "ID is $id and Size is $size <br />"; } } It is echoing: ID is 8 and Size is ID is 10;1.5 and Size is ID is 3 and Size is I'm gonna continue to chug away at it and see if I can get it to separate the 10 from 1.5 and set 1.5 as the size variable and keep 10 as the id. If anyone has any input that would be great! Quote Link to comment https://forums.phpfreaks.com/topic/142513-solved-help-with-explode-array-and-cart/#findComment-747591 Share on other sites More sharing options...
aebstract Posted January 27, 2009 Author Share Posted January 27, 2009 haha, it was a space after the ;. I think this topic is solved. Quote Link to comment https://forums.phpfreaks.com/topic/142513-solved-help-with-explode-array-and-cart/#findComment-747594 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.