非法字符串偏移codeigniter

时间:2013-09-25 08:12:52

标签: php codeigniter

您一直在尝试从我的数据库中检索记录,但我在几个字段中一直收到“严重性:警告消息:非法字符串偏移”这个错误。

这是我的控制器view_logs.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class View_Logs extends CI_Controller {

function View_Logs()
 {
   parent::__construct();
 }  

function Logs(){
    $id = $this->uri->segment(3);

    $this->load->model('log_listmodel');
    $this->log_listmodel->log_list_get($id);    
}
}
?>

这是我的模型log_listmodel.php

<?php
class Log_Listmodel extends CI_Model{

    function Log_Listmodel()
    {
      parent::__construct();
    }

    function log_list_get($id){
    $query = $this->db->get_where('test_request_log', array('test_request_id' => $id));
    //return $query->result();
    $results=$query->result_array();


    $data['query']=$results[0];
    $this->load->view('logs_list_view',$data);
    }
}
?>

这是我的视图页面log_list_view.php

<table class="list_header" bgcolor="#ffffff" border="0" width="1020px" cellpadding="4px">

            <?php foreach($query as $row): ?>
            <tr> 
                <td><b>Updated</b></td>
                <td><?php echo $row['id'];?>.</td>
                <td><?php echo $row['new_testing_reason'];?></td>
                <td><?php echo $row['new_applicant_name'];?></td>
                <td><?php echo $row['new_authorizer_name'];?></td>
                <td><?php echo $row['new_received_by'];?></td>
                <td><?php echo $row['new_test_required'];?></td>
                <td><?php echo $row['new_laboratory_number'];?></td>
                <td><?php echo $row['log_date'];?></td>
                <td><?php echo $row['who'];?></td>
            </tr>
            <?php endforeach; ?>
        </table>

2 个答案:

答案 0 :(得分:10)

您已将$data['query']设置为结果的第一行,但在视图中您正在使用它,因为它将拥有整个数据集。

所以你需要改变

$data['query']=$results[0];

$data['query']=$results;

$data['query']=$query->result_array();

答案 1 :(得分:1)

你的代码是一种不正确的结构,我只是重构它并简化它。 希望它有效。

首先让我们加载你的数据库。

转到 application / config / autoload.php   找到这一行然后自动加载数据库

/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your application/libraries folder.
|
| Prototype:
|
|   $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

$autoload['libraries'] = array('database');

控制器视图

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class View_Logs extends CI_Controller {

function View_Logs()
 {
   parent::__construct();
   $this->load->library('database');   // if you didn`t load 'database' in your autoload.php
   $this->load->model('log_listmodel');
 }  

function Logs(){
    $id = $this->uri->segment(3);


    $data['query'] = $this->log_listmodel->log_list_get($id)->result();

    $this->load->view('logs_list_view',$data);
}
?>

模型视图

<?php
class Log_Listmodel extends CI_Model{

    function Log_Listmodel()
    {
      parent::__construct();
    }

    function log_list_get($id){
        return $this->db->get_where('test_request_log', array('test_request_id' => $id));

    }
}
?>

查看模式

<table class="list_header" bgcolor="#ffffff" border="0" width="1020px" cellpadding="4px">

            <?php foreach($query as $row): ?>
            <tr> 
                <td><b>Updated</b></td>
                <td><?php echo $row->id;?>.</td>
                <td><?php echo $row->new_testing_reason;?></td>
                <td><?php echo $row->new_applicant_name;?></td>
                <td><?php echo $row->new_authorizer_name;?></td>
                <td><?php echo $row->new_received_by;?></td>
                <td><?php echo $row->new_test_required;?></td>
                <td><?php echo $row->new_laboratory_number;?></td>
                <td><?php echo $row->log_date;?></td>
                <td><?php echo $row->who;?></td>
            </tr>
            <?php endforeach; ?>
</table>