Oscar Oscar - 13 days ago 7
MySQL Question

php / MySQL - How to join 'cart' table together

How can I join this table together, showing the header only once? I have tried to take th out of the while loop but I had no luck, maybe I made a mistake?

Here is my code so far:

<?php
function cart() {
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));?>
<center>
<table class='menufinal' border=0 width=75%>
<th>Remove Item</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Line Total</th>
<?php while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;?>
<tr>
<td><?echo '<a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br>'?></td>
<td><?echo $get_row['name']?></td>
<td><?echo '&pound' . number_format($get_row['price'], 2);?></td>
<td><?echo '<a href="cart.php?remove=' .$id. '"style="text-decoration:none">- </a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a>' ?> </td>
<td> <?echo '&pound ' . number_format($sub, 2);?> </td>
</tr>
<?
}
}
if (empty($total)) {

if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
if (!empty($total)){
echo '<br>Total: &pound' . number_format($total, 2) . '<br>';
echo '<div id="dorc"><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="200"> <a href="checkout.php"><img src="checkout.png" width="240" height="152"></a>';
} else {
header ('Location: index.php');
}
}

?>


Currently this code displays:

enter image description here

Answer

Below is the corrected code, the creation of the table is moved outside of the loop.

Also the table header was added into a tr to create valid html, and also collected data into an $output variable to prevent a bug with your location redirect since data would already be written to the browser (you may vary well still have a problem with this since there is likely other output prior to this cart() function being called.

<?php 
function cart() {

    $output = '';
    $output .= '<center>';
    $output .= '<table class='menufinal' border=0 width=75%>';
    $output .= '<tr>';
    $output .= '<th>Remove Item</th>';
    $output .= '<th>Item Name</th>';
    $output .= '<th>Item Price</th>';
    $output .= '<th>Quantity</th>';
    $output .= '<th>Line Total</th>';
    $output .= '</tr>';

    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));
               while ($get_row = mysql_fetch_assoc($get)) {
                    $sub = $get_row['price']*$value;
                    $output .= '<tr>';
                    $output .= '<td><a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br></td>';
                    $output .= '<td>' . $get_row['name'] . '</td>';
                    $output .= '<td>&pound ' . number_format($get_row['price'], 2) . '</td>';
                    $output .= '<td><a href="cart.php?remove=' .$id. '"style="text-decoration:none">- </a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a></td>';
                    $output .= '<td>&pound ' . number_format($sub, 2) . '</td>';
                    $output .= '</tr>';
                }
            } 
            if (empty($total)) {

                if (empty($sub)) {
                    //do nothing
                } else {
                    $total = $sub;
                }
            } else {
                $total += $sub;
            }
        }
    }

    $output .= '</table>';

    if (empty($total)){
        header ('Location: index.php');
        exit;    
    }

    $output .=  '<br>Total: &pound' . number_format($total, 2) . '<br>';
    $output .=  '<div id="dorc"><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="200"> <a href="checkout.php"><img src="checkout.png" width="240" height="152"></a>';

    echo $output;
}

?>