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

(This post was last modified: 08-16-2016, 12:34 PM by Wouter60.)

There are various ways to solve this.
One way is introducing a 3rd parameter for the render function, e.g.:
PHP Code:
protected function render($the_view NULL$template 'master'$data NULL )
{
 
  if ($data$this->data $data;
 
 //etc.


From you controller, call the function with the 3 parameters:
PHP Code:
$this->render('admin/docs/existing_view'NULL$data); 

But remember to check all calls to the render function in all your controllers.
Reply
#12

(This post was last modified: 08-16-2016, 01:26 PM by CodinMoldovanu. Edit Reason: UPDATED ERRORS )

Even more errors!

Code:
A PHP Error was encountered
Severity: Runtime Notice
Message: Declaration of Admin_Controller::render() should be compatible with MY_Controller::render($the_view = NULL, $template = 'master', $data = NULL)
Filename: core/MY_Controller.php
Line Number: 47
Backtrace:
File: /home/codmol/public_html/siiga/index.php
Line: 315
Function: require_once
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: 72
Function: render
File: /home/codmol/public_html/siiga/index.php
Line: 315
Function: require_once

[b]Fatal error[/b]: Call to a member function result_array() on null in [b]/home/codmol/public_html/siiga/application/views/admin/docs/existing_view.php[/b] on line [b]3[/b]
A PHP Error was encountered
Severity: Error
Message: Call to a member function result_array() on null
Filename: docs/existing_view.php
Line Number: 3
Backtrace:
Reply
#13

(08-16-2016, 12:52 PM)CodinMoldovanu Wrote: Even more errors!

Code:
A PHP Error was encountered
Severity: Runtime Notice
Message: Declaration of Admin_Controller::render() should be compatible with MY_Controller::render($the_view = NULL, $template = 'master', $data = NULL)
Filename: core/MY_Controller.php
Line Number: 47
Backtrace:
File: /home/codmol/public_html/siiga/index.php
Line: 315
Function: require_once
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: 72
Function: render
File: /home/codmol/public_html/siiga/index.php
Line: 315
Function: require_once

[b]Fatal error[/b]: Call to a member function result_array() on null in [b]/home/codmol/public_html/siiga/application/views/admin/docs/existing_view.php[/b] on line [b]3[/b]
A PHP Error was encountered
Severity: Error
Message: Call to a member function result_array() on null
Filename: docs/existing_view.php
Line Number: 3
Backtrace:

The render() method in Admin_controller should have the same signature with render() method in MY_Controller.

In the model, use $query->result()

In the view, revise the following line:

PHP Code:
foreach ($query as $row// not $query->result() 
Reply
#14

Just make sure that the render function in the Admin_Controller has the same number and type of parameters as in MY_Controller. Right now, they are incompatible because one function has only 2 parameters, and the other one (with the same name), has 3 parameters. They must have the same structure.
Reply
#15

Code:
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: core/MY_Controller.php

Line Number: 24

Backtrace:

File: /home/codmol/public_html/siiga/application/core/MY_Controller.php
Line: 24
Function: _error_handler

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
An Error Was Encountered

Unable to load the requested file: templates/Array_view.php
This is the error.


MY_Controller

Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

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 = 'siiga_admin')
 {
   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);
   }
 }
}

class Admin_Controller extends MY_Controller
{

 function __construct()
 {
   parent::__construct();
   $this->load->library('ion_auth');
   if (!$this->ion_auth->logged_in())
   {
     redirect('admin/user/login', 'refresh');
   }
   $this->data['page_title'] = 'SIIGA';
 }

 protected function render($the_view = NULL, $template = 'siiga_admin')
 {
   parent::render($the_view, $template);
 }
}

class Public_Controller extends MY_Controller
{

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

Controller

Code:
 public function existing() // Recieved, Unsolved
 {
     $this->load->database();
     $this->load->model('Docs_model');
     $this->load->library('table');
     $this->data['query'] = $this->Docs_model->viewexisting();

     $this->render('admin/docs/existing_view', $this->data);
 }
View
Code:
<table>
<?php foreach($query as $row){?>
   <tr>
       <td><?php echo $row->numar_inreg ;?></td>
       <td><?php echo $row->nume_doc ;?></td>
   </tr>
<?php }?>
</table>
I changed $data to $this->data, should've fixed things but it didn't.
Reply
#16

Quote:Controller

Code:
 public function existing() // Recieved, Unsolved
 {
     $this->load->database();
     $this->load->model('Docs_model');
     $this->load->library('table');
     $this->data['query'] = $this->Docs_model->viewexisting();

     $this->render('admin/docs/existing_view', $this->data);
 }

PHP Code:
     //...
 
     $template 'your_template';
 
     $this->render('admin/docs/existing_view'$template); 
Reply
#17

I really think you need to redesign the render functions in both your MY_Controller class as the Admin_Controller class. Because Admin_Controller extends MY_Controller, they must have the same set of parameters.
Now, from your controller, you not only wish to pass the view and template, but also some data that has been collected from your database (via a Model). This requires a third parameter for both render functions.

Simplified example:

Controller:
PHP Code:
$data['query'] = $this->Docs_model->viewexisting();
$this->render('admin/docs/existing_view',NULL,$data);  //NULL says: use the default template 

MY_Controller (class MY_Controller AND class Admin_Controller!!!):
PHP Code:
public function render($view 'default_view'$template 'default_template'$data NULL);
$this->load->view('templates/' $template $view$data); 

$data is an array. In this example, it only contains the 'query' element, but you can pass as many elements in one $data array as you want. The $data array is automatically exploded to seperate variables when it's passed to a view.
So in the view, you can refer to $query. If $query itself is an array (in this case it's an array of objects), you can loop through it's elements with foreach(...).
Hope this will help you.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB