【问题标题】:I am getting Unsupported operand types error in codeigniter我在 codeigniter 中收到不支持的操作数类型错误
【发布时间】:2019-02-18 13:52:21
【问题描述】:

当我尝试添加分页链接时,我收到不支持的操作数类型错误

这是我的代码

型号

public function getCategory($limit,$offset){

    $this->db->select("*");
    $this->db->from("tbl_category");
    $this->db->limit($limit,$offset);

    $row = $this->db->get()->result_array();

    return $row;
}

public function num_rows(){
    $this->db->select('*');
    $this->db->from('tbl_category');
    $row = $this->db->get()->result_array();
    return $row;
}

控制器

public function category($script="list",$id=""){
    $data['script'] = $script;

    if($script == 'list'){

        $config = [
            'base_url' => base_url('http://localhost/training/admin/'),
            'per_page' => 2,
            'total_rows' => $this->admin_model->num_rows(),
        ];
        $this->pagination->initialize($config);
        $rows = $this->admin_model->getCategory($config['per_page'], $this->uri->segment(3));
        $data['rows'] = $rows;
    }

在我的视图文件中,我这样做是为了获取链接

<?php $this->pagination->create_links(); ?>

我得到的错误如下所示

遇到未捕获的异常 类型:错误

消息:不支持的操作数类型

文件名:C:\xampp\htdocs\training\system\libraries\Pagination.php

行号:412

回溯:

文件:C:\xampp\htdocs\training\application\views\category.php 线路:101 函数:create_links

文件:C:\xampp\htdocs\training\application\controllers\Admin.php 线路:150 功能:查看

文件:C:\xampp\htdocs\training\index.php 线路:315 函数:require_once

【问题讨论】:

  • 显示功能正常,唯一的问题是我没有获得页面链接

标签: php codeigniter


【解决方案1】:

在你的控制器函数中,当你得到你的结果时,你必须将它传递给你的视图文件,如下所示。

$data['rows'] = $rows;

// load the view
$this->load->view('category_view', $data);

然后在你的视图文件中,你可以得到分页链接。

<?php echo $this->pagination->create_links(); ?>

EDIT => 在配置数组'total_rows' =&gt; $this-&gt;admin_model-&gt;num_rows(), 中,您分配的是数组而不是计数。

你的模型函数是

public function num_rows(){
    $this->db->select('*');
    $this->db->from('tbl_category');
    $row = $this->db->get()->result_array();
    return $row; //This $row is an Array, NOT count no. of rows
}

在上面的函数中,您返回的是一个数组 return $row;这就是你收到类似 Unsupported operand types 错误的原因

所以 2 解决方案。 只尝试任何一种解决方案。

要么 1) 您必须将结果集的计数返回给 total_rows 变量:

$config = [
            'base_url' => base_url('http://localhost/training/admin/'),
            'per_page' => 2,
            'total_rows' => count($this->admin_model->num_rows()), //Pass the count of returned array.
        ];

或 2)你可以改变功能。

public function num_rows(){
    $this->db->select('*');
    $this->db->from('tbl_category');
    $row = $this->db->get()->result_array();
    return $row->num_rows(); //Return count result instead of array.
}

注意:- 如果您只想计算行数,您可以使用 $this->db->count_all_results() 作为下面。

public function num_rows(){
    $this->db->from('tbl_category');
    return $this->db->count_all_results();
}

num_rows() :- 使用 num_rows() 你首先执行查询,然后你可以检查你得到了多少行。 当您需要表格数据时很有用

count_all_results() :- 使用 count_all_results() 您可以获得查询将产生的行数,但不会给您实际的结果集。 当您只需要行数时很有用,即分页,显示编号。记录等。

在此处参考我的另一个答案。 Codigniter Query Returning wrong count

【讨论】:

    【解决方案2】:

     $mess_fees = $this->db->query("SELECT sum(t2.amount) as mess_fees FROM `mess_fee_head` as t1 JOIN mess_fee_detail as t2 ON t1.id=t2.mess_fee_head_id WHERE t1.batch=$student_batch_id AND t1.academic_year_id=$newstudent_acedemic_year_id AND t2.status='Y'")->result();
     $num=4;
     $mess_fee_amount=($mess_fees)/$num;
    

    【讨论】:

      猜你喜欢
      • 2019-11-23
      • 2017-03-30
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多