2016-12-04 55 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