PHP primer for shopping carts

PHP is a hypertext pre-processor. What this means is that PHP processes .php files prior to (pre) sending the HTML output to your visitors web browser.

In other words, the pre-processor is responsible for converting your PHP code into HTML!

Take this example:

In my normal HTML file I have:

<html>
<body>
Test
</body>
</html>


As a PHP alternative I might have:

<?
print “<html>”;
print “<body>”;
print “Test”;
print “</body>”;
print “</html>”;
?>


This will produce the exact same result in the web browser.

Note that PHP code must be enclosed in the <? and ?> tags and that each line must end with a semi-colon (;).

PART 2


A PHP file does NOT need to contain any PHP at all, it can contain only html.

For instance, the file we made above:

<html>
<body>
Test
</body>
</html>

could have been saved as a .php file. The PHP pre-processor will process any bits between the <? and ?> tags and just leave the rest as it is.

This would not normally be how you would use a PHP file, but it raises a very important point: you can mix PHP and plain HTML in a single file (you need to save the file as a .php file).

eg,

<html>
<body>
This part is plain HTML<br>
<? print “This part is PHP<br>”; ?>
Some more HTML<br>
<? print “and more PHP<br>”; ?>
</body>
</html>



This is important because for a shopping cart you would want to use this mixing. Even if a page in your site only has HTML, you would typically still want to use the PHP to move session info across, etc.
 

PART 3


To store a value throughout the users session, you can use cookies, or session variable. As cookies are slightly more complicated, I suggest you use session variable until you’re more comfortable with PHP.

To store and use a session variable, you use it like this:

<?
session_start();

$_SESSION[“FirstName”] = “John”;

?>


on another page, I might use this:

<?
session_start();

if(isset($_SESSION[“FirstName”]))
{
    print “Hello “.$_SESSION[“FirstName”].”<br>”;
}

?>


The first part (page) above simply saves the name John to a session variable called “FirstName”.

The second part (page) displays the name of the session variable, if it is set. If its not set, it skips that part.
You should always check if variables are set before using them to prevent problems.


NOTE: You must use the session_start(); function at the top of your page before ANY other text.
You cannot even have a space or a line before the session_start(); function call, ie,

————————-
<?
session_start();
……
…..



would work, but

———————–

<?
session_start();
………..
……….


would not, assuming the line represents the top of the page and in the second example there is a line between the top of the page and the <?


The session_start() MUST be on each page. It “transfers” the session information from page to page, otherwise your session information would only work on a the page its set, but would be lost to other pages.

PART 4 – Include files

You can use include files in PHP. This means that you store a functions, etc in a separate file on the server, and every page which needs to use this functionality simply includes this page. The PHP pre-processor then outputs the code as if it were part of the file.

For instance if I save a file called “functions.inc” in the root directory (remember your directory structure)

Lets say this file has the following code:

<?

if(!isset($_SESSION[“CartID”]))
{
    $_SESSION[“CartID”] = date(“Ymd”).date(“his”);
}

?>


and in ALL our pages we have:

<?
session_start
include($_SERVER[“DOCUMENT_ROOT”].”/functions.inc”);
?>

HTML GOES HERE




What this does is in each page it includes the functions.inc file. In this file it checks to see if the session variable is set. NOTE the ! in front of the isset. ! means not, so that line reads as

if not set CartID…

In other words, if the CartID is not set, then set it to some unique number. In this case I just set it to the date and time.
The date(“Ymd”) means the date in “YYYYMMDD” format, and the date(“his”) is the time in “HHMMSS” format.

The period (.) between the two means concatenate (add) the strings together, so in this example, it sets the CartID to 20100802183801

This will probably work for a small startup, but for a large site such as amazon, you may have many users coming online at the same time, so only use this as an example. Google for “PHP random numbers” to get more ideas on unique random numbers.
 

PART 5 — Putting it all together

Ok, now we have to put some sense to this all for a shopping cart scenario… NOTE, this is just a basic example and its done off the top of my head, so it may not be 100% workable, but it should give you some ideas.

You have an include file which checks for a valid CartID. If there isn’t one, ie, the user just landed on one of your pages, then you set the CartID.

You have a database table, maybe called Sessions. It has three columns:

CartID | ProductID | Quantity


As a user adds a product to their cart, you save this product id into this table with the cart id. If they add another one, you simply increase the Quantity.

At your checkout, you simply read back the Session Table and extract the pricing info and product detail from the products tables using the product ids from the session table.

That is really the short story, but I think it may give you some ideas.
 

PART 6 — Resources

W3Schools
PHP official site
Google!

Leave a Reply

Your email address will not be published. Required fields are marked *