At the moment, home.php is displaying a list of items in an html table, with their name, price and description, as well as a checkbox, which the user can check. What I want to be able to do, is that after the user has checked a few boxes, they click an 'Add to basket' button, which will add the items that have associted check boxes which have been checked, to the shopping basket, but I'm not too sure how to go about doing that.
I'd be grateful if someone could point me in the right direction?
My home.php page looks like this:
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>
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'];
}
?>
<html>
<head>
<title>Shopping Basket</title>
</head>
<body>
<h1>Shopping Basket</h1>
<?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
// 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(',',$cart);
$contents = array();
foreach($items as $item){
$contents[$item] = (isset($contents[$item])) ? $contents[$item] +1:1;
}
}
?>
<?php
// Display the details for each individual product the user has selected
$totalGamesSelected = 0;
$output[] = '<table>';
print_r($contents);
if(count($contents)){
foreach($contents as $id => $quantity){
$selectedGameInfoQuery = 'SELECT * FROM CSGames WHERE id = '.$id;
$selectedGameInfoQueryResult = $db->query($selectedGameInfoQuery);
$row = $selectedGameInfoQueryResult->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="shoppingBasket.php?action=delete&id='.$id.'"class="r">Remove</a></td>';
$output[] = '<td>'.$gameTitle.'</td>';
$output[] = '<td>£'.$price.'</td>';
$output[] = '<td><input type="text" name="quantity'.$id.'" value="'.$quantity.'"size="3" maxlength="3" /></td>';
$output[] = '<td>£'.($price * $quantity).'</td>';
$total += $price * $quantity;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: £'.$total.'</p>';
} echo("You have not added anything to your shopping basket!");
// $output[] = '</table>';
// $output[] = '<p>Grand total: £'.$total.'</p>';