我正在尝试让一个图片库管理删除过程在 PHP 中工作。我一直在纠结如何更新列表顺序,以便在删除后它们保持相同的顺序。
我有一个关联数组 ($images),其中键与“订单”值相同,该数字定义了画廊中的位置。我还有一个应该删除的订单号列表,通过用订单号标识每个图像来删除它。
$images format
array(28) {
[1]=>
array(5) {
["gallery_id"]=>
string(2) "71"
["property_id"]=>
string(1) "3"
["picture"]=>
string(17) "imgname.jpg"
["order"]=>
string(1) "1"
["alt_text"]=>
string(14) "discription"
}
[2]=>
array(5) {
["gallery_id"]=>
string(2) "83"
["property_id"]=>
string(1) "3"
["picture"]=>
string(17) "imgname.jpg"
["order"]=>
string(1) "2"
["alt_text"]=>
string(14) "discription"
}
So on... how ever large the list might be.
要删除的图像列表
$removedImgs
array(2) {
[0]=> string(1) "1"
[1]=> string(1) "3"
}
以上显示图像 1 和 3 将从图库中删除
Current: 1 2 3 4 5 6 ...
Removal: 2 4 5 6
| | | |
Reordering: 1 2 3 4
实际删除代码
// Loop though with each image and remove the ones posted from the list
foreach ($_POST['orderID'] as $removeImg)
{
// Store each removed images order id
$removedImgs[] = $removeImg;
// If we're removing images create a list of the image paths to
// unlink the files later.
if (isset($images[$removeImg]))
{
$unlinkList[] = $imgPath . $images[$removeImg]['picture'];
$unlinkList[] = $imgPath . 'thumbs/thumb' . $images[$removeImg]['picture'];
}
// $images should only contain the ones that we haven't removed.
unset($images[$removeImg]);
// Update the image order
foreach ($images as $key => &$img)
{
if ($key > $removeImg)
{
(int)$img['order']--;
}
}
var_dump($images);
echo "\n\n==========\n\n";
}