php - Codeigniter foreach() 限制
问题描述
使用 Codeigntier 3
我试图将我的数据库内容限制为 5 个帖子。现在我的 foreach 工作正常,但我不知道如何将数字限制为 5。
我在网上找到的大部分东西都来自 codeigniter 2。
控制器
public function index(){
$data['Sideposts'] = $this->Blog_Model->get_bposts();
模型
public function get_bposts($slug = FALSE){
if($slug === FALSE){
$this->db->order_by('date','DESC');
$query = $this->db->get('blogposts');
return $query->result_array();
}
查看代码
<?php foreach($Sideposts as $Sidepost):{ ?>
<ul style="list-none;">
<?php echo $Sidepost['title']; ?>
</ul>
<?php }endforeach; ?>
解决方案
public function get_bposts($slug = FALSE,$lmt=0){
if($slug === FALSE){
$this->db->order_by('date','DESC');
if($lmt>0)
{
$query = $this->db->get('blogposts',$lmt);
} else {
$query = $this->db->get('blogposts');
}
return $query->result_array();
}
使用它如下:
$data['Sideposts'] = $this->Blog_Model->get_bposts(FALSE,5);
推荐阅读
- powershell - 如何使用 powershell 连接到 Microsoft Teams
- php - 删除 altervista 的 .htaccess 后,该站点不断将我重定向到旧 URL
- html - 页眉与打印预览模式中的内容重叠
- 3d - Gnuplot:来自部分参数化输入的 3D 图
- reactjs - 酶测试错误,方法“模拟”意味着在 1 个节点上运行。找到了 0 个
- android - 在宽松的内存模型下触发错误(尤其是部分存储排序)
- c - 为什么在函数的链表头部添加新节点时需要使用指向指针的指针?
- c# - C# - 如何将表单数据发布到 PHP 文件并将用户永久重定向到 url?
- c# - 将模板占位符替换为带有反射的对象属性
- arrays - 当用户输入特定整数时如何正确退出while循环