Forum Moderators: coopster

Message Too Old, No Replies

Shopping cart script

         

adamnichols45

3:08 pm on Apr 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Im trying to re-configure a shopping cart script but Im having a problem with the formatting of the database output.

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>&pound;'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>&pound;'.($price * $qty).'</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>&pound;'.$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?

IanKelley

3:35 am on Apr 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Referring to the code you posted... what an odd and inefficient way to handle the output, I hope the rest of the script isn't written similarly.

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?

adamnichols45

7:29 pm on Apr 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes that script was taken from the internet!

I wanted to simply build a table using html and put in the fields of data where requried. using php echo.

I have the script working as I want it nowbut using the very messy formatting that is seen above.

Not really what im after.

IanKelley

9:24 pm on Apr 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yeah I assumed it wasn't yours :-)

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>&pound;'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>&pound;'.($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>&pound;<?php echo $price; ?></td>
<td><input type="text" name="qty<?php echo $id; ?>" value="<?php echo $qty; ?>" size="3" maxlength="3" /></td>
<td>&pound;<?php echo $subtot; ?></td></tr>
<?php
$total += $subtot;

Hope that helps

adamnichols45

12:53 pm on Apr 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thats great mate thanks. Im going to have a little tinker with that now :)