2012-09-11 32 views
0

對於某些情況:我正在構建一個博客系統,用戶可以在其中看到他們添加的所有文章。爲此,我恢復其中username = $ thatUser的文章的ID並使用foreach循環來顯示。作品發現除了我希望用戶能夠刪除這些文章。有點「你確定嗎?」消息在燈箱中出現,這是問題所在。點擊按鈕顯示燈箱時,只顯示最後的ID。請參見下面的代碼:在燈箱中恢復ID

首先燈箱代碼(jQuery的):

$(document).ready(function() 
    { 
      $('a#deleteArticleFormJS').click(function() 
      { 
        $('#background,#deleteArticle').fadeIn('fast'); 
      }); 

      $('a#hideDeleteArticle, a#cancelDeleteArticle').click(function() 
      { 
        $('#background,#deleteArticle').fadeOut('fast'); 
      }); 
    }); 

然後PHP/HTML代碼:

<?php 
     //articles(); is a basic SQL query to recover the ID 
     $Articles = articles(); 

     foreach($Articles as $Article) 
     { 
    ?> 
     <!-- Recover the thumnail of the articles, the ID is correct. --> 
     <img src="avatar/<?php echo $Article['ID']?> - Avatar.jpg"> 

     <!-- Link that shows the lightbox with the "Are you sure" message, the ID is correct. --> 
     <a href="#<?php echo $Article['ID'];?>" id="deleteArticleFormJS">Delete</a> 

     <!-- The "lightbox". -->      
     <div id="deleteArticle"> 
      //*** PROBLEM: only show the LAST ID?! ***// 
      Delete <?php echo $Article['ID'];?>        
     </div> 

    <!-- End of the foreach loop --> 
    <?php 
    } 
    ?> 

我的問題是:
1 - 如何獲得適當的在燈箱中的ID?

如果需要更多的細節,代碼或任何東西,請詢問。

P.S:我不確定這是正確的「StackExchange」網站把這個。如果沒有,我很抱歉。

+0

從瀏覽器中呈現的HTML將是有益的了。 – Jack

+0

你的第一個問題是你正在使用ID,你應該使用類。 ID應該是不同的/唯一的,但是您通過將它們放在循環中多次使用相同的ID(「deleteAreticleFormJS」和「deleteArticle」)。 –

回答

0

試試這個:

$(document).ready(function() 
{ 
     $('a.deleteArticleFormJS').click(function() 
     { 
       $('#background,#deleteArticle'+$(this).data('article-id')).fadeIn('fast'); 
     }); 

     $('a.hideDeleteArticle, a.cancelDeleteArticle').click(function() 
     { 
       $('#background,#deleteArticle'+$(this).data('article-id')).fadeOut('fast'); 
     }); 
}); 


    <?php 
     //articles(); is a basic SQL query to recover the ID 
     $Articles = articles(); 

     foreach($Articles as $Article) 
     { 
    ?> 
    <!-- Recover the thumnail of the articles, the ID is correct. --> 
    <img src="avatar/<?php echo $Article['ID']?> - Avatar.jpg"> 

    <!-- Link that shows the lightbox with the "Are you sure" message, the ID is correct. --> 
    <a href="#<?php echo $Article['ID'];?>" class="deleteArticleFormJS" data-article-id="<?php echo $Article['ID']; ?>" >Delete</a> 

    <!-- The "lightbox". -->      
    <div class="deleteArticle" id="deleteArticle<?php echo $Article['ID']; ?>" > 
     //*** PROBLEM: only show the LAST ID?! ***// 
     Delete <?php echo $Article['ID'];?>        
    </div> 

<!-- End of the foreach loop --> 
<?php 
} 
?> 
+0

完美,工作完美。謝謝! – user1648791