1 Replies - 752 Views - Last Post: 04 August 2010 - 03:35 PM Rate Topic: -----

#1 gymangel812  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 15-February 10

Problems understanding shopping cart scripts

Posted 04 August 2010 - 10:13 AM

I was given the following scripts and I'm having some trouble understanding what's going on. My book is really bad with explaining what is going on.

1. what statement checks whether a selection is already in the cart?
2. what statement checks whether a selection has been added to the cart?
3. Is the shopping cart actually in the add_cart or view_cart script?
4. I get that $_SESSION['cart'][$sid]['quantity'] = $qty; updates the cart, but i'm not sure how...

add_cart.php
<?php # Script 17.8 - add_cart.php
// This page adds selections to the shopping cart.

// Set the page title and include the HTML header:
$page_title = 'Add to Cart';
include ('includes/header.html');

if (isset ($_GET['sid']) && is_numeric($_GET['sid']) ) { // Check for a selection ID.

	$sid = (int) $_GET['sid'];

	// Check if the cart already contains one of these selections;
	// If so, increment the quantity:
	if (isset($_SESSION['cart'][$sid])) {
	
		$_SESSION['cart'][$sid]['quantity']++; // Add another.

		// Display a message.
		if (!empty($_SESSION['cart'])) { 
			echo '<p>Another quantity of the selection has been added to your shopping cart.</p>';
		}
		
	} else { // New product to the cart.

		// Get the selection's price from the database:
		require_once ('mysqli_connect.php');
		$q = "SELECT price FROM selections WHERE selection_id = $sid";
		$r = mysqli_query ($dbc, $q);		

		if ($r) { // Valid selection ID.
		
			// Fetch the information.
			list($price) = mysqli_fetch_array ($r, MYSQLI_NUM);
                 			
			// Add to the cart:
			$_SESSION['cart'][$sid] = array ('quantity' => 1, 'price' => $price);

			// Display a message:
			if (!empty($_SESSION['cart'])) { 
				echo '<p>Selection has been added to your shopping cart.</p>';
			}

		} else { // Not a valid selection ID.
			echo '<div align="center">This page has been accessed in error!</div>';
		}
		
		mysqli_close($dbc);

	} // End of isset($_SESSION['cart'][$sid] conditional.

} else { // No selection ID.
	echo '<div align="center">This page has been accessed in error!</div>';
}

//include ('includes/footer.html');
?>



view_cart.php
<?php # Script 17.9 - view_cart.php
// This page displays the contents of the shopping cart.
// This page also lets the user update the contents of the cart.

// Set the page title and include the HTML header:
$page_title = 'View Your Shopping Cart';
include ('includes/header.html');

// Check if the form has been submitted (to update the cart):
if (isset($_POST['submitted'])) {

	// Change any quantities:
	foreach ($_POST['qty'] as $key => $value) {

		// Must be integers!
		$sid = (int) $key;
		$qty = (int) $value;
		
		if ( $qty == 0 ) { // Delete.
			unset ($_SESSION['cart'][$sid]);
		} elseif ( $qty > 0 ) { // Change quantity.
			$_SESSION['cart'][$sid]['quantity'] = $qty;
		}
		
	} // End of FOREACH.
} // End of SUBMITTED IF.

// Display the cart if it's not empty...
if (!empty($_SESSION['cart'])) {

	// Retrieve all of the information for the prints in the cart:
	require_once ('mysqli_connect.php');

        $query = 'SELECT * FROM selections WHERE selections.selection_id IN (';
	foreach ($_SESSION['cart'] as $key => $value) {
		$query .= $key . ',';
	}
	$query = substr ($query, 0, -1) . ') ORDER BY selections.selection_id ASC';

	$result = mysqli_query ($dbc, $query);
	
	// Create a table and a form.
	echo <<<EOT
	<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
	<tr>
		<td align="left" width="30%"><b>Selection</b></td>
		<td align="left" width="30%"><b>Description</b></td>
		<td align="right" width="10%"><b>Price</b></td>
		<td align="center" width="10%"><b>Qty</b></td>
		<td align="right" width="10%"><b>Total Price</b></td>
	</tr>
	<form action="view_cart.php" method="post">
EOT;

	// Print each item.
	$total = 0; // Total cost of the order.
        if ($result) {
	    while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
		// Calculate the total and sub-totals.
		$subtotal = $_SESSION['cart'][$row['selection_id']]['quantity'] * $row['price'];
		$total += $subtotal;
		
		// Print the row.
		echo "	
		<tr>
		<td align=\"left\">{$row['selection_name']}</td>
		<td align=\"left\">{$row['description']}</td>
		<td align=\"right\">\${$row['price']}</td>
		<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['selection_id']}]\" 
value=\"{$_SESSION['cart'][$row['selection_id']]['quantity']}\" />
                </td>
		<td align=\"right\">$" . number_format ($subtotal, 2) . "</td></tr>";
		

            } // End of the WHILE loop.
	}	
	
	mysqli_close($dbc); // Close the database connection.

	// Print the footer, close the table, and the form.
	echo '<tr>
		<td colspan="4" align="right"><b>Total:</b></td>
		<td align="right">$' . number_format ($total, 2) . '</td>
	</tr>
	</table>
	<div align="center"><input type="submit" name="submit" value="Update My Cart" /></div>
	<input type="hidden" name="submitted" value="TRUE" />
	</form><p align="center">Enter a quantity of 0 to remove an item.
	<br /><br /><a href="checkout.php">Checkout</a></p>';

} else {
	echo '<p>Your cart is currently empty.</p>';
}

include ('./includes/footer.html');
?>



view_selection.php
<?php # Script 17.6 - view_selection.php
// This page displays the details for a particular selection.

session_start();
$row = FALSE; // Assume nothing!

if (isset($_GET['sid']) && is_numeric($_GET['sid']) ) { // Make sure there's a selection ID!

	$sid = (int) $_GET['sid'];
	
	// Get the print info:
	require_once ('mysqli_connect.php'); 
	$q = "SELECT selection_name, description, image_name, price FROM selections WHERE selection_id = $sid";
	$r = mysqli_query ($dbc, $q);
	if (mysqli_num_rows($r) == 1) { // Good to go!
	
		echo "<center><table border=1><tr><td>";
		// Fetch the information:
		$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);
		
		// Start the HTML page:
		$page_title = $row['selection_name'];
		//include ('includes/header.html');
	
		// Display a header:
		echo "<div align=\"center\"><b>{$row['selection_name']}</b><br />";
	
		// Print the size or a default message:
		//echo (is_null($row['size'])) ? '(No size information available)' : $row['size'];

		echo "<br />\${$row['price']}</div><br />";
	
		// Get the image information and display the image:
		if ($image = @getimagesize ("images/{$row[image_name]}")) {
			echo "<div align=\"center\"><img src=\"show_image.php?image_name=" . urlencode($row['image_name']) . "\" $image[3] alt=\"{$row['selection_name']}\" /></div>\n";
		} else {
			echo "<div align=\"center\">No image available.</div>\n"; 
		}
		
		// Add the description or a default message:
		echo '<p align="center">' . ((is_null($row['description'])) ? '(No description available)' : $row['description']) . '</p>';
	
		echo "<p align=\"center\"><a href=\"add_cart.php?sid=$sid\">Add to Cart</a></p>";

		echo "</td></tr></table></center>";	

	} // End of the mysqli_num_rows() IF.
	
	mysqli_close($dbc);

}

if (!$row) { // Show an error message.
	$page_title = 'Error';
	include ('includes/header.html');
	echo '<div align="center">This page has been accessed in error!</div>';
}

// Complete the page:
include ('includes/footer.html');
?>



browse_selections.php
<?php # Script 17.5 - browse_selections.php
// This page displays the available selections (products).

// Set the page title and include the HTML header:
$page_title = 'Browse the Selections';
include ('includes/header.html');

require_once ('mysqli_connect.php');
 
// Default query for this page:
$q = "SELECT selection_id, selection_name, description, image_name, price FROM selections ORDER BY selection_name ASC";

// Are we looking at a particular selection?
if (isset($_GET['aid']) && is_numeric($_GET['aid']) ) {
	$aid = (int) $_GET['aid'];
	if ($aid > 0) { // Overwrite the query:
		echo "AID: $aid";
		$q = "SELECT selection_id, selection_name, description, image_name, price FROM selections WHERE selection_id = $aid ORDER BY selection_name";
	}
}

// Create the table head:
echo '<table border="1" width="90%" cellspacing="3" cellpadding="3" align="center">
	<tr>
		<td align="left" width="20%"><b>Selection Name</b></td>
		<td align="left" width="20%"><b>Description</b></td>
                <td align="middle" width="20%"><b>Image</b></td>
		<td align="right" width="20%"><b>Price</b></td>
	</tr>';

// Display all the selections, linked to URLs:
$r = mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {

	// Display each record:
	echo "\t<tr>
		<td align=\"left\"><a href=\"view_selection.php?sid={$row['selection_id']}\">{$row['selection_name']}</a></td>
		<td align=\"left\">{$row['description']}</td>
		<td align=\"middle\"><img src=images/{$row['image_name']} width=50></td>
		<td align=\"right\">\${$row['price']}</td>
	</tr>\n";

} // End of while loop.

echo '</table>';
mysqli_close($dbc);
//include ('includes/footer.html');
?>



Is This A Good Question/Topic? 0
  • +

Replies To: Problems understanding shopping cart scripts

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,888
  • Joined: 08-August 08

Re: Problems understanding shopping cart scripts

Posted 04 August 2010 - 03:35 PM

  • // Check if the cart already contains one of these selections;
    // If so, increment the quantity:
    if (isset($_SESSION['cart'][$sid])) {

  • if (isset ($_GET['sid']) && is_numeric($_GET['sid']) ) { // Check for a selection ID.
  • The shopping cart is in the session variable.
  • $_SESSION['cart'][$sid]['quantity'] = $qty; updates the session variable with the new quantity: $qty

This post has been edited by CTphpnwb: 04 August 2010 - 03:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1