html - array_push overwriting previously created object PHP -
html - array_push overwriting previously created object PHP -
i trying create array of objects can display, objects beingness created when form has been submitted.
the first object gets added, when add together object, overwrites lastly created object. can see going wrong?
<?php require_once $_server['document_root'].'/includes/classes/goal.php'; ?> <?php require_once $_server['document_root'].'/global/head.php'; ?> <?php require_once $_server['document_root'].'/config/init.php'; ?> <?php $input['title'] = ""; $input['deadline'] = ""; $input['description'] = ""; if(!isset($_session['goals'])) {$_session['goals'] = array();} if (isset($_post['submit'])) { $_session['goalcount'] ++; $input['title'] = htmlentities($_post ['title'],ent_quotes); $input['deadline'] = htmlentities($_post ['deadline'],ent_quotes); $input['description'] = htmlentities($_post ['description'],ent_quotes); convertdate($input['deadline']); ${'goal'. $_session['goalcount']} = new goal($input['title'], $input['description'], $_session['username'], $input['deadline']); array_push($_session['goals'], ${'goal'. $_session['goalcount']}); ?> <div class="top"> <p>h</p> </div> <div class="container"> <div class="sixteen columns topbar"> <?php require $_server['document_root'].'/global/header.php'; ?><!-- content begins --> <div class="content"> <h1> oo test page - batch add together goals</h1><hr/> <div class="six columns"> <form action="" method="post"> <fieldset> <div> <h4>title</h4> <span id='title-result'></span> <label for="title"></label><br /> <input type="text" id="title" name="title" placeholder="e.g. graduate" value="" required aria-required="true"> </div> <div> <h4>description</h4> <span id='description-result'></span> <label for="description"></label> <textarea type="description" id="description" placeholder="e.g. first-class honours" name="description" value="" required aria-required="true"></textarea> </div> <div> <h4>deadline</h4> <span id='description-result'></span> <label for="deadline"></label> <input rows="2"type="date" id="datepick" placeholder="" name="deadline" value="" required aria-required="true"/> </div> <div class="submit"> <input type="submit" name="submit" value="add"> </div> </fieldset> </form></div> <div class="ten columns"> <?php foreach ($_session['goals'] $goal) { echo '<div class="goal"><h4>'. $goal->title .'</h4>'. $goal->desc .'</h4><p>'. $goalcount .'</p></div>'; } echo goal::$counter; var_dump($_session['goals']) ?> </div> </div> <!-- content ends --> <?php require $_server['document_root'].'/global/footer.php'; ?> </div> </div> </body> </html>
simply initialize session variables need. , no need utilize variable variables , using counter force inside. force object within session.
example:
<?php require_once $_server['document_root'].'/includes/classes/goal.php'; ?> <?php require_once $_server['document_root'].'/global/head.php'; ?> <?php require_once $_server['document_root'].'/config/init.php'; ?> <?php if(!isset($_session['goals'], $_session['goalcount'])) { $_session['goals'] = array(); $_session['goalcount'] = 0; } if (isset($_post['submit'])) { $_session['goalcount'] += 1; $input['title'] = htmlentities($_post ['title'],ent_quotes); $input['deadline'] = htmlentities($_post ['deadline'],ent_quotes); $input['description'] = htmlentities($_post['description'],ent_quotes); convertdate($input['deadline']); $goal = new goal($input['title'], $input['description'], $_session['username'], $input['deadline']); $_session['goals'][] = $goal; // ^ add together dimension } // missing closing curly brace ?> <div class="top"> <p>h</p> </div> <div class="container"> <div class="sixteen columns topbar"> <?php require $_server['document_root'].'/global/header.php'; ?><!-- content begins --> <div class="content"> <h1> oo test page - batch add together goals</h1><hr/> <div class="six columns"> <form action="" method="post"> <fieldset> <div> <h4>title</h4> <span id='title-result'></span> <label for="title"></label><br /> <input type="text" id="title" name="title" placeholder="e.g. graduate" value="" required aria-required="true"> </div> <div> <h4>description</h4> <span id='description-result'></span> <label for="description"></label> <textarea type="description" id="description" placeholder="e.g. first-class honours" name="description" value="" required aria-required="true"></textarea> </div> <div> <h4>deadline</h4> <span id='description-result'></span> <label for="deadline"></label> <input rows="2"type="date" id="datepick" placeholder="" name="deadline" value="" required aria-required="true"/> </div> <div class="submit"> <input type="submit" name="submit" value="add"> </div> </fieldset> </form> </div> </div> <div class="ten columns"> <?php foreach ($_session['goals'] $goal) { echo '<div class="goal"><h4>'. $goal->title .'</h4>'. $goal->desc .'</h4><p>'. $goalcount .'</p></div>'; } ?> </div> <!-- content ends --> <?php require $_server['document_root'].'/global/footer.php'; ?> </div> </div>
sidenote: turn on error reporting.
php html
Comments
Post a Comment