Jump to content

Help with a php shopping cart assignment (please?)


glcapam03

Recommended Posts

Hi.

 

I've been working on a shopping cart for a college course, and everything is going all right but there is something I'm confused about. What the professor wants is for the total number of items in the shopping cart to be displayed on all pages EXCEPT the actualy cart page. Right now, this feature appears on all pages, including the cart. I don't know how to make it invisible on the cart page  :(.

 

Here is the code from the header.html file, which is where the function to display this feature is located:

 

<?php # Script 5.2 - header.html

/* 
*	This page begins the HTML header for the site.
*	The header also creates the right-hand column.
*	This page calls session_start().
*/

// Need sessions!
session_start();

// Check for a $page_title value:
if (!isset($page_title)) $page_title = 'WC::Widget Central';
?><!DOCTYPE html 
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php echo $page_title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="./includes/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="all">

<div class="box">
	<div class="menu"><a href="index.php">home</a><a href="about.php">about</a><a href="products.php">products</a><a href="contact.php">contact</a></div>
	<div class="header"><img alt="" style="float:right; " src="./images/www.jpg" width="225" height="95" />
	<h1>[<span class="style1">WC</span>] Widget Central</h1>
	<div class="clearfix"></div>
</div>

<div class="newsbar">
	<h1>Browse Widget Categories</h1>
	<div class="p2"><ul>
<?php 
// Get all the categories and
// link them to category.php.

// Define and execute the query:
$q = 'SELECT category_id, category FROM categories ORDER BY category';
$r = mysqli_query($dbc, $q);

// Fetch the results:
while (list($fcid, $fcat) = mysqli_fetch_array($r, MYSQLI_NUM)) {

// Print as a list item.
echo "<li><a href=\"category.php?cid=$fcid\">$fcat</a></li>\n";

} // End of while loop.

	?></ul></div>
	<?php

		if($_SERVER['PHP_SELF']!="../cart.php"){
	            echo "<h1>Cart Contents</h1>";
	            echo "<div class=\"p2\">";
		     $qty=0;
	            $itemCount=$qty;

	            foreach($_SESSION['cart'] as $qty){

	                for($i=0;$i<count(qty);$i++){
	                    $itemCount+=$qty;
	                  }
	            }
	            echo "<a href=\"cart.php\">You have ".$itemCount." items in your cart.</a>";
	            echo "</div>\n";
		 }


	?>

	<h1>Specials</h1>
	<div class="p2"> 
		Coming soon.
	</div>

</div>

<div class="content"> 

 

If anyone can help, I'd greatly appreciate it! Thanks in advance!

Link to comment
Share on other sites

I'm not 100% sure I know what you mean...?

 

To elaborate more on the assignment: the code for displaying the "number of items in cart" feature was not my idea, it was actually given to all the students in class by the professor. The line:

 

if($_SERVER['PHP_SELF']!="../cart.php")

 

wasn't create by me. I don't know what you mean, though, on what I should do with it if it's incorrect.

Link to comment
Share on other sites

'PHP_SELF'

    The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.

 

This $_SERVER variable will not return a relative file path such as ../anything.  Rather, it returns the full path back to the document or web root.  To try it out, just echo it out. 

<?php echo $_SERVER['PHP_SELF']; ?>

Link to comment
Share on other sites

I'll break it down to you then.

<?php
if($_SERVER['PHP_SELF']!="../cart.php"){ //if this returns true (current page IS NOT equal to ../cart.php)
	            echo "<h1>Cart Contents</h1>";
	            echo "<div class=\"p2\">";
		     $qty=0;
	            $itemCount=$qty;

	            foreach($_SESSION['cart'] as $qty){

	                for($i=0;$i<count(qty);$i++){
	                    $itemCount+=$qty;
	                  }
	            }
	            echo "<a href=\"cart.php\">You have ".$itemCount." items in your cart.</a>"; //this will echo.
	            echo "</div>\n";
		 }

 

Look at the comments.  If you fix the if statement to say: only show this info if the page is not equal to the cart script.  You fix your problem.  I'm trying not to give you the direct answer, rather get you to think about the problem and the solution. 

 

Hint (big one).  Those two dots (..) are trying to get the better of you.

Link to comment
Share on other sites

You changed to SCRIPT_NAME, did you echo it out to see what it contained?

SCRIPT_NAME is defined in the CGI 1.1 specification, and is thus a standard. However, not all web servers actually implement it, and thus it isn't necessarily portable. PHP_SELF, on the other hand, is implemented directly by PHP, and as long as you're programming in PHP, will always be present.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.