I am trying to create a shopping basket using PHP, but can't seem to work out how to add items to the basket.
Currently, I have a table which contains a list of games, along with a price and description for each game. The table also has a column which contains a checkbox for every entry. What I want to do is that when a user checks one or more checkboxes, they should be able to click an 'add to basket' button, which would add the items whose checkbox they have ticked to the shopping basket. I then need to be able to display the contents of the shopping basket (i.e. all the games whose checkbox was ticked) on another page.
This is the code for the page containing the list of games:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
session_start();
// retrieve stored session data (this line doesn't display the session variable-
// it only retrieves it, ready to be used elsewhere in the page).
$_SESSION['userName']=$_POST['userName'];
?>
<html>
<head>
<title>Home</title><a href="logout.php">Log Out</a>
</head>
<body>
<h1>Home</h1>
<?php
// retrieve session data- userName (this line displays the session variable- as it
// has already been retrieved at the top of the file.
echo "Welcome ".$_SESSION['userName']."!";
// display how many items the user currently has in their shopping basket
function writeShoppingBasket(){
$basket = $_SESSION['basket'];
if(!$basket){
return '<p>You have no items in your shopping basket</p>';
}else{
$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>';
}
}
?>
<p>
<a href="shoppingBasket.php">View your shopping basket</a>
<br /></p>
<?php
echo extension_loaded('pgsql');
$conn_string = "host=***** port=**** dbname=***** user=***** password=*****";
$dbconn = pg_connect($conn_string);
if(!$dbconn){
die('Could not connect: ' . pg_error());
}
// generate and execute a simple query to check that the php script is connecting
// to the database, and that SQL queries will return required data.
$query = "SELECT * FROM CSGames";
$result = pg_query($dbconn, $query) or die("Error in query: $query." . pg_last_error($dbconn));
// Get the number of games in the resultset
$rows = pg_num_rows($result);
echo "\n There are currently $rows games in the database.";
?>
<p><br /></p>
<h1>Search for games by price: </h1>
<p>Enter price range of games you would like to search for:</p>
<form action="gamesByPrice.php" method="post">
Miniumum price: <input type="text" name="minPrice" />
Maximum price: <input type="text" name="maxPrice" />
<input type="submit" value="Search" />
</form>
<?php
// This stores the values that the user entered for min and max price, so that
// a user can search for games by any particular price range.
$_SESSION ['minPrice']="minPrice";
$_SESSION ['maxPrice']="maxPrice";
?>
<?php
// query to retrieve titles of all games from database, with their price and description
$gameTitlesQuery = "SELECT title, price, description FROM CSGames";
$gameTitlesQueryResult = pg_query($dbconn, $gameTitlesQuery) or die("Error in query: $gameTitlesQuery." . pg_last_error($dbconn));
?>
<?php
// print out the data stored in the $gameTitleQueryResult variable in a table
echo '<div id="Games">';
echo '<table id="Games" border="1">';
while ($a=pg_fetch_row($gameTitlesQueryResult)){
echo '<tr>';
for ($i=0; $i<pg_num_fields($gameTitlesQueryResult); $i++){
echo '<td>'.htmlspecialchars($a[$i], ENT_QUOTES).'</ td>';
}
echo "<td><input type='checkbox' name='selectGame' value='{$a['refnumber']}' /></ td>
</ tr>";
}
echo '</table></ div>';
?>
</body>
</html>