2016-12-04 110 views
-1

我一直在寻找解决方案这么久没有成功。在一个div中的foreach只是检索最后一个值

我设置了一个显示带有各自标题的图片的foreach,它完美的工作,我也设置了删除选项,但是当我点击要删除的图片时,它只会检索到最后一个值div(假设我有10个图像,我想删除图像编号3,它给了我图像编号10的值)。

这里是我的代码工作正常显示图像:

$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>'; 
} 

这里是我试图从特定的图像检索值

if(isset($_POST['deletepic'])){ 
    foreach ($ekey as $ekey1=>$newvalue) { 
    var_dump($newvalue); 
    } 
} 

我无法找到解决办法获取我想要删除的图像的当前值。 (注意:我使用var_dump,因为我想查看输出)你能给我一些提示吗?

+1

移动''

元素'foreach'环 – Dekel

+0

我尝试之外,但它不工作。 – Daniela

回答

0
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; 
+0

我将移到了外面,但结果仍然相同。如果(isset($ _ POST ['deletepic']))foreach($ ekey as $ ekey1 => $ newvalue),则此问题似乎与此 var_dump($ newvalue); } } 此外,我可以给每个图像的具体ID?也许这是解决方案? – Daniela

+0

我想你说错了解决方案。我没有建议将表格移到外面。事实上,我认为你需要它在循环内...每种形式。我不知道的是'$ ekey'元素包含该图片的unqiue ID。 – WEBjuju

+0

是的,抱歉对错误的解决方案发表评论。也许var_dump的结果,当我点击提交按钮可以帮助吗? string'pic10.jpg'(length = 9) 字符串'Caption 10'(长度= 10) 而且您要删除哪个图像并不重要。它总是给我返回相同的结果。 – Daniela