php cart items with multiple options
<p>First of all I am new to OOP in PHP and a beginner to PHP in general.</p>

<p>I am working on a shopping cart for a friend's website. I found some code for a basic cart and am attempting to modify it to suit my purposes. </p>

<p>the code uses an ID that matches with a prod in the database and im ok with that. the problem begins when I try to add modifiers to the product. ex prod 1d and a color. this is sent to the cart in the form of a get and in this format. cart.php?action=add&amp;id=1&amp;color=c1</p>

<p>the cart then queries the database and pulls the prod name it also checks for dup id's. This is where I am getting lost. </p>

<p>Here is the cart code.</p>

<pre><code>function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
    return '&lt;p&gt;You have no items in your shopping cart&lt;/p&gt;';
} else {
    // Parse the cart session variable
    $items = explode(',',$cart);
    $s = (count($items) &gt; 1) ? 's':'';
    return '&lt;img src="images\cart.jpg" alt="cart" /&gt;You have &lt;a href="/cart/cart.php"&gt;'.count($items).' item'.$s.' in your cart&lt;/a&gt;';
}
}

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;
    }
    /*$colors = explode('c',$item);
    foreach ($item as $cid) {

        }*/
    $output[] = '&lt;form action="cart.php?action=update" method="post" id="cart"&gt;';
    $output[] = '&lt;table&gt;';
    foreach ($contents as $id=&gt;$qty) {
        $sql = 'SELECT * FROM products WHERE id = '.$id;
        //$sql2 = 'SELECT * FROM colors WHERE id = '.$cid;
        $result = $db-&gt;query($sql);
        //$result2 = $db-&gt;query($sql2);
        $row = $result-&gt;fetch();
        //$row2 = $result2-&gt;fetch();
        extract($row);
        //extract($row2);
        $output[] = '&lt;tr&gt;';
        $output[] = '&lt;td&gt;&lt;a href="cart.php?action=delete&amp;id='.$id.'" class="r"&gt;Remove&lt;/a&gt;&lt;/td&gt;';
        $output[] = '&lt;td&gt;'.$type.'&lt;/td&gt;';
        //$output[] = '&lt;td&gt;'.$color.'&lt;/td&gt;';
        $output[] = '&lt;td&gt;$'.$price.'&lt;/td&gt;';
        $output[] = '&lt;td&gt;&lt;input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /&gt;&lt;/td&gt;';
        $output[] = '&lt;td&gt;$'.($price * $qty).'&lt;/td&gt;';
        $total += $price * $qty;
        $output[] = '&lt;/tr&gt;';
    }
    $output[] = '&lt;/table&gt;';
    $output[] = '&lt;p&gt;Grand total: &lt;strong&gt;$'.$total.'&lt;/strong&gt;&lt;/p&gt;';
    $output[] = '&lt;div&gt;&lt;button type="submit"&gt;Update cart&lt;/button&gt;&lt;/div&gt;';
    $output[] = '&lt;/form&gt;';

} else {
    $output[] = '&lt;p&gt;Your cart is empty.&lt;/p&gt;';
}
return join('',$output);
}
</code></pre>

<p>I have commented out the lines that I modified in an attempt to figure this out. I am thinking if I explode and break on 'c', the color identifier, then loop through again with a foreach then I can create a var for each c. then I need to either change the id in my database to show c# or parse out the c then query based on that. I am having trouble writing the code for that though. </p>

<p>Also if you dont mind can someone explain the => and how its used in PHP. I have done searches but they dont really explain it well. neither does the php.net site.</p>

<p>thanks.</p>
