holladb Posted November 20, 2007 Share Posted November 20, 2007 Hey fellas. I have a shopping cart script and i need to add product attributes. I have the attributes added to a separate array from the cart items. The color attribute is going in fine. The only problem is displaying. My problem: Say you added a product called 1F with the color yellow, you go back and add 2F with the color black. In your cart now you see: 1F - Yellow : price 1F - Black : price 2f - Yellow : price 2f - Black : price Does anybody have an idea of how i can properly display my attributes or have a better way of doing this! function showCart() { global $db; $cart = $_SESSION['cart']; $attributes = $_SESSION['attributes']; print($attributes); if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $at = explode(',',$attributes); $content = array(); foreach ($at as $a) { $content[$a] = (isset($content[$a])) ? $content[$a] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { foreach ($content as $att=>$qty2) { $sql = 'SELECT * FROM products WHERE cereal = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$row['itemcode'].' - '.$att.'</td>'; $output[] = '<td>$'.$row['pricemiddle'].'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>$'.($row['pricemiddle'] * $qty).'</td>'; $total += $row['pricemiddle'] * $qty; $output[] = '</tr>'; } } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>'; $output[] = '<div><input type="image" src="images/buttons/update.gif" border="0"> <a href="checkout.php"><img src="images/buttons/checkout.gif" border="0"></a></div>'; $output[] = '</form>'; } else { $output[] = '<p>Your shopping cart is empty.</p>'; } return join('',$output); } Link to comment https://forums.phpfreaks.com/topic/78090-solved-product-attributes/ Share on other sites More sharing options...
holladb Posted November 20, 2007 Author Share Posted November 20, 2007 i just thought about this a little bit, what if i put all of the data into 1 array... like $do = array(1F-Black-167,2F-Yellow-192); //and explode it $it = explode(',',$do); foreach($it as $i) { $new = explode('-',$i); echo $new; } But how would i get it to explode that data and set each $new as a separate variable? Link to comment https://forums.phpfreaks.com/topic/78090-solved-product-attributes/#findComment-395234 Share on other sites More sharing options...
holladb Posted November 20, 2007 Author Share Posted November 20, 2007 yeah that'll work thanks any way kids! Link to comment https://forums.phpfreaks.com/topic/78090-solved-product-attributes/#findComment-395243 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.