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

时间:2014-10-04 06:07:58

标签: php codeigniter

我已经制作了一个最近的用户活动模块。

我只想解决几个问题。

问题如何限制要在我的信息中心的活动中显示的活动行数

<?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();

    }
}

查看

<div class="panel panel-default">
    <div class="panel-heading">
    <h3 class="panel-title"><i class="fa fa-calendar"></i> Recent Activity For Admin Users</h3>
    </div>
    <ul class="list-group">
        <?php foreach ($activitys as $activity) { ?>
        <li class="list-group-item">
        <a href=""><?php echo $activity['firstname'];?> <?php echo $activity['lastname'];?></a> <?php echo $activity['isLogged'];?>
        <br />
      <small class="text-muted"><i class="fa fa-clock-o"></i></small> <?php echo $activity['last_logged'];?></li> 
      <?php } ?>
    </ul>
</div>

1 个答案:

答案 0 :(得分:0)

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

替换了我的数据
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();  
} 

现在使用限制

工作正常