我希望通过数据库中的foreach在另一列中显示更多结果,但我无法让它工作。
<div class="row">
<div class="col-md-4">
<?php
if (is_array($results))
{
foreach ($results as $data) { ?>
<!-- User item starts -->
<div class="user-item">
<!-- User action buttons -->
<div class="user-btns">
<a href="#" class="btn btn-green btn-xs"><i class="fa fa-pencil"></i></a>
<a href="#" class="btn btn-red btn-xs"><i class="fa fa-times"></i></a>
</div>
<!-- Image -->
<img src="<?php echo $data->avatar ?>" alt="" class="img-responsive">
<!-- User details -->
<div class="u-details">
<h5><i class="fa fa-user"></i><?php echo $data->first_name . ' ' . $data->last_name ?></h5>
<h5><i class="fa fa-envelope"></i> <?php echo $data->email ?></h5>
<h5><i class="fa fa-user-md"></i> <?php echo $data->username ?></h5>
</div>
<div class="clearfix"></div>
</div>
<!-- User item ends -->
<?php } } ?>
</div>
<div class="col-md-4">
<?php
if (is_array($results))
{
foreach ($results as $data) { ?>
<!-- User item starts -->
<div class="user-item">
<!-- User action buttons -->
<div class="user-btns">
<a href="#" class="btn btn-green btn-xs"><i class="fa fa-pencil"></i></a>
<a href="#" class="btn btn-red btn-xs"><i class="fa fa-times"></i></a>
</div>
<!-- Image -->
<img src="<?php echo $data->avatar ?>" alt="" class="img-responsive">
<!-- User details -->
<div class="u-details">
<h5><i class="fa fa-user"></i><?php echo $data->first_name . ' ' . $data->last_name ?></h5>
<h5><i class="fa fa-envelope"></i> <?php echo $data->email ?></h5>
<h5><i class="fa fa-user-md"></i> <?php echo $data->username ?></h5>
</div>
<div class="clearfix"></div>
</div>
<!-- User item ends -->
<?php } } ?>
</div>
</div>
如您所见,它是两个不同的列。我想做foreach并在一列中显示一组信息而在另一列中显示另一组。但是,下面的代码在两列中都返回相同的值。
我该怎么做?感谢。
答案 0 :(得分:0)
您正在迭代两个循环中的每个结果项,因此它们的结果是相同的。为什么不试试这样的事呢
<?php
$cols = 3;
?>
<div class="row">
<?php
if (is_array($results))
{
for($i = 0 ; $i < $cols ; $i++)
{
echo "
<div class='col-md-".(12/$cols)."'>";
for ($j = 0 ; $j < (count($results) / $cols) + 1 ; $j++)
{
if ($i * $cols + $j < count($results)) // make sure it won't give errors for array out of range....
{
echo "
<div class='user-item'>
<div class='user-btns'>
<a href='#' class='btn btn-green btn-xs'><i class='fa fa-pencil'></i></a>
<a href='#' class='btn btn-red btn-xs'><i class='fa fa-times'></i></a>
</div>
<img src='".$results[$i* $cols + $j]->avatar."' alt=' class='img-responsive'/>
<div class='u-details'>
<h5><i class='fa fa-user'></i>".$results[$i* $cols + $j]->first_name . ' ' . $results[$i* $cols + $j]->last_name."</h5>
<h5><i class='fa fa-envelope'></i>".$results[$i* $cols + $j]->email."</h5>
<h5><i class='fa fa-user-md'></i>".$results[$i * $cols + $j]->username."</h5>
</div>
<div class='clearfix'></div>";
}
}
echo "
</div>";
}
}
?>
</div>