我如何为用户创建一个选项以从对象集合中删除对象?

How can i create an option for the user to delete an object out of the Object Collection?

这是我的代码:

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

中删除一个 "Product" 对象

public function delLineItem($line_item) 是 ObjectCollection class 中的函数。但是我如何将它集成到 for 循环中的 OPTION 中,因为用户可能会或可能不会删除它?

例如,我希望 for 循环的输出为:

自行车 ID - 1 价格 10000 英镑数量 - 3 删除按钮

因此,如果用户单击删除按钮,它将从对象集合中删除该特定产品。 谢谢

您必须 link 返回到当前脚本并创建一个操作,例如称为删除...

Cjeck if 块到顶部,添加用于删除项目的代码...再往下您将看到用于激活操作的 Delete link...

$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 "&nbsp Price &pound"; 

}
print $item-> getPrice();

if ($item instanceof Product) {
 print "&nbsp Quantity - "; 

}

print $li->getQuantity();
echo "</br>";