【问题标题】:CodeIgniter: Pagination with Filter RowsCodeIgniter:使用过滤器行进行分页
【发布时间】:2014-08-09 17:08:13
【问题描述】:

我正在尝试使用一些过滤器选项和分页来制作数据表。 (也是以后想添加排序列)。

如果我尝试过滤第一页上的数据,它就可以正常工作。但是当我在另一个页面上并尝试过滤数据时,它会给出错误invalid argument supplied for foreach()

型号

public function fetch_rows($limit, $start)
{
    $emp_id = $this->input->post('emp_id');
    $this->db->like('employee_id',$emp_id);

    $emp_fname = $this->input->post('emp_fname');
    $this->db->like('first_name',$emp_fname);

    $emp_login = $this->input->post('emp_login');
    $this->db->like('login_id',$emp_login);

    $emp_position = $this->input->post('emp_position');
    $this->db->like('position',$emp_position);

    $this->db->limit($limit, $start);
    $query = $this->db->get($this->_table_name);

    var_dump($query);

    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    return FALSE;
}

控制器

public function employees()
{
    $this->data['title'] = '<i class="fa fa-users"></i> ' . lang('emp_all');

    $config                = array();
    $config['base_url']    = base_url() . 'admin/hr/employees';
    $config['total_rows']  = $this->employees_model->row_count();
    $config['per_page']    = 10;
    $config['uri_segment'] = 4;

    $this->pagination->initialize($config);

    $page                  = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
    $this->data['results'] = $this->employees_model->fetch_rows($config['per_page'], $page);
    $this->data['links']   = $this->pagination->create_links();

    $this->load->view('hr/employees/index', $this->data);
}

查看

<div class="form-group">
    <div class="row">
        <?= form_open(); ?>
        <div class="col-xs-1">
            <?= form_input(array('name' => 'emp_id', 'id' => 'emp_id', 'class' => 'form-control', 'placeholder' => 'Employee ID')); ?>                                
        </div>
        <div class="col-xs-2">
            <?= form_input(array('name' => 'emp_fname', 'id' => 'emp_fname', 'class' => 'form-control', 'placeholder' => 'First Name')); ?>                                
        </div>
        <div class="col-xs-1">
            <?= form_input(array('name' => 'emp_login', 'id' => 'emp_login', 'class' => 'form-control', 'placeholder' => 'Login Id')); ?>                                
        </div>
        <div class="col-xs-2">
            <?= get_positions('dropdown', 'emp_position'); ?>                                
        </div> 
        <div class="col-xs-1">
            <?= form_submit('filters', 'Go', 'class="btn-default btn"'); ?>
        </div>
        <div class="col-xs-2">

        </div>

        <?= form_close(); ?>
    </div>
</div><!-- End filter form -->

<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered datatables" id="">
    <thead>
        <tr class="active">
            <?= is_system_admin() ? '<th class="center">ID<span>*</span></th>' : NULL; ?>
            <th>Photo</th>
            <th>Employee ID</th>
            <th>Name</th>
            <th>Position</th>
            <th>Login ID</th>
            <th>Email</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        <?php
        if (count($results) > 0): foreach ($results as $data):                    
                echo '<tr>';
                echo is_system_admin() ? '<td class="center">' . $data->id . '</td>' : NULL;
                echo '<td><img src="' . get_employee_snap($data->employee_id, $data->employee_snap) . '" alt="snap" width="64"/></td>';
                echo '<td>' . $data->employee_id . '</td>';
                echo '<td>' . $data->first_name . ' ' . $data->last_name . '</td>';
                echo '<td>' . get_position_by_code($data->position) . '</td>';
                echo '<td>' . $data->login_id . '</td>';
                echo '<td>' . $data->email . '</td>';
                echo '<td>' . $data->employment_status . '</td>';
                echo '</tr>';
            endforeach;
        else:
            echo show_alert('No Postions record found in database.', FALSE, 'warning');
        endif;
        ?>                                     
    </tbody>
</table><!--end table-->
<?= $links; ?>

如何解决这个问题,无论我在哪个页面上都可以过滤,我也想让列可排序。

注意:我也想根据过滤结果更新分页数。

万分感谢... :)

【问题讨论】:

  • 您遇到了哪个 foreach 错误?在模型或视图文件中?您是否在 foreach 循环之前尝试转储 $query-&gt;result 以确保不为空?
  • 视图foreach()。您可以查看我的查看代码表

标签: php codeigniter pagination


【解决方案1】:

而不是

        foreach ($query->result() as $row)
        {
            $data[] = $row;
        }
        return $data;

你可以简单地使用

    return $query->result_array();

我发现如果没有找到结果,$this-&gt;data['results'] 将是 false,FALSE 将导致 foreach 出错,因为它不是数组。

注意:如果结果集为空,则会给出一个空白数组;

所以在模型中而不是

if ($query->num_rows() > 0) {
        foreach ($query->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    return FALSE;

只需使用

return $query->result_array();

【讨论】:

  • 谢谢.. 但是当我尝试时,它给我Message: Trying to get property of non-object 的每一列错误,如$data-&gt;employee_id etcc..
  • @CodeLover 它是一个数组 ($query-&gt;result_array();) 所以你必须使用 $data['employee_id'] 而不是 $data-&gt;employee_id
  • 好的,现在这很好,但是当我不在第一页上时,如果该项目不在那个页面上,它就会忽略过滤器。无论如何它会在哪里检查整个表格而不仅仅是在页面上?如果在过滤结果中只找到一条记录,它也会显示分页
  • 很抱歉,无法联系到您。 :(
  • 分页错误的原因:row_count() 您的employees_model 考虑过滤器?或者只是获取所有行(which is wrong, you must consider filters for getting $config['total_rows'])
【解决方案2】:

像这样改变你的模型:

 if ($query->num_rows() > 0) {
    $data = array();
    foreach ($query->result() as $row)
    {
        $data[] = $row;
    }
    return $data;
}

由于任何原因,如果查询无法加载数据,$data 被视为未定义。因为foreach没有执行。

【讨论】:

  • 您的代码也可以工作,但我想根据过滤结果影响分页。因此,当结果行数小于分页 per_page 时,分页应该不可见,依此类推
  • 如果是这样,我认为您的问题与$this-&gt;uri-&gt;segment(4) 部分有关。确保您通过fetch_rows 方法向模型传递了正确的值。
  • 没关系,一切正常,但只是在另一个页面上添加了一个过滤器。
猜你喜欢
  • 2021-01-02
  • 2019-07-22
  • 1970-01-01
  • 2021-10-19
  • 2020-10-05
  • 2019-07-21
  • 2016-07-01
  • 2018-12-25
  • 1970-01-01
相关资源
最近更新 更多