array_push覆盖以前创建的对象PHP

时间:2014-10-20 01:28:56

标签: php html

我正在尝试创建一个对象数组,然后我可以显示这些对象,这些对象是在提交表单时创建的。

第一个对象成功添加,但是当我添加另一个对象时,它只是覆盖最后创建的对象。谁能看到我错在哪里?

<?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 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. with 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'] as $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>

1 个答案:

答案 0 :(得分:1)

只需初始化您需要的会话变​​量即可。并且不需要使用变量并将其用作推入内部的计数器。通常只是在会话中推送这些对象。

示例:

<?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 another 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 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. with 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'] as $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>

旁注:始终打开错误报告。