PHP: Array appears to reinitialize

I'm trying to make a basic html form that passes information to a php object then adds said object to an array. It works to the point of passing the information from the form, to the object, and adding it to the array and displaying said object. However, when I try to add a second object to the array it only seems to replace the array with a new single element array rather then adding to it. Here is my code... any ideas?

index.php file:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Custom Forms</title>
    </head>
    <body>
        <h2>Add Data</h2>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
            Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
            <button type="submit" name="submit" value="client">Submit</button>
        </form>
        <?php
        include_once 'clientInfo.php';
        include_once 'clientList.php';

        if ($_POST) {
            $clientArray[] = new clientInfo($_POST["Fname"], $_POST["Lname"]);
        }

        if (!empty($clientArray)) {
            $clientList = new clientList($clientArray);
        }
        ?>
        <p><a href="clientList.php">go to client list</a></p>
    </body>
</html>

clintInfo.php file:

<?php
class clientInfo {

    private$Fname;
    private$Lname;

    public function clientInfo($F, $L) {
        $this->Fname = $F;
        $this->Lname = $L;
    }

    public function __toString() {
        return $this->Fname . " " . $this->Lname;
    }
}
?>

clientList.php file:

<?php
class clientList {
    public function clientList($array) {
        foreach($array as $c) {
            echo $c;
        }
    }
}
?>

EDITED WORKING CODE WITH ANSWER

index.php file:

<?php
include('clientInfo.php');
session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Custom Forms</title>
    </head>
    <body>
        <h2>Add Data</h2>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
            Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
            <button type="submit" name="submit" value="client">Submit</button>
        </form>
        <?php
        if ($_POST) {
            $testClient = new clientInfo($_POST["Fname"], $_POST["Lname"]);

            echo $testClient . " was successfully made. <br/>";

            $_SESSION['clients'][] = $testClient;

            echo end($_SESSION['clients']) . " was added.";
        }
        ?>
        <p><a href="clientList.php">go to client list</a></p>
    </body>
</html>

clientList.php file:

<?php
include('clientInfo.php');
session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>
            Accessing session variables
        </title>
    </head>
    <body>
        <h1>
            Content Page
        </h1>
        <?php
        for ($i = 0; $i < sizeof($_SESSION['clients']); $i++) {
            echo $_SESSION['clients'][$i] . " was added. <br/>";
        }
        ?>
        <p><a href="index.php">return to add data</a></p>
    </body>
</html>

The object file clientInfo.php stayed the same. The objects needed to be stored in a multidimensional $_SESSION array and recalled with a for loop, a foreach loop would not work, unless some one else knows a way to make a foreach loop work, stick to a for. On a side note the $testClient variable could be skipped and just created and placed with in the $_SESSION at the same time, however doing it with the temp variable made it easier to trouble shoot and see how to make it work. Just thought I'd post the working code with the answer supplied by Josh!

Answers


You need to add persistance to your array object.

See PHP's $_SESSION.

If you don't store it to memory or disk in between requests, it cannot possibly exist on successive loads. There is a nice tutorial in that link that should get you up and running.

Alternatively, you can store things in a database, for larger needs.


Need Your Help

Can a Windows Phone 7 device be registered to more than one developer (Live ID) at the same time

windows-phone-7

I will be developing apps using both my own Live ID and one specific to my employer. Can I register the same device to both IDs at the same time?

Scala Default Values: How do I get a default value from type Any in Scala while creating a LinkedList?

scala linked-list manifest default-value any

I am creating a Doublelylinked list in Scala and I have having a problem creating the sentinel node. For my sentinel node's data, I need the default value of the type I am using (EX: I need 0 for I...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.