2017-10-11 172 views
-2

我想把一個滾動條放在foreach中,現在問題是,輸出顯示每個回顯數據上的div。如何把div放入foreach循環?

+0

給你的代碼...... – GYaN

+0

https://shrib.com/#foreachproblem –

+0

你能提供結果數組,所以我可以給你解決..? – GYaN

回答

0

每次循環迭代時,foreach循環都會回顯。我假設你正在嘗試將某些數組或對象的內容回顯爲可能的行。

要做到這一點,你需要在foreach循環之外回顯div的打開和關閉標籤。然後,您將使div爲靜態高度,並設置溢出滾動。使用內聯樣式是不是最好的做法,但我會用他們在我的例子爲簡潔起見:

$listOfAnimalsOnNoahsArk = array('dog', 'cat','sheep','warewolf', '...'); 

echo '<div style="width:100%;height:500px;overflow:scroll">'; 
foreach($listOfAnimalsOnNoahsArk as $animal) { 
    echo "<p>$animal</p>"; //Double quotes will print variable values 
} 
echo '</div>'; //Single quotes use less CPU because they print verbatim 

現在,如果你打算在你的foreach循環複製<div>,您的問題是一個簡單的CSS之一。根據需要調整寬度和高度。

[編輯]

您的代碼發佈後,我發表了我的評論。這裏是我會寫代碼來解決你的工作通過問題:

<?php 
if($results): 
?> 
    <div class="container"><!--You seem to only need one container--> 
    <?php 
    foreach($results as $blog): //Consider using the colon format (alternate syntax for control structures) for clarity 
    if($blog->role == 'student'): 
    ?> 
     <div class="alert alert-success alert-dismissable"> 
     <a href="<?=base_url('main/delete/'.$blog->replyid)?>" class="close" data-dismiss="alert" aria-label="close">×</a> 
     <?=$blog->reply.$blog->sent?> 
     </div><!--Added this--> 
    <?php 
    //Note: "<?=" in php is the same as "<?php echo" 
    endif; 

    if($blog->role == 'guidance'): 
    ?> 
     <div class="alert alert-danger alert-dismissable"> 
     <a href="<?=base_url('main/delete/'.$blog->replyid)?>" class="close" data-dismiss="alert" aria-label="close">×</a> 
     <?=$blog->sender.':'.$blog->reply.$blog->sent?> 
     </div><!--Added this--> 
    <?php 
    endif; 
    endforeach; 
    ?> 
    </div> 
<?php 
endif; 
?> 

你的主要問題是你的主容器必須是循環外。如果我錯了,用清晰的語法編寫代碼將會提高代碼的可讀性,並且使得移動元素變得更加容易。我選擇了另一種控制結構if():endif;來更清楚地區分PHP邏輯和HTML輸出。

+0

是的,我想嘗試一下,但是我的代碼的一致性處於危險之中,因爲您可以看到我使用了很多php打開和關閉標記來避免單引號方法。 –

0

我根據您提供的信息給出答案。

它可以爲你的作品...

請忽略數組$結果。

<?php 
$results[0] = json_decode(json_encode(array(
     'concern' => 'hehehe', 
     'sent' => '2017-10-11 08:36:09', 
     'flag' => 1, 
     'adflag' => 1, 
     'role' => 'student', 
     'stud_delete' => 0, 
     'gui_delete' => 0, 
     'replyid' => 118, 
     'blogid' => 16, 
     'sender' => 'Alfred Santos Angeles', 
     'username' => 201410165, 
     'reply' => 'oo' 
    ))); 

echo '<div class="container">'; 
if($results){ foreach ($results as $blog) { if($blog->role == 'student')echo ' 
     <div class="alert alert-success alert-dismissable"> 
      <a href="'.base_url('main/delete/'.$blog->replyid).'" class="close" data-dismiss="alert" aria-label="close">×</a>'.$blog->reply.$blog->sent.' 
     </div>';if($blog->role == 'guidance') echo ' 
     <div class="container"> 
      <div class="alert alert-danger alert-dismissable"> 
       <a href="'.base_url('main/delete/'.$blog->replyid).'" class="close" data-dismiss="alert" aria-label="close">×</a>'.$blog->sender.':'.$blog->reply.$blog->sent.' 
      </div> 
     </div>';}} echo ' 
    </div>';?>