0

I have a page where there is a form on which the users can add objects. One by one. When the user sends the form a POST is made to the current page to update $objects with the new object. Here is the code.

<?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>

The problem occurs when I am trying to iterate through my objects to retrieve their values and put them on a list, I get Trying to get property of non-object.

This is the code of my Object class:

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

What am I doing wrong here and why is this error occuring?

3
  • 2
    Your array is called $objects; your foreach loop is generated from $object - I think the latter needs an 's' on the end Commented Apr 26, 2013 at 18:23
  • Indeed. Can't believe I've not seen that. Post it as answer, I will mark it as answered. Commented Apr 26, 2013 at 18:25
  • I think we've all done that sort of thing. Glad to have helped. Commented Apr 26, 2013 at 18:28

1 Answer 1

1

You're creating your array of objects as $objects; but your foreach loop is:

 foreach ($object as $product)

You've missed the 's' of the end of $objects

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

Comments

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.