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


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 "&nbsp Price &pound"; 
}
print $item-> getPrice();
if ($item instanceof Product) {
 print "&nbsp 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

对象

集合由产品对象组成。如何在 for 循环中添加一个选项,以便用户可以从 ObjectCollection 类中删除"产品"对象?

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

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

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

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

您必须

链接回当前脚本并创建一个操作,例如名为 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 "&nbsp Price &pound"; 
}
print $item-> getPrice();
if ($item instanceof Product) {
 print "&nbsp Quantity - "; 
}
print $li->getQuantity();
echo "</br>";