Forum Moderators: coopster
The code is as follows:-
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="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM products WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
$ig = '$image';
extract($row);
$_SESSION['ig'] = $image;
$output[] = '<tr>';
$output[] = '<td>'.$image.'</td>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$title.' by '.$author.'</td>';
$output[] = '<td>£'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>£'.($price * $qty).'</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
But I dont want it to be outputted using php formating I simply want to create a table in html and put in the session varibles where required.
The only problem is that when i do that and remove the item from the shopping cart its not removing it self.
Any ideas?
When you say you don't want to output formatted by PHP, what exactly do you mean?
When you say session variables are you instead referring to the item data from the database?
I think I see what you mean about the HTML, however since this is a function that returns a variable to be outputted by another part of the script it's hard to say how easy it would be for you to make the changes.
Basically what you would do...
Remove the print or echo elsewhere in the script that sends the variable returned by showCart() to the browser. There may be extra formatting or processing here too, which would make it more complicated.
In the showCart() function replace each instance of $output[] = with echo. Also remove the return line at the end of the function.
In the block of $outputs you could also use PHP stop and end if you want it to be more straightforward HTML, like so...
This:
$output[] = '<tr>';
$output[] = '<td>'.$image.'</td>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$title.' by '.$author.'</td>';
$output[] = '<td>£'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>£'.($price * $qty).'</td>';
$total += $price * $qty;
$output[] = '</tr>';
Would become:
$subtot = $price * $qty;
?>
<tr><td><?php echo $image; ?></td>
<td><a href="cart.php?action=delete&id=<?php echo $id; ?>" class="r">Remove</a></td>
<td><?php echo $title; ?> by <?php echo $author; ?></td>
<td>£<?php echo $price; ?></td>
<td><input type="text" name="qty<?php echo $id; ?>" value="<?php echo $qty; ?>" size="3" maxlength="3" /></td>
<td>£<?php echo $subtot; ?></td></tr>
<?php
$total += $subtot;
Hope that helps