Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: Array appears to reinitialize
    primarykey
    data
    text
    <p>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?</p> <p>index.php file:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;Custom Forms&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h2&gt;Add Data&lt;/h2&gt; &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt; First Name:&lt;input type="text" size="12" maxlength="12" name="Fname"&gt;&lt;br /&gt; Last Name:&lt;input type="text" size="12" maxlength="36" name="Lname"&gt;&lt;br /&gt; &lt;button type="submit" name="submit" value="client"&gt;Submit&lt;/button&gt; &lt;/form&gt; &lt;?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); } ?&gt; &lt;p&gt;&lt;a href="clientList.php"&gt;go to client list&lt;/a&gt;&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>clintInfo.php file:</p> <pre><code>&lt;?php class clientInfo { private$Fname; private$Lname; public function clientInfo($F, $L) { $this-&gt;Fname = $F; $this-&gt;Lname = $L; } public function __toString() { return $this-&gt;Fname . " " . $this-&gt;Lname; } } ?&gt; </code></pre> <p>clientList.php file:</p> <pre><code>&lt;?php class clientList { public function clientList($array) { foreach($array as $c) { echo $c; } } } ?&gt; </code></pre> <p><strong>EDITED WORKING CODE WITH ANSWER</strong></p> <p>index.php file:</p> <pre><code>&lt;?php include('clientInfo.php'); session_start(); ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;Custom Forms&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h2&gt;Add Data&lt;/h2&gt; &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt; First Name:&lt;input type="text" size="12" maxlength="12" name="Fname"&gt;&lt;br /&gt; Last Name:&lt;input type="text" size="12" maxlength="36" name="Lname"&gt;&lt;br /&gt; &lt;button type="submit" name="submit" value="client"&gt;Submit&lt;/button&gt; &lt;/form&gt; &lt;?php if ($_POST) { $testClient = new clientInfo($_POST["Fname"], $_POST["Lname"]); echo $testClient . " was successfully made. &lt;br/&gt;"; $_SESSION['clients'][] = $testClient; echo end($_SESSION['clients']) . " was added."; } ?&gt; &lt;p&gt;&lt;a href="clientList.php"&gt;go to client list&lt;/a&gt;&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>clientList.php file:</p> <pre><code>&lt;?php include('clientInfo.php'); session_start(); ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Accessing session variables &lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt; Content Page &lt;/h1&gt; &lt;?php for ($i = 0; $i &lt; sizeof($_SESSION['clients']); $i++) { echo $_SESSION['clients'][$i] . " was added. &lt;br/&gt;"; } ?&gt; &lt;p&gt;&lt;a href="index.php"&gt;return to add data&lt;/a&gt;&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload