这是我的代码:
$printcoll = $_SESSION['ObjColl'];
for($i = 0;$i < $printcoll->getlineCount();$i++){
$li = $printcoll -> getLineItem($i);
$item = $li->getItem();
if ($item instanceof Product) {
print "Bike ID - ";
}
print $item-> getId();
if ($item instanceof Product) {
print "  Price £";
}
print $item-> getPrice();
if ($item instanceof Product) {
print "  Quantity - ";
}
print $li->getQuantity();
echo "</br>";
以下是ObjectCollection的样子:
ObjectCollection Object ( [line_items_array:ObjectCollection:private] => Array ( [0] => LineItem Object ( [item:LineItem:private] => Product Object ( [id] => 1 [name] => Yamaha [price] => 10000 ) [quantity:LineItem:private] => 3 ) [1] => LineItem Object ( [item:LineItem:private] => Product Object ( [id] => 4 [name] => Kawasaki [price] => 12000 ) [quantity:LineItem:private] => 7 )
For循环的输出是:
自行车ID - 1价格£10000数量 - 3
自行车ID - 4价格£12000数量 - 7
ObjectCollection由Product对象组成。如何在for循环中添加一个选项,以便用户可以从ObjectCollection类中删除“Product”对象?
public function delLineItem($ line_item)是ObjectCollection类中的函数。但是我如何将它集成到for循环中的OPTION中,因为用户可能会也可能不会删除它?
因此,例如,我希望for循环的输出为:
自行车ID - 1价格£10000数量 - 3 DELETE BUTTON
因此,如果用户单击删除按钮,它将从对象集合中删除该特定产品。 感谢
答案 0 :(得分:0)
您必须链接回当前脚本并创建操作,例如名为Delete ...
将if块移到顶部,添加你的代码以删除那里的项目...再往下,你会看到一个删除链接,用于激活动作......
$printcoll = $_SESSION['ObjColl'];
//Code for delete operation
if (is_set($_GET['delete'])) {
$id = $_GET['delete'];
echo "Deleting $id...";
}
for($i = 0;$i < $printcoll->getlineCount();$i++){
$li = $printcoll -> getLineItem($i);
$item = $li->getItem();
if ($item instanceof Product) {
print "Bike ID - ";
}
print $item->getId();
//Add a delete link
print "<a href='" . $_SERVER['PHP_SELF'] . "?delete=" . $i . "'>Delete</a>";
if ($item instanceof Product) {
print "  Price £";
}
print $item-> getPrice();
if ($item instanceof Product) {
print "  Quantity - ";
}
print $li->getQuantity();
echo "</br>";