Information and support for products of WithinWeb.com

 header image 1

A Shopping Cart using PHP Sessions

October 25th, 2014 · No Comments

This posts illustrates a relatively simple shopping cart using PHP sessions to store the cart details, quantities and total cart amount. The cart has an ‘add to cart’, ‘remove from cart’ and displays the total value of the cart.

This post is taken from part of the book PHP Tutorials: Programming with PHP and MySQL which is available as a paper back printed version or as a downloadable Kindle version. Click here for paper back version.

The following listing is the complete cart:

<?php session_start();
#cart.php - A simple shopping cart with add, and remove links
 //---------------------------
 //initialise sessions

//Define the products and cost
$products = array("product A", "product B", "product C");
$amounts = array("19.99", "10.99", "2.99");

 if ( !isset($_SESSION["total"]) ) {

   $_SESSION["total"] = 0;
   for ($i=0; $i< count($products); $i++) {
    $_SESSION["qty"][$i] = 0;
   $_SESSION["amounts"][$i] = 0;
  }
 }

 //---------------------------
 //Reset
 if ( isset($_GET['reset']) )
 {
 if ($_GET["reset"] == 'true')
   {
   unset($_SESSION["qty"]); //The quantity for each product
   unset($_SESSION["amounts"]); //The amount from each product
   unset($_SESSION["total"]); //The total cost
   unset($_SESSION["cart"]); //Which item has been chosen
   }
 }

 //---------------------------
 //Add
 if ( isset($_GET["add"]) )
   {
   $i = $_GET["add"];
   $qty = $_SESSION["qty"][$i] + 1;
   $_SESSION["amounts"][$i] = $amounts[$i] * $qty;
   $_SESSION["cart"][$i] = $i;
   $_SESSION["qty"][$i] = $qty;
 }

  //---------------------------
  //Delete
  if ( isset($_GET["delete"]) )
   {
   $i = $_GET["delete"];
   $qty = $_SESSION["qty"][$i];
   $qty--;
   $_SESSION["qty"][$i] = $qty;
   //remove item if quantity is zero
   if ($qty == 0) {
    $_SESSION["amounts"][$i] = 0;
    unset($_SESSION["cart"][$i]);
  }
 else
  {
   $_SESSION["amounts"][$i] = $amounts[$i] * $qty;
  }
 }
 ?>
 <h2>List of All Products</h2>
 <table>
   <tr>
   <th>Product</th>
   <th width="10px">&nbsp;</th>
   <th>Amount</th>
   <th width="10px">&nbsp;</th>
   <th>Action</th>
   </tr>
 <?php
 for ($i=0; $i< count($products); $i++) {
   ?>
   <tr>
   <td><?php echo($products[$i]); ?></td>
   <td width="10px">&nbsp;</td>
   <td><?php echo($amounts[$i]); ?></td>
   <td width="10px">&nbsp;</td>
   <td><a href="?add=<?php echo($i); ?>">Add to cart</a></td>
   </tr>
   <?php
 }
 ?>
 <tr>
 <td colspan="5"></td>
 </tr>
 <tr>
 <td colspan="5"><a href="?reset=true">Reset Cart</a></td>
 </tr>
 </table>
 <?php
 if ( isset($_SESSION["cart"]) ) {
 ?>
 <br/><br/><br/>
 <h2>Cart</h2>
 <table>
 <tr>
 <th>Product</th>
 <th width="10px">&nbsp;</th>
 <th>Qty</th>
 <th width="10px">&nbsp;</th>
 <th>Amount</th>
 <th width="10px">&nbsp;</th>
 <th>Action</th>
 </tr>
 <?php
 $total = 0;
 foreach ( $_SESSION["cart"] as $i ) {
 ?>
 <tr>
 <td><?php echo( $products[$_SESSION["cart"][$i]] ); ?></td>
 <td width="10px">&nbsp;</td>
 <td><?php echo( $_SESSION["qty"][$i] ); ?></td>
 <td width="10px">&nbsp;</td>
 <td><?php echo( $_SESSION["amounts"][$i] ); ?></td>
 <td width="10px">&nbsp;</td>
 <td><a href="?delete=<?php echo($i); ?>">Delete from cart</a></td>
 </tr>
 <?php
 $total = $total + $_SESSION["amounts"][$i];
 }
 $_SESSION["total"] = $total;
 ?>
 <tr>
 <td colspan="7">Total : <?php echo($total); ?></td>
 </tr>
 </table>
 <?php
 }
 ?>

The cart example uses the following sessions to maintain the state of the cart:

$_SESSION[“qty”][i] Stores the quantity for each product
$_SESSION[“amounts”][i] Stores the price from each product
$_SESSION[“cart”][i] Identifies which items have been added to the cart
$_SESSION[“total”] Stores the total cost

The sessions are actually arrays so in the case of:

$_SESSION[“qty”][i]

is the quantity for the element with number i.

Description of the shopping cart code

We start by defining PHP to use sessions by:

session_start();

This has to be at the very top of the PHP page.

Next we set up our products and populate our sessions. In this example we are using a fixed array of product descriptions and amounts. You may want to do this in your application or you could read in the data into the $product and $amounts array from a database.

//---------------------------
 //initialise sessions

 //Define the products and cost
 $products = array("product A", "product B", "product C");
 $amounts = array("19.99", "10.99", "2.99");

 if ( !isset($_SESSION["total"]) ) {

  $_SESSION["total"] = 0;

  for ($i=0; $i< count($products); $i++) {
   $_SESSION["qty"][$i] = 0;
   $_SESSION["amounts"][$i] = 0;
 }
}

The following code will reset and clear the sessions when the Reset Cart link is selected.

//---------------------------
  //Reset
  if ( isset($_GET['reset']) )
   {
    if ($_GET["reset"] == 'true')
    {
     unset($_SESSION["qty"]); //The quantity for each product
     unset($_SESSION["amounts"]); //The amount from each product
     unset($_SESSION["total"]); //The total cost
     unset($_SESSION["cart"]); //Which item has been chosen
   }
}

The following code adds an item to the sessions when the ‘Add to Cart’ link is clicked:

//---------------------------
//Add
if ( isset($_GET["add"]) )
{
$i = $_GET["add"];

$qty = $_SESSION["qty"][$i] + 1;

$_SESSION["amounts"][$i] = $amounts[$i] * $qty;
$_SESSION["cart"][$i] = $i;
$_SESSION["qty"][$i] = $qty;
}

and the following deletes an item from the cart when the ‘Delete from Cart’ link is clicked:

 //---------------------------
 //Delete
 if ( isset($_GET["delete"]) )
 {
   $i = $_GET["delete"];
   $qty = $_SESSION["qty"][$i];
   $qty--;
   $_SESSION["qty"][$i] = $qty;

 //remove item if quantity is zero
 if ($qty == 0) {
   $_SESSION["amounts"][$i] = 0;
   unset($_SESSION["cart"][$i]);
 }
 else
 {
   $_SESSION["amounts"][$i] = $amounts[$i] * $qty;
 }
 }

The rest of the code is the visual display using a table and various loops to show the product lists and the cart details together with the links.

This post is taken from part of the book PHP Tutorials: Programming with PHP and MySQL which is available as a paper back printed version or as a downloadable Kindle version. Click here for paper back version.

→ No CommentsTags: eBooks · General PHP

Amazon Reviews for your Books

October 11th, 2014 · No Comments

The Amazon reviews can have a huge effect on your book sales.  You need to be popular to become popular which can be tricky when you first make a book available.

You need to drive as many reviews to your book as possible so try to get your friends and family to do a review each.  A minimum of 4 or 5 is really good but up to 10 is what you need.

The number of reviews a book has is a good indicator to a possible purchaser as to how serious they should consider it.

As a self publisher you have to take responsibility to make sure this section is populated which means promoting it in wherever you can.  Unfortunately, a genuine review is usually only made when the customer either really loves it or really hates it.

Once you have got going with reviews you should be able to rely on your normal sales to get further reviews.

→ No CommentsTags: eBooks

Book Front Cover Layout and Design

October 2nd, 2014 · No Comments

Creating a stand out front cover is a must to get your book noticed on web sites and in book shop shelves – if you are lucky to get your book that far.

The front cover needs to be smart, professional and not too complicated. Your potential purchaser will see it as an indication of the quality of what is inside if the outside looks good.

As you will probably be selling an eBook version, you need to have a thumbnail image which can be placed on web sites and will be used as the image on sites like Amazon. This has particular display issues as you must use fonts, images and graphics that are clear to read even when the cover is reduced to a thumbnail. Make sure that there is sufficient contrast between the background colours and the text. Don’t forget the back cover where you can add some text to advertise your book.

Web links:

An article from the Book Designer:
http://www.thebookdesigner.com/2012/01/book-cover-design-and-the-problem-of-symbolism/

a simple On line cover designer:
https://www.createspace.com/Tools/CoverCreator.jsp

A free free on line graphics program which has many of the functionality of PhotoShop:
http://pixlr.com/

→ No CommentsTags: eBooks

Paper Back PHP Book Now Available

September 7th, 2014 · No Comments

The paperback version of ‘PHP Tutorials: Programming with MySQL and PHP’ has been published and is available from the Amazon book store.

Do a search for the ISBN number 0992869730 or the above title should get you there or click on the link:

PHP Tutorials Paper Back Book

PHP Tutorials Cover Image

→ No CommentsTags: eBooks

Word Large Documents with different header / footer

July 2nd, 2014 · No Comments

It is not immediately obvious how to do different header / footer for different Chapters in a long document.

The main control of this is by section breaks.  For each section that requires a different header / footer you use a new section break.

In word 2013, go to “PAGE LAYOUT” and select “Breaks”.  In the drop down list select Section Breaks -> Next Page
This inserts a section break and starts a new section on the next page.  Other versions of word will be similar.

Create your header in the normal way by double clicking on the head of the Word document.

Once you have your section breaks set up, the next important step is to control the linking between headers in different sections.  To do this you go into editing of the header by double clicking on it.  In the menu bar you should see “Link to Previous”.  If you are in the second section of a document, you click on this to unlink the header of the second section.  You can then edit the header in the second section and that will allow you to have a different header in that second section.

You can now do other clever things such as different header / footer for odd / even pages and that is controlled from the header menu bar by ticking the appropriate tick boxes.

→ No CommentsTags: eBooks

How to remove unwanted styles from Microsoft Word

May 26th, 2014 · No Comments

I use Word to write and format my eBook and printed version of the book because it is easy to use and is a common platform with plenty of online help.  However, it has take me a very long time to become comfortable with styles.

The main issue I have had is how to removed unwanted styles.  I found that my styles list was made up of styles with similar names.  So I had one style called CODEBLOCK and others called CODEBLOCK1 , CODEBLOCK2, CODEBLOCK3 and so on.  In some documents this amounted to thousands of unwanted duplicate styles which slowed down loading the file.

I don’t know how these were created, it may have been something to do with Master Pages or possible to do with copying text from one document to another.  It made the document larger than necessary and when I did saved the file as HTML I ended up with a massive list of css styles in the HTML document.

I tried a number of methods, one of which was to run a Word make to delete unused styles.  This particular macro seemed to work on small documents, but caused Word to hang up when I tried it on large documents.

Eventually I found a way of getting rid of them but event this method can take a a few minutes per file.

First you need to show the styles window which will look something like:

styles

Click on Manage Styles icon at bottom of display.
Click on Import/Export…

This display allows you to select multiple styles in the left hand column and then delete the multiple styles.

Use the mouse button with the Ctrl key to select the multiple styles and then delete them.  Note that you cannot delete built in styles and also this displays allows you to move styles from one document to another.

→ No CommentsTags: eBooks

Next step for PHP Tutorials eBook

April 3rd, 2014 · No Comments

The eBooks is on sale on the Amazon web site and hence is available for Kindle devices.  It is also available as a PDF download which may be purchased from my web site http://www.paulvgibbs.com.

The next step is to make the eBook more widely available through other stores and other formats and to create a forum where purchasers of the book can more easily ask questions and discuss PHP coding issues.

In the mean time you are quite welcome to ask questions via my email.

→ No CommentsTags: eBooks

New web site for eBook paulvgibbs.com

March 19th, 2014 · No Comments

The new web site http://www.paulvgibbs.com is now available and provides information of eBooks and other projects.

→ No CommentsTags: eBooks

The eBooks PHP-Tutorials has been updated

March 19th, 2014 · No Comments

The content structure has been re-arranged slightly to improve navigation and a new section has been added on an introduction to WordPress plugins.  This section goes through developing a number of admin pages that might be required for a plugin.   All the code is provided as with other code examples and I hope to add more to this section shortly.

It is now possible to purchase the eBook as a PDF version from the web site http://www.paulvgibbs.com

→ No CommentsTags: eBooks

Updates to eBook PHP Tutorials

January 8th, 2014 · No Comments

The eBook PHP Tutorials – Programming with mySQL and PHP, has been updated.

The changes cover revised section on PHP security and the reformatting of the chapters.

The latest version is available on amazon at:

http://www.amazon.com/dp/B00CBLZ1U0

→ No CommentsTags: eBooks