2016-03-29 26 views
0

所以我有一個條件,檢查用戶表中的用戶標識與附加到該標題的用戶標識相同,然後我要附加一個特定的鏈接,以滿足每個職位與實際數據發佈情況:在if語句爲真的情況下一次處理一個結果

<?php if($post->user_id == $first):?> 
       <?php foreach($code1 as $k=>$codes1): ?> 
       <div class="imgSet"> 
        <a href= "modify.php?1u=<?php echo $codes1->code;?>"> <!--hyperlink to edit text --> 
       <img src="img/settings.png"/> <!-- edit logo --> 
        <a/> 
       </div> 
       <?php endforeach;?> 
       <?php endif; ?> 

例如: 如果$post->user_id== $first,我的代碼將顯示$code從數據庫中獲取所有的價值,我想展示每個帖子的結果一次爲$post->user_id== $first

更具體地說,如果我有更多的帖子在if條款中遇到相同的條件,我想附加到那個特定於該帖子的代碼,但是我的程序返回的每個條件滿足所有數據庫的結果。

回答

0

您可以使用array_shift從代碼數組中獲取第一個代碼。所有你必須保證的是,你像以前那樣獲得代碼的順序相同的職位

<?php 
foreach ($posts as $post) { 
    if($post->user_id == $first) { 
     $codes1 = array_shift($code1); 
?> 
<div class="imgSet"> 
    <a href="modify.php?1u=<?php echo $codes1->code;?>"> <!--hyperlink to edit text --> 
     <img src="img/settings.png"/> <!-- edit logo --> 
    <a/> 
</div> 
<?php 
    } 
} 
?> 
+0

它仍然是相同的結果,都可以通過數據庫的結果,每個柱子的地方,如果是條件滿足 –

+0

'$ codes1'和'$ post'之間有什麼關係? –

+0

'codes1'是特定於每個帖子的代碼,'post'從帖子表中檢索所有數據。 –

相關問題