I'm developing a website to allow a user to buy games, and am having a couple of problems with the shopping basket part of the website:
I'm currently working on three of the pages of my website: home.php, gamesByPrice.php and shoppingBasket.php
Both home.php and gamesByPrice.php display a list of games in a table, including their title, price, description, and a checkbox, along with an 'Add to basket' button below the table, to add the selected games to the shopping basket.
Currently, I am able to add games to the shopping basket from the home.php page, by checking a couple of the checkboxes in the table, and clicking the 'Add to basket' button. When I click the button, I am taken to shoppingBasket.php, which displays a list of the games I had selected using the checkboxes on the previous page, along with their prices, and text box to allow me to enter the number of each item I would like to buy (this text box automatically has the value '1' the first time you view the page). However, if the user clicks back, on this page, and then clicks forward again, the value of the text box is automatically increased to '2', even though the user did not select the games from the list again, or click the 'Add to basket' button again.
Any idea how I would set this text box value to only increase when the user clicks the 'Add to basket' button on the previous page, rather than just each time they view this page?
I also have another function on my home.php page, which allows the user to search for games within a certain price range by entering a minimum and maximum price value into two separate text boxes, and then clicking a 'search' button. When the user does this, an SQL query is run, which queries a PostgreSQL database table, where all of the game information is stored. The query results are then stored in a PHP variable, and printed out to a table in my gamesByPrice.php page using a while loop. The table has the same format as the table displaying all of the games in the database, which is on the home.php page, with the same checkboxes and 'Add to basket' button as is on the home.php page.
However, when I select games on this page, and click the 'Add to basket' button, I am taken to the shopping basket page, but instead of seeing the games that I've just added displayed there, I get an error message which says "Warning: Invalid argument supplied for foreach() in filepath/shoppingBasket.php on line 37".
The code for my shoppingBasket.php page is below, line 37 is the start of the foreach loop under the comment which says
// add items from $POST in home2.php to basket- this function is working (it's getting the games)- now need to
// make sure it saves to the session properly
Code: Select all
<?php
session_start();
// retrieve stored session data (not display), after checking that
// $_POST['userName'] is not empty
if(!empty($_POST['userName'])){
$_SESSION['userName']=$_POST['userName'];
}
$conn_string = "host=***** port=**** dbname=***** user=***** password=*****";
$dbconn = pg_connect($conn_string);
?>
<?php
// allow user to remove all quantities of one item
if(isset($_GET['action']) && $_GET['action'] = 'delete'){
$basket = $_SESSION['basket'];
if ($basket){
$items = explode(',',$basket);
$contents = '';
foreach($items as $item){
if($item != $_GET['id']){
$contents .= $item.',';
}
}
$_SESSION['basket'] = $contents;
}
}
?>
<?php
// add items from $POST in home2.php to basket- this function is working (it's getting the games)- now need to
// make sure it saves to the session properly
if(isset($_POST['selectGame'])){
foreach($_POST['selectGame'] as $game){
echo $game;
echo '<br />';
$_SESSION['basket'] .= $game.',';
}
}
?>
<html>
<head>
<title>Shopping Basket</title><a href="logout.php">Log Out</a>
</head>
<body>
<h1>Shopping Basket</h1>
<p>This is not a real web shop; it is created as part of my university coursework. Please do not attempt
to buy anything from this site, nor enter your real card details.</p>
<?php
// this should display session data
echo "Welcome ".$_SESSION['userName']."!";
// Will also need to retrieve the games selected on either home2.php or gamesByPrice.php
// Function to display message "You have X items in your shopping basket."
// This is not currently being displayed on the shoppingBasket.php page, but
// code will be useful for other pages
function displayNumberOfItemsInShoppingBasket(){
$basket = $_SESSION['basket'];
if(!$basket){
return '<p>You have no items in your shopping basket</p>';
} else{
// Parse the basket session variable
$items = explode(',',$basket);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="shoppingBasket.php">'.count($items).' item'.$s.' in your shopping basket</a></p>';
}
}
/*// Now create the shopping basket.
$basket = $_SESSION['basket'];
if($basket){
$basket .= ','.$_GET['id'];
}else{
$basket = $_GET['id'];
}
$_SESSION['basket'] = $basket;
*/ ``
?>
<?php
// allow user to add more than one of the same item to the cart.
$basket = $_SESSION['basket'];
if ($basket){
$items = explode(',',$basket);
$contents = array();
foreach($items as $item){
if($item !='')
$contents[$item] = (isset($contents[$item])) ? $contents[$item] +1:1;
}
}
?>
<?php
// Display the details for each individual product the user has selected
$totalGamesSelected = 0;
echo '<table>';
print_r($contents);
if(count($contents)){
foreach($contents as $id => $quantity){
echo $id;
$selectedGameInfoQuery = 'SELECT * FROM CSGames WHERE refnumber = '.$id;
$selectedGameInfoQueryResult = pg_query($dbconn, $selectedGameInfoQuery);
while($row = pg_fetch_assoc($selectedGameInfoQueryResult)){
// extract($row);
$output = '<tr>';
$output .= '<td><a href="shoppingBasket.php?action=delete&id='.$id.'"class="r">Remove</a></td>';
$output .= '<td>'.$row['title'].'</td>';
$output .= '<td>£'.$row['price'].'</td>';
$output .= '<td><input type="text" name="quantity'.$id.'" value="'.$quantity.'"size="3" maxlength="3" /></td>';
$output .= '<td>£'.($row['price'] * $quantity).'</td>';
$total += $row['price'] * $quantity;
$output .= '</tr>';
echo $output;
}
}
echo '</table>';
echo '<p>Grand total: £'.$total.'</p>';
} else{
echo("You have not added anything to your shopping basket!");
}
// $output[] = '</table>';
// $output[] = '<p>Grand total: £'.$total.'</p>';
$_SESSION['total']= $total;
?>
<form id="ProceedToCheckout" action="checkout.php" method="post" />
<input type="submit" name="ProceedToCheckout" value="Proceed To Checkout" />
</form>
</body>
</html>