4

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

这是我的控制器 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>
4

2 回答 2

15

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

所以你需要改变

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

$data['query']=$results;

或者

$data['query']=$query->result_array();
于 2013-09-25T08:28:44.040 回答
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>
于 2013-09-25T08:23:42.220 回答