INTRODUCTION

PHP Web Home
Server Setup
nav.php
code.php
Download Tutorial

LESSONS

01 Hello World!
02 Text I/O
03 Arithmetic
04 Date and Time
05 Image Creation
06 Pie Chart
07 Array and Records
08 Delete a Record
09 Shopping
10 Shop Checkout
11 MM Bingo

MORE PHP

PHP Console Apps
PHP and MySQL

09 - Shopping Page Using PHP

Item

Description

Price

Quantity

Cost

Kitten

Soft furry kitten for a small child

£ 0.2

£ 0.00

Teddy

Cuddly teddy bear

£ 4.99

£ 0.00

Total:

£ 0.00

Homework: Add two more products to the example shop.


<!-- Here is the page source code for 09shop.php -->

<?php
  include "code.php";    // Contains the function used to display
                         // the source code of web pages.
  $kittenPrice = 0.20;
  $teddyPrice  = 4.99;
  
  // SERIOUS VALIDATION CODE FOLLOWS
  // TEST IF ANY DATA AT ALL HAS BEEN POSTED - SET COUNTS TO ZERO OTHERWISE
  if (isset($_POST))
  {
    // TEST IF KITTENS HAVE BEEN POSTED - SET COUNT TO ZERO OTHERWISE
    if (isset($_POST['kitten']))
    {
      $kitten = $_POST['kitten'];
      
      // CHECK FOR NUMERIC INPUT - ERROR MESSAGE OTHERWISE
      if (is_numeric($kitten))
      {
        // CHECK FOR WHOLE NUMBER INPUT -  - ERROR MESSAGE OTHERWISE
	if (is_int(- - $kitten))  // - - forces text to type numeric conversion
        {
          // CHECK FOR POSITIVE NUMBER INPUT
	  if ($kitten >= 0)
          {
            $kittenError = "";
	  }
	  else
	  {
            $kitten = 0;
            $kittenError = "Please enter a positive number of kittens.";
	  }
	}
	else
	{
          $kitten = 0;
  	  $kittenError = "Please enter a whole number of kittens.";
	}
      }
      else
      {
        $kitten = 0;
	$kittenError = "Please enter a number of kittens.";
      }
    }
    else
    {
      $kitten = 0;
    }

    // TEST IF TEDDIES HAVE BEEN POSTED - SET COUNT TO ZERO OTHERWISE
    if (isset($_POST['teddy']))
    {
      $teddy = $_POST['teddy'];
      
      // CHECK FOR NUMERIC INPUT - ERROR MESSAGE OTHERWISE
      if (is_numeric($teddy))
      {
        // CHECK FOR WHOLE NUMBER INPUT -  - ERROR MESSAGE OTHERWISE
        if (is_int(- - $teddy))  // - - forces text to type numeric conversion
        {
          // CHECK FOR POSITIVE NUMBER INPUT
          if ($teddy >= 0)
          {
            $teddyError = "";
	  }
	  else
	  {
            $teddy = 0;
            $teddyError = "Please enter a positive number of teddies.";
	  }
	}
	else
	{
          $teddy = 0;
  	  $teddyError = "Please enter a whole number of teddies.";
	}
      }
      else
      {
        $teddy = 0;
	$teddyError = "Please enter a number of teddies.";
      }
    }
    else
    {
      $teddy = 0;
    }
  }
  else
  {
    $kitten = 0;
    $teddy = 0;
  }
?>

<html>
<head>
<title>Shopping Page Using PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<table border="1" cellpadding="4" cellspacing="0">
  <tr valign="top">
    <td> 
      <?php include "nav.php"; ?>
    </td>
    <td> 
      <h1>09 - Shopping Page Using PHP</h1>
      <form name="form1" method="post" action="09shop.php">
        <table border="1" cellspacing="0" cellpadding="4">
          <tr> 
            <td><h3>Item</h3></td>
            <td><h3>Description</h3></td>
            <td><h3>Price</h3></td>
            <td><h3>Quantity</h3></td>
            <td><h3>Cost</h3></td>
          </tr>
          <tr valign="top"> 
            <td><p>Kitten</p></td>
            <td><p>Soft furry kitten for a small child</p>
                <p><img src="09-cat-glove.jpg" width="87" height="133"></p>
	    </td>
            <td><div align="right">
                <p><?php echo "£ " . $kittenPrice; ?></p>
                </div>
	    </td>
            <td><p>
                <input name="kitten" type="text" id="kitten" value="<?php echo $kitten; ?>">
                </p>
                <p><strong><?php echo $kittenError; ?></strong></p>
	    </td>
            <td><div align="right">
                 <p><?php echo "£ " . sprintf("%01.2f", $kittenPrice * $kitten); ?></p>
                 </div>
	    </td>
          </tr>
          <tr valign="top"> 
            <td><p>Teddy</p></td>
            <td><p>Cuddly teddy bear</p>
                <p><img src="09-teddy.jpg" width="116" height="133"></p>
	    </td>
            <td><div align="right">
                 <p><?php echo "£ " . $teddyPrice; ?></p>
                 </div>
	    </td>
            <td> <p>
                 <input name="teddy" type="text" id="teddy" value="<?php echo $teddy; ?>">
                 </p>
                 <p><strong><?php echo $teddyError; ?></strong></p>
	    </td>
            <td><div align="right">
                 <p><?php echo "£ " . sprintf("%01.2f", $teddyPrice * $teddy); ?></p>
                 </div>
	    </td>
          </tr>
          <tr>
            <td colspan="4"><div align="right"><p><strong>Total: </strong></p></div></td>
            <td><div align="right"><p><?php echo "£ " . sprintf("%01.2f", $teddyPrice * $teddy + $kittenPrice * $kitten); ?></p></div></td>
          </tr>
          <tr> 
            <td colspan="5">
	      <div align="right"> 
              <input type="submit" name="Submit" value="Update">
              </div>
	    </td>
          </tr>
        </table>
      </form>
      <p><strong><font color="#FF0000">Homework:</font></strong> Add two more 
        products to the example shop.</p>
      <?php dump_page("09shop.php"); ?>
</td>
</tr>
</table>

</body>
</html>