2

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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I was starting to think that, but because I'm new I was trying to avoid it. I'll see what I can do with adding it as a session variable. Thanks!
@defaultNINJA: Glad to help, good luck! And since you seem to be new here, don't forget to mark your question's answers as correct by clicking the green checkmark. This helps people who later find your question.
Ok will do! Yeah I've been programming in JAVA and C++ for a little while now, just got my first programming gig and they want me to do PHP stuff so now I'm trying to convert my OOP skills from JAVA and C++ to PHP! Fun times :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.