Hi,
this is my first post so I will say hello to all and thanks for your help in advance.
Ive been sat in front of the pc now for about 5 hours without any luck, so im now asking for help 🙂
If the code is badly written then please bare with me as I am still learning 🙂
I need to add items called from my databse in to the shopping cart but have failed over and over again..
Im getting no errors from the code just no output into the cart.
Here is a link so you can see what i am trying to do for yourself.
http://maworden.bimserver2.com/Year3/shopping%20cart/index.php
Basically if I change the objects to arrays then I can get it to work, but i need to use objects.
The code is pasted below, as this is the first time i have posted my code online I hope I haven't done the wrong thing by pasting it all...
<?php
session_start();
// SETUP FOR SHOPPING CART
$page = 'index.php'; // index set as a variable to be used later for Location Header direction
include 'connect.php'; // connection details to connect to the database
if (isset($_GET['add'])) //check to see if 'add' has a value and then proceed to the query
{
$stock = mysql_query('SELECT id, quantity FROM radios WHERE id="'.$row->id.'"');
while ($stock_row = mysql_fetch_object($stock))
{
if ($stock_row->quantity !=$_SESSION[$_GET['add']])
{
$_SESSION['item'.$_GET['add']]+='1';
}
}
header('Location: '.$page); //redirecting the click to index.php
}
if (isset($_GET['remove'])) //remove item from cart
{
$_SESSION['item'.$_GET['remove']]--;
header('Location: '.$page);
}
if (isset($_GET['delete']))
{
$_SESSION['item'.$_GET['delete']]='0';
header('Location: '.$page);
}
function cart () { //START OF SHOPPING CART
foreach($_SESSION as $name => $value)
{
if ($value>0) // if $value is greater than zero we ******** else we display 'your cart is empty' to the user.
{
if (substr($name, 0, 4)=='item')
{
$id = substr($name, 4, (strlen($name) -4)); // this allows me to extract the number from each 'cart_' by removing the first 5 characters from the $name variable. EG. cart_7 can be broken down in to (1)c - (2)a - (3)r - (4)t - (5)_ KEEPING (6)-->7<--. So we remove characters 1, 2, 3 , 4 ,5 or 'cart_' which leaves us with the cart number only, in our case '7'
include 'connect.php';
$sql = mysql_query('SELECT id, make, model, price, description FROM radios WHERE id="'.$row->id.'"');
$result = mysql_query($sql, $conn);
if (!$result) {
echo "There are no pencils results obtained from the search criteria".$_GET['search'];
}
else {
while ($row = mysql_fetch_object($sql))
{
$subtotal = $row->price * $value;
// calculates product price * number of product = $subtotal
echo $row->make.': '.$row->model.' x '.$value.' @ £'.number_format($row->price, 2).' <a href="cart.php?remove='.$row->id.'">[-]</a> <a href="cart.php?add='.$row->id.'">[+]</a> <a href="cart.php?delete='.$row->id.'">[Delete]</a><br />';
}
}
$total += $subtotal;
}
}
if ($total==0)
{
echo '<img src="cartlogo.jpg" height="100" width="100"> <br />
Your cart is empty';
}
else
{
echo 'Total: £'.number_format($total, 2).' <br />
<img src="cartlogo.jpg" height="100" width="100"> ';
echo '<a href="INSERT LINK HERE">Checkout</a>';
}}}
function search() //SEARCH FUNCTION
{
//if the search is not executed then the search form will continue to be displayed. Once submit = true then the 'else' statement is exectued.
if(!isset($_POST['finding'] )) {
echo '<form name="search" method="post" action="".echo $PHP_SELF;."">
<p>Search the Museum Exhibition</p>
<p>
<input name="userinput" type="text" value="search museum here">
</p>
<p>
<select name="field">
<option value="make" selected="selected">Model</option>
<option value="model">Manufacturer</option>
</select>
</p>
<input type="hidden" name="finding" value="yes" />
<input type="submit" name="search" value="search" />
';
}else{
$userinput= $_POST["userinput"];
echo 'VARIABLE TEST 1: '.$userinput.'<br />';
$userchoice = $_POST["field"];
echo 'VARIABLE TEST 2: '.$userchoice;
include 'connect.php';
$sql = ( 'SELECT * FROM radios WHERE '.$userchoice.' LIKE "%'.$userinput.'%"');
$result = mysql_query($sql, $conn);
if (!$result) {
echo "There are no radios results obtained from the search criteria".$_GET['search'];
}
//I THINK THIS IS WHERE THE PROBLEM IS BUT I JUST CANT FIGURE OUT HOW TO MAKE THE WHILE LOOP WORK WITH THE ELSE,
//AS IT *** DOESNT *** FETCH ALL RESULTS AND STOP WHEN NO MORE ARE AVAILABLE LIKE ITS SUPPOSED TOO.
//IT ONLY DISPLAYS THE 1 RESULT
else {
while ($row= mysql_fetch_object($result))
{
// PS when using TH doesnt that already make the text strong.. thats why its different to TD?
echo '<table>
<tr><th scope="row">Make:</th><td>' . $row->make . '</a></td></tr>
<tr><th scope="row">Model</th><td>' . $row->model. '</td></tr>
<tr><th scope="row">Description</th><td>' . $row->description . '</td></tr>
<tr><th scope="row">Price</th><td>' . $row->price . '</td></tr>
<tr><th scope="row">Quantity</th><td>' . $row->quantity . '</td></tr>
<center><a href="cart.php?add='.$row->id.'"> Add to Cart </a></center></p>
</table>';
//sets the number format to two decimal places, also calls each field from the array and displays it
}
// THE TABLE BELOW DISPLAYS CORRECTLY WITH ONLY ONE ENTRY WHEN THERE SHOULD BE 2 OR 3
}}}
?>