Shopping cart issue, items wrongly displayed in table

zalex

New Member
When adding an item to the cart it creates the cart and then displays the table holding the items added information. Just whenever a new item is added it creates a new table below the original one and re-creates the column headings when they should be just being added as a new row sharing the column headings as one table. My code so far: \[code\]function minicart(){ $items = 0; $tbl = array(); foreach($_SESSION as $name => $value) { if ($value > 0) { if (substr($name, 0, 5)=='cart_') { $id = substr($name, 5, (strlen($name) -5)); $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id)); $tbl[] = '<table border="1"><thead><tr>' . '<th>Item</th>' . '<th>Quantity</th>' . '<th>Unit Price</th>' . '<th>SubTotal</th>' . '<th>Action</th>' . '</tr></thead><tbody>' ; while ($get_row = mysql_fetch_assoc($get)) { $items++; $sub = $get_row['price'] * $value; $tbl[] = '<tr>' . '<td>' . $get_row['name'] . '</td>' . '<td>' . $value . '</td>' . '<td>&pound;' . number_format( $get_row['price'], 2 ) . '</td>' . '<td>$pound;' . number_format( $sub, 2) . '</td>' . '<td>' . ' <a href="http://stackoverflow.com/questions/15466734/minicart.php?remove=' . $id . '">[-]</a> ' . ' <a href="http://stackoverflow.com/questions/15466734/minicart.php?add=' . $id . '">[+]</a> ' . ' <a href="http://stackoverflow.com/questions/15466734/minicart.php?delete=' . $id . '">[Delete]</a>' . '</td>' . '</tr>' ; } $tbl[] = '</tbody>'; } $total += $sub; } } if ($items==0) { echo "Your cart is empty"; } else { $tbl[] = '<tfoot><tr>' . '<td colspan="3" style="text-align:right; font-weight:bold">Total:</td>' . '<td>&pound;' . number_format($total, 2) . '</td></tr></tfoot></table>'; echo implode( "\n", $tbl ); }}\[/code\]Any suggestions? Thanks
 
Back
Top