2013-04-26 81 views
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; 
    } 
?> 

什麼我錯在這裏做什麼,這是爲什麼錯誤出現?

+2

結束你的數組被稱爲的 'S''$ objects';你的foreach循環是從'$ object'生成的 - 我認爲後者需要's'結尾 – andrewsi 2013-04-26 18:23:43

+0

確實。不能相信我沒有看到。發佈它作爲答案,我會將其標記爲已回答。 – 2013-04-26 18:25:48

+0

我想我們都做過這樣的事情。很高興有幫助。 – andrewsi 2013-04-26 18:28:16

回答

1

您正在創建的對象數組爲$objects;但你的foreach循環是:

foreach ($object as $product) 

你已經錯過了的$objects

相關問題