gpt4 book ai didi

php - 使用 HTML 表单从图像数组中删除字符串

转载 作者:行者123 更新时间:2023-11-30 01:12:25 25 4
gpt4 key购买 nike

我的记录存储在 MySQL 数据库中。图像存储方式如下:

[image1.jpg],[image2.jpg],[image3.jpg] ...等

我使用 SQL Select 查询选择行,然后:

$images = explode(',',$project['images']);

分解每个图像(用 , 分隔)

然后我有:

<?php
foreach($images as $image)
{
$display_image = substr($image, 1, -1);
?>
<tr>
<td><img src="/img/project-gallery/<?php echo $display_image; ?>" width="160px" /></td>
</tr>
<?php
}
?>

如何使用表单中的 HTML 复选框来删除图像(仅选定的图像),如下所示:

<input type="checkbox" name="images[]" id="images[]" />

最佳答案

这是一个非常广泛的问题,因此我只讨论 HTML 部分。

您想要添加 <input>每个图像旁边的元素...类似这样:

<tr>
<td>
<input name="images_to_delete[]" type="checkbox" value="<?php echo $display_image; ?>" />
<img src="/img/project-gallery/<?php echo $display_image; ?>" width="160px" />
</td>
</tr>

您需要用 <form> 包围此表/列表标签。您还需要一个 php 页面来处理此表单的提交,但我将把它留给您。

在 PHP 中,一旦获得 $images_to_delete数组,这是一种可以从原始 $images 字符串中删除图像的方法:

$images = "," . $images . ","; // add comma to beginning and end of list

foreach ($images_to_delete as $image_to_delete) {
str_replace("," . $image_to_delete . ",", ","); // e.g. replaces ",image1.jpg," with ","
}

$images = substr($images, 1, -1); // remove comma from beginning and end

关于php - 使用 HTML 表单从图像数组中删除字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19369027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com