Php 限制要在模块上显示的行数

Php 限制要在模块上显示的行数,php,codeigniter,Php,Codeigniter,我最近制作了一个用户活动模块 我只是想解决几个问题 问题:如何限制活动行数,使其仅显示在我的仪表板中的“我的活动”上 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Dashboard_activity extends MX_Controller { public function __construct() { parent::__construc

我最近制作了一个用户活动模块

我只是想解决几个问题

问题:如何限制活动行数,使其仅显示在我的仪表板中的“我的活动”上

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dashboard_activity extends MX_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->load->model('admin/dashboard/dashboard_activity_model');

        $results = $this->dashboard_activity_model->getRecentActivity();

        foreach ($results as $result) {
            $data['activitys'][] = array(
                'user_id' => $result['user_id'],
                'firstname' => $result['firstname'],
                'lastname' => $result['lastname'],
                'isLogged' => ($result['isLogged'] ? "Has Logged On" : "Has Logged Out"),
                // Should display Last Logged.
                'last_logged' => date($this->lang->line('date_format_short'), strtotime($result['last_logged']))
            );

        }

        return $this->load->view('dashboard/dashboard_activity', $data);
    }

}
模型

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dashboard_activity_model extends CI_Model {

    public function getRecentActivity($data = array()) {

        $sql = "SELECT * FROM `" . $this->db->dbprefix . "user`";

        $sort_data = array(
            'user_id',
            'firstname',
            'lastname',
            'isLogged',
            'last_logged'
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY firstname";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->result_array();

    }
}
看法


在我的数据库模型中,我用这个替换了我的数据

public function getRecentActivity() {
    $this->db->select('*');
    $this->db->from('user');
    $this->db->order_by('firstname', 'dsc');
    $this->db->limit(5);
    $query = $this->db->get();
    return $query->result_array();  
} 
现在工作正常,有限制

public function getRecentActivity() {
    $this->db->select('*');
    $this->db->from('user');
    $this->db->order_by('firstname', 'dsc');
    $this->db->limit(5);
    $query = $this->db->get();
    return $query->result_array();  
}