Welcome Guest, Not a member yet? Register   Sign In
Array To String error / undefined $query
#1

I'm learning CodeIgniter 3 and have come to a halt because of a (probably) stupid thing I did. Could you help me in identifying the issue in my code?
I'm trying to display some rows of data from a database and I get this error
Quote:A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: core/MY_Controller.php Line Number: 24 Backtrace:
File: /public_html/siiga/application/core/MY_Controller.php Line: 24 Function: _error_handler
File: /public_html/siiga/application/core/MY_Controller.php Line: 45 Function: render
File: /public_html/siiga/application/controllers/admin/Docs.php Line: 74 Function: render
File: /public_html/siiga/index.php Line: 315 Function: require_once
My Controller
Code:
 public function existing() // Recieved, Unsolved
 {
     $this->load->database();
     $this->load->model('Docs_model');
     $this->load->library('table');
     $data['query'] = $this->Docs_model->viewexisting();
     $this->render('admin/docs/existing_view', $data);
 }
My Model
Code:
public function viewexisting()
{
   $query = $this->db->query("SELECT * FROM docs");
   return $query->result();  
}
My View
Code:
<?php foreach($query as $row){?>
<table>
   <tr>
       <td><?php echo $row->numar_inreg ;?></td>
       <td><?php echo $row->nume_doc ;?></td>

<?php }?>

Help? Please?
Reply
#2

The error seems to be in your MY_Controller file, which is most likely in your application/core folder.
Please check what it does at line 24.
Reply
#3

Your return your query as an object not an associated array change $query->result() to $query->result_array()
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(08-14-2016, 03:31 PM)InsiteFX Wrote: Your return your query as an object not an associated array change $query->result() to $query->result_array()
Did so, got this error.



Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: docs/existing_view.php

Line Number: 3

Backtrace:

File: /home/codmol/public_html/siiga/application/views/admin/docs/existing_view.php
Line: 3
Function: _error_handler

File: /home/codmol/public_html/siiga/application/core/MY_Controller.php
Line: 23
Function: view

File: /home/codmol/public_html/siiga/application/core/MY_Controller.php
Line: 45
Function: render

File: /home/codmol/public_html/siiga/application/controllers/admin/Docs.php
Line: 73
Function: render

File: /home/codmol/public_html/siiga/index.php
Line: 315
Function: require_once

Fatal error: Call to a member function result() on null in /home/codmol/public_html/siiga/application/views/admin/docs/existing_view.php on line 3
A PHP Error was encountered

Severity: Error

Message: Call to a member function result() on null

Filename: docs/existing_view.php

Line Number: 3

Backtrace:
Reply
#5

(08-14-2016, 10:41 AM)Wouter60 Wrote: The error seems to be in your MY_Controller file, which is most likely in your application/core folder.
Please check what it does at line 24.

Code:
class MY_Controller extends CI_Controller
{
 protected $data = array();
 function __construct()
 {
   parent::__construct();
   $this->data['page_title'] = 'SIIGA';
   $this->data['before_head'] = '';
   $this->data['before_body'] ='';
 }

 protected function render($the_view = NULL, $template = 'master')
 {
   if($template == 'json' || $this->input->is_ajax_request())
   {
     header('Content-Type: application/json');
     echo json_encode($this->data);
   }
   else
   {
     $this->data['the_view_content'] = (is_null($the_view)) ? '' : $this->load->view($the_view,$this->data, TRUE);;
     $this->load->view('templates/'.$template.'_view', $this->data);
This is MY_Controller file. Line 24 is //
Code:
     $this->load->view('templates/'.$template.'_view', $this->data);
Reply
#6

Please show full content of docs/existing_view.php
Reply
#7

(08-16-2016, 12:02 AM)pdthinh Wrote: Please show full content of docs/existing_view.php
Code:
<?php

    foreach($query->result() as $row)
    {
      echo $row->numar_inreg;  // Do stuff here on each $row
    }

?>
^ this is existing view.
I tried result_array and got the $query undefined error.
Reply
#8

(This post was last modified: 08-16-2016, 03:20 AM by InsiteFX.)

If your using the $data array in your MY_Controller then you need to call it using $this->data

PHP Code:
public function viewexisting()
{
 
  $query $this->db->query("SELECT * FROM docs");
 
  return $query->result_array();  

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

In your controller, you are calling the render method like this:
PHP Code:
$this->render('admin/docs/existing_view'$data); 
Where the second parameter $data is an array of objects.

However, in your MY_Controller, the render function expects a string as the second parameter:
PHP Code:
protected function render($the_view NULL$template 'master'

That's why you are getting the array to string conversion error.
Reply
#10

(08-16-2016, 10:46 AM)Wouter60 Wrote: In your controller, you are calling the render method like this:
PHP Code:
$this->render('admin/docs/existing_view'$data); 
Where the second parameter $data is an array of objects.

However, in your MY_Controller, the render function expects a string as the second parameter:
PHP Code:
protected function render($the_view NULL$template 'master'

That's why you are getting the array to string conversion error.

Thank you for your answer! What should I do then?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB