doushang7209 2015-09-07 19:07
浏览 42
已采纳

在codeigniter中从模型中获取数据的奇怪问题

I have a model a method in my Model as

public function selectWhereLimitOffset($table_name, $where = NULL, $limit, $offset)
{
    if ($where) {
        $this->db->where($where);
    }

    $this->db->order_by('post_id', "desc"); 

    if($limit)
        $this->db->limit($limit, $offset);

    $result = $this->db->get($table_name);
    return $result->result();
} 

I'm calling this method from this function in my controller:

public function exams($page_id = NULL)
{
    $offset = 0;

    if($page_id == NULL)
        $offset = 1;
    else
        $offset = ($page_id * 10);

    $limit = 10;

    $data['main_content'] = 'dashboard/exams';
    $data['page_errors'] = FALSE;
    $where = NULL;
    $data['results_exam'] = $this->Fetch->selectWhereLimitOffset('exam', $where, $limit, $offset);
    $data['results_count_total'] = $this->Fetch->getCount('exam');
    $this->load->view('includes/template_su', $data);
}

Now the weird thing here is, the method selectWhereLimitOffset() works fine if called from other similar methods in my controller. But when called from this method exams(), it doesn't fetch the data from the table. A similar method that work perfectly fine is:

public function posts($page_id = NULL)
{
    $offset = 0;

    if($page_id == NULL)
        $offset = 1;
    else
        $offset = ($page_id * 10);

    $limit = 10;

    $data['main_content'] = 'dashboard/posts';
    $data['page_errors'] = FALSE;
    $where = NULL;
    $data['results_post'] = $this->Fetch->selectWhereLimitOffset('post', $where, $limit, $offset);
    $data['results_count_total'] = $this->Fetch->getCount('post');
    $this->load->view('includes/template_su', $data);
}

One more thing, the next statement:

$data['results_count_total'] = $this->Fetch->getCount('exam');

does return the count of rows from the table, so that proves that there's no issue with the table name. The table has only one row, if that might help. The output of the data array is as follows:

Array
(
    [main_content] => dashboard/exams
    [page_errors] => 
    [results_exam] => Array
        (
        )

    [results_count_total] => 1
)

Please help.

  • 写回答

1条回答 默认 最新

  • dongta5747 2015-09-07 19:16
    关注

    Solved the problem. Just had to change the offset value from 1 to 0. Thats it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 画出的分类图不对,求解答一下
  • ¥20 springboot和springcloud版本问题
  • ¥15 ps2手柄控制树莓派小车
  • ¥30 C#:vsto powerpoint的外接程序
  • ¥30 stata将do文件代码转化为ado文件
  • ¥15 两个同维数组相比,不同位置、出现重复比无意义,而不同位置、不出现重复比,则有意义。把有意义的两个数组放入新的集合MK中。
  • ¥15 可以远程电脑安装nvm
  • ¥15 写一个可直接调用的函数,将32位有符号数转成另一个无符号的数
  • ¥15 CMAKE+VS2019+QT5.15.2组合进行二次编译
  • ¥15 nginx 配置静态html访问 ,后台登录时页面始终被重定向到登录页,无法访问到后台的静态html页