I am making a shopping cart for university project and following tutors sample code but tweaking it to fit my own design.
my site products are named such as
cutecupcak.es/product.php?id=vanilla_cupcakes
rather than
cutecupcak.es/product.php?id=2
The code for the shopping cart page to display the products in your cart is
if(is_numeric($id)) {
$cartresult = mysql_query($sql);
but my product urls are NOT numeric and so the shopping cart is not displaying the results, as you can see here -- http://cutecupcak.es/shoppingcart.php - if you test it out.
it will work if you have products.php?id=2
etc. though
what do i need to change from - is_numeric
to make it work with my url format?
full code
<?php
session_start(); // start new session or call exisiting session
include('include/dbconnect.php'); // include the database connections
function renderCartForm()
{
$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="shoppingcart.php?action=update" method="post" id="cart">';
$output[] = '<div class="form">';
$output[] = '<table border="2" class="formcontainer">';
$output[] = '<tr class="formlabels">';
$output[] = '<td width="400">Cake Name</td>';
$output[] = '<td width="75">Price</td>';
$output[] = '<td width="75">Quantity</td>';
$output[] = '<td width="100">Total</td>';
$output[] = '<td width="100">Remove?</td>';
$output[] = '</tr>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM `CAKE` WHERE `cake_url` = '.$id;
if(is_numeric($id)) {
$cartresult=mysql_query($sql);
while($cartrow = mysql_fetch_array($cartresult)) {
$output[] = '<tr>';
$output[] = '<td>'.$cartrow['cake_name'].'</td>';
$output[] = '<td>'.$cartrow['cake_price'].'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="2" maxlength="2" /></td>';
$output[] = '<td>£'.($cartrow['cake_price'] * $qty).'</td>';
$output[] = '<td><a href="shoppingcart.php?action=delete&id='.$id.'">Remove</a></td>';
$total += $cartrow['cake_price'] * $qty;
$output[] = '</tr>';
}
}
}
$output[] = '</table>';
$output[] = '</div>';
$output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>';
$output[] = '<div class="\"formlabels\"><button type="submit">Update Cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>Your shopping cart is empty.</p>';
}
echo join('
',$output);
}
?>