0

我有一个页面,其中有一个用户可以添加对象的表单。逐个。当用户发送表单时,会对当前页面进行 POST 以$objects使用新对象进行更新。这是代码。

<?php
    include ('header.php');
    include ('dbc.php');
    include ('object.php');

    $objects = array();

    if(isset($_POST['flag']))
    {
        $objects = unserialize($_POST['objects']);

        $name = mysql_fix_string($_POST['name']);
        $quantity = mysql_fix_string($_POST['quantity']);
        $observation = mysql_fix_string($_POST['observation']);

        $object = new Object();

        $object->name = $name;
        $object->quantity = $quantity;
        $object->observation = $observation;

        $objects[] = $object;
    }

    var_dump($objects);
?>
<body>
        <div class="leftContainer">
            <div class="upperContainer">
                <div class="ContainerTitle">
                    Imagem do produto:
                </div>
                <div style="height: 200px;" class="upperContainerImageDiv"></div>
            </div>
            <div class="bottomContainer">
                <div class="bottomContainerRequestsDiv">
                    <un>
                        <li class="listItem">
                            <?php
                                foreach ($object as $product)
                                {
                                    $counter = 1;
                                    ?>
                                        <div class="listItemInnerDiv">
          //error here                      <span class="listItemInnerDivText"><?php echo $counter ." ". $product->name ?></span>
                                        </div>
                                    <?php
                                    $counter + 1;
                                }
                            ?>
                        </li>
                    </un>
                </div>
            </div>
        </div>
        <div class="rightContainer">
            <form action="" method="POST">
                <table style="margin-left: auto; margin-right: auto;">
                    <thead>
                        <div class="ContainerTitle">
                            Adicionar produto:
                        </div>
                    </thead>
                    <tbody>
                        <tr>
                            <td><span class="tdTitle">Produto:</span></td>
                            <td>
                                <select class="input" name="name" onchange="ImageUpdater(this.value);">
                                    <option>Selecione</option>
                                    <?php
                                        $query = "SELECT g_id, name FROM `group`";

                                        $result = mysql_query($query);

                                        while($row = mysql_fetch_array($result))
                                        {
                                            ?><optgroup label="<?php echo $row[1] ?>"><?php

                                            $query = "SELECT name FROM `object` WHERE g_id='$row[0]'";

                                            $secondaryResult = mysql_query($query);

                                            while($secondaryRow = mysql_fetch_array($secondaryResult))
                                            {
                                                ?><option><?php echo $secondaryRow[0] ?></option><?php
                                            }
                                        }
                                    ?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td><span class="tdTitle">Quantidade:</span></td>
                            <td>
                                <input class="input" type="number" name="quantity">
                            </td>
                        </tr>
                        <tr>
                            <td><span class="tdTitle">Observações:</span></td>
                            <td>
                                <input class="input" type="text" name="observation">
                            </td>
                        </tr>
                    </tbody>
                </table>
                <input type="hidden" name="flag" value="1"/>
                <input type="hidden" name="objects" value="<?php echo htmlentities(serialize($objects)) ?>"/>
                <center><input name="addbutton" type="submit" value="Adicionar"/></center>
            </form>
        </div>
    </body>
</html>

当我试图遍历我的对象以检索它们的值并将它们放在列表中时,就会出现问题,我得到Trying to get property of non-object.

这是我Object班的代码:

<?php
    class Object
    {
        public $name;
        public $quantity;
        public $observation;
    }
?>

我在这里做错了什么,为什么会发生这个错误?

4

1 回答 1

1

您正在将对象数组创建为$objects; 但你的 foreach 循环是:

 foreach ($object as $product)

你错过了结尾的“s”$objects

于 2013-04-26T18:27:08.250 回答