我正在尝试在foreach中放置一个带有滚动条的div,现在的问题是,输出显示每个回显数据的div。
答案 0 :(得分: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
现在,如果您打算在<div>
循环中复制foreach
,那么您的问题就是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输出。
答案 1 :(得分:0)
我会根据您提供的信息给您回答。
它可能对你有用......
请忽略数组$ results。
<?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>';?>