Php div中的Foreach仅检索最后一个值
我一直在寻找解决办法,但没有成功 我用foreach设置了一个div,它可以显示带有各自标题的图片,效果很好,我还设置了一个delete选项,但当我单击要删除的图像时,它只检索div中的最后一个值(假设我有10个图像,我要删除图像编号3,它给出了图像编号10的值) 以下是我的代码,可以很好地显示图像:Php div中的Foreach仅检索最后一个值,php,Php,我一直在寻找解决办法,但没有成功 我用foreach设置了一个div,它可以显示带有各自标题的图片,效果很好,我还设置了一个delete选项,但当我单击要删除的图像时,它只检索div中的最后一个值(假设我有10个图像,我要删除图像编号3,它给出了图像编号10的值) 以下是我的代码,可以很好地显示图像: $a1 = new ArrayIterator($newrow); $a2 = new ArrayIterator($newrowcaption); $it = new MultipleIter
$a1 = new ArrayIterator($newrow);
$a2 = new ArrayIterator($newrowcaption);
$it = new MultipleIterator;
$it->attachIterator($a1);
$it->attachIterator($a2);
foreach($it as $e =>$ekey) {
?><form action ='' method='POST'>
<?php
echo '<div class="boxpic">'."<img src='../../pictures/pics/{$ekey[0]}' height='120' width='auto' />"."<br />". $ekey[1]."<input type='submit' class='deleteinput' name='deletepic' . ' value='Delete' >"."<br />".'</div>';
}
我找不到获取要删除的图像的当前值的解决方案。(注意:我正在使用var_dump,因为我想查看输出)您能给我一些提示吗?foreach($e=>$ekey):
foreach($it as $e =>$ekey) :
?>
<form action ='' method='POST'>
<div class="boxpic">
<img src='../../pictures/pics/<?php echo $ekey[0]; ?>' height='120' width='auto' />
<br />
<?php echo $ekey[1]; ?>
// i'm making a guess as to the pic id...is it in ekey[3]? anyway
// put the pic id here in the hidden input var for this form
// change $ekey[3] in this next line to the element of the unqiue id
<input type='hidden' name='deletepic' value='<?php echo $ekey[3]; ?>'>
<input type='submit' class='deleteinput' name='deletepic' value='Delete'>
<br />
</div>
</form>
?>
endforeach;
?>
'height='120'width='auto'/>
//我在猜测照片的id…它在ekey[3]中吗?无论如何
//将pic id放在这个表单的隐藏输入变量中
//将下一行中的$ekey[3]更改为unqiue id的元素
endforeach;
将
元素移到foreach
循环的外部我尝试了,但没有成功。我移到了外部,但结果仍然相同。问题似乎是这样的,如果(isset($_POST['deletepic']){foreach($ekey as$ekey1=>$newvalue){var_dump($newvalue);}},我可以为每个图像提供一个特定的id吗?也许这就是解决方案?我想你评论错了解决方案。我没有建议将表格移到外面。事实上,我认为你需要在循环中使用它…每个表单一个表单。我不知道的是,$ekey
元素包含该图片的unqiue id。是的,很抱歉评论了错误的解决方案。当我点击submit按钮时,var_转储的结果可能会有所帮助?字符串'pic10.jpg'(长度=9)字符串'Caption 10'(长度=10),要删除哪个图像并不重要。它总是返回相同的结果。每个图片在数据中都有唯一的id吗?如果是,迭代器中$ekey
元素中的元素是什么?是的,每个图片在数据中都有一个唯一的id:我的数据库有10列,分别命名为pic1、pic2、pic3,。。。和10列,分别命名为caption1、caption2、caption3,。。。保存在数据库中的图片没有一个具有相同的名称。迭代器将pic和它们各自的标题组合在同一个div中。
foreach($it as $e =>$ekey) :
?>
<form action ='' method='POST'>
<div class="boxpic">
<img src='../../pictures/pics/<?php echo $ekey[0]; ?>' height='120' width='auto' />
<br />
<?php echo $ekey[1]; ?>
// i'm making a guess as to the pic id...is it in ekey[3]? anyway
// put the pic id here in the hidden input var for this form
// change $ekey[3] in this next line to the element of the unqiue id
<input type='hidden' name='deletepic' value='<?php echo $ekey[3]; ?>'>
<input type='submit' class='deleteinput' name='deletepic' value='Delete'>
<br />
</div>
</form>
?>
endforeach;