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

07 - Array of Class Objects and a Session Variable Using PHP

Blank Last Name - Table not updated.

First Name:
Last Name:
Date of Birth yyyy-mm-dd:

Record count = 0

Record #

First Name

Last Name

Date of Birth

Homework: Upgrade this example to manage a phone number in addition to the name and date fields.


<!-- Here is the page source code for 07array.php -->

<?php
  include "code.php";    // Contains the function used to display
                         // the source code of web pages.

  session_start();
  header("Cache-control: private"); //IE 6 Fix
  session_register('myArray');

  $myArray = $_SESSION['myArray'];

  $fname = trim($_POST['textfield1']);    // First name
  $lname = trim($_POST['textfield2']);    // Last name
  $dob   = trim($_POST['textfield3']);    // Date Of Birth
  
  class Person
  {
    // CLASS DATA IS SPECIFIED HERE
    var $fname;    // First name
    var $lname;    // Last name
    var $dob;      // Date Of Birth

    // INPUT - SET THE FIRST NAME
    function setFirstName($newFirstName)
    { 
      $this->fname = trim($newFirstName);
    }

    // INPUT - SET THE LAST NAME
    function setLastName($newLastName)
    {
      $this->lname = trim($newLastName);
    }

    // INPUT - SET THE DOB
    function setDOB($newDOB)
    {
      $this->dob = trim($newDOB);
    }

    // OUTPUT FROM THE CLASS
    function displayData()
    {
      return "<td>$this->fname</td><td>$this->lname</td><td>$this->dob</td>";
    }
  }
  
  if ($lname == "")
  {
    $message =  "<p><strong>Blank Last Name - Table not updated.</strong></p>\n";
  }
  else
  {
    // CREATE A NEW Person OBJECT AND 
    $person = new Person();
    $person->setFirstName($fname);
    $person->setLastName($lname);
    $person->setDOB($dob);
    $message =  "<p><strong>Created a new Person.</strong></p>\n";
	
    // ADD THE NEW OBJECT TO THE NEXT FREE ARRAY LOCATION
    $myArray[] = $person;

    $_SESSION['myArray'] = $myArray;
  }
	
  $count = count($myArray);  // The number of items in the array
?>



<html>
<head>
<title>Array of Class Objects and a Session Variable 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>07 - Array of Class Objects and a Session Variable Using PHP</h1>

      <?php echo $message; ?>
      
<form name="form1" method="post" action="07array.php">
        <table border="1" cellspacing="0" cellpadding="4">
    <tr> 
      <td><div align="right">First&nbsp;Name:</div></td>
      <td> <input type="text" name="textfield1"> </td>
    </tr>
    <tr> 
      <td><div align="right">Last&nbsp;Name:</div></td>
      <td> <input type="text" name="textfield2"> </td>
    </tr>
    <tr> 
      <td><div align="right">Date&nbsp;of&nbsp;Birth yyyy-mm-dd:</div></td>
      <td> <input type="text" name="textfield3"> </td>
    </tr>
    <tr> 
      <td colspan="2"><div align="right"> 
        <input type="submit" name="Submit" value="Submit">
        </div>
      </td>
    </tr>
  </table>
</form>

<?php
  echo "<p>Record count = $count</p>\n";

  echo "<table border=\"1\" cellpadding=\"4\" border=\"1\" cellspacing=\"0\">\n";
    
  echo "<tr><td><h3>Record #</h3></td><td><h3>First Name</h3></td><td><h3>Last Name</h3></td><td><h3>Date of Birth</h3></td></tr>\n";
	
  // foreach iterates through every array element even if they are numbered non-sequentially
  // on each iteration, $key is set to the array key
  // on each iteration, $value is set to the item stored at the key position
  if (isset($myArray) )
  {
    foreach($myArray as $key => $value)
    {
      // Display a table row
      echo "<tr><td align=\"center\">$key</td>".$value->displayData()."</tr>\n";
    }
  }  
	
  echo "</table>\n";
?>

      <p><strong><font color="#FF0000">Homework:</font></strong> Upgrade 
      this example to manage a phone number in addition to the name
      and date fields.</p>

      <?php dump_page("07array.php"); ?>
</td>
</tr>
</table>

</body>
</html>