我有一个页面,其中有一个表单,用户可以在其中添加对象。逐一。当用户发送表单时,对当前页面进行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;
}
?>
我在这里做错了什么,为什么会出现这个错误?
答案 0 :(得分:1)
您正在创建对象数组$objects
;但你的foreach循环是:
foreach ($object as $product)
你错过了$objects