Hi been trying to retrieve records from my database, but I keep getting this error "Severity: Warning Message: Illegal string offset " in several fields.
Here's my controller 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);
}
}
?>
Here's my Model 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);
}
}
?>
Here's my view page 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>
You've set $data['query']
to the first row of your result, but in the view you're using it as it would have the whole data set.
So you need to change
$data['query']=$results[0];
to
$data['query']=$results;
or
$data['query']=$query->result_array();
Your code is kind of unproperly structure, I just reconstruct it and make it simple.
Hope it works.
First lets load your database lib.
Go to application/config/autoload.php
find this line then autoload database library
/*
| -------------------------------------------------------------------
| 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');
Controller View
<?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);
}
?>
Model View
<?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));
}
}
?>
View Mode
<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>