为什么我的视图显示错误:数组到字符串转换

时间:2015-09-02 09:39:06

标签: codeigniter

我在控制器功能和调用表中创建表格,以便在视图页面中显示。

我在视图页面中的代码

<?php 
    foreach($tables as $table){
        echo $tables;
    }
?>

这是我的控制器

$table ='';
    $tables[]= array();
    $result = array();
    $param = new stdClass();

    if(sizeof($_GET) > 0){
        $param->date_from = $this->input->get('date_from');
        $param->date_to = $this->input->get('date_to');
        $param->sales_name = $this->input->get('sales_name');
        $data['result'] = $this->m_commission->check_invoice_user ($param);
        foreach ($result as $result){
        $table = '<div>'.$result->INVOICEDATE.'</div>';
        }
        $tables[] = $table;
    }
    $data['tables'] = $tables;
    $data['team'] = $this->m_user->get_team();
    $this->load->view('v_header');
    $this->load->view('v_test', $data);
    $this->load->view('v_footer');

我的模型我已经检查查询已经有效了。

4 个答案:

答案 0 :(得分:0)

第一眼(回声$table而非$tables):

<?php 
    foreach($tables as $table){
        echo $table;
    }
?>

(编辑:) 在您的控制器中:

    $tables[]= array();
    $param = new stdClass();

    if(sizeof($_GET) > 0){
        $param->date_from = $this->input->get('date_from');
        $param->date_to = $this->input->get('date_to');
        $param->sales_name = $this->input->get('sales_name');
        $results = $this->m_commission->check_invoice_user ($param);
        foreach ($results as $result){
           $tables[]= '<div>'.$result->INVOICEDATE.'</div>';
        }
    }
    $data['tables'] = $tables;
    $data['team'] = $this->m_user->get_team();
    $this->load->view('v_header');
    $this->load->view('v_test', $data);
    $this->load->view('v_footer');

在您的模型中,更改行:

if ($query->num_rows() > 0) {
            $result = $query->num_rows();
        }
        return $result;

if ($query->num_rows() > 0) {
     return $query->result();
}else return false;

答案 1 :(得分:0)

它仍然显示错误“数组到字符串转换”并显示“为foreach()提供的无效参数”

这是我的模特

function check_invoice_user($param){
    $result = array();
    $this->db->select("*");
    $this->db->from("bsm_salesinvoiceheader bh");
    $this->db->join("bsm_customerpayment bsmc", "bsmc.INVOICENUMBER = bh.INVOICENUMBER", 'LEFT');
    $this->db->join("bsm_salesinvoicelines bsms", "bsms.INVOICENUMBER = bh.INVOICENUMBER", 'LEFT');
    $this->db->like("bh.ADDITIONALFIELD_2", $param->sales_name, 'BOTH');
    $this->db->or_like("bh.ADDITIONALFIELD_5", $param->sales_name, 'BOTH');
    $this->db->or_like("bh.ADDITIONALFIELD_8", $param->sales_name, 'BOTH');
    $this->db->where("bh.INVOICEDATE >= ",$param->date_from);
    $this->db->where("bh.INVOICEDATE <= ",$param->date_to);
    $this->db->where("bsmc.APPLIEDDATE >= ",$param->date_from);
    $this->db->where("bsmc.APPLIEDDATE <= ",$param->date_to);
    $this->db->order_by("bh.INVOICEDATE", 'ASC');

    $query = $this->db->get();
    echo $this->db->last_query();
    if ($query->num_rows() > 0) {
        $result = $query->num_rows();
    }
    return $result;

}

答案 2 :(得分:0)

查看:

<?php 
    foreach($tables as $table){
        echo $table;
    }
?>

实际上它仍然是一个数组。 试试这个:

<?php 
print_r($tables);
    foreach($tables as $table){
        echo $table['your_column_name'];
        //print_r($table);
    }
?>

答案 3 :(得分:0)

1

  

遇到PHP错误

     

严重性:警告

     

消息:为foreach()提供的参数无效

     

文件名:controllers / commission.php

     

行号:334

     

回溯:

     

文件:   C:\ XAMPP \ htdocs中\ sales_commission \应用\控制器\ commission.php   行:334函数:_error_handler

     

文件:C:\ xampp \ htdocs \ sales_commission \ index.php行:292功能:   require_once

2

  

遇到PHP错误

     

严重性:注意

     

消息:数组到字符串转换

     

文件名:views / v_test.php

     

行号:21

     

回溯:

     

文件:C:\ xampp \ htdocs \ sales_commission \ application \ views \ v_test.php   行:21功能:_error_handler

     

文件:   C:\ XAMPP \ htdocs中\ sales_commission \应用\控制器\ commission.php   行:349功能:视图

     

文件:C:\ xampp \ htdocs \ sales_commission \ index.php行:292功能:   require_once