0

I have a model method which returns time and view to pass the data. But when I pass the data I get Undefined variable error message.

$this->load->model('Driver_model');
$result = $this->Driver_model->get_shift_data($id);

This is the method calling and if I use var_dump($result). I get following result.

array (size=1)
  0 => 
    object(stdClass)[26]
      public 'start_time' => string '2016-01-22 05:32:42' (length=19)

But if I send the $result to the view, I get error.

$this->load->view('driver/report_view.php', $result);

My question is, how it possible to work in controller if I don't load the view. But if I send the data, it doesn't identify the variable.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: result

Filename: driver/report_view.php

Line Number: 52

Backtrace:

File: C:\wamp\www\cabs\application\views\driver\report_view.php
Line: 52
Function: _error_handler

File: C:\wamp\www\cabs\application\controllers\Driver.php
Line: 44
Function: view

File: C:\wamp\www\cabs\index.php
Line: 292
Function: require_once

I have tested it using var_dump($result) in view as well.

      <div class="panel-body">
Line 52    <?php var_dump($result); ?>                              
        <form>
Isuru
  • 3,818
  • 13
  • 49
  • 64

1 Answers1

0

You can not pass a simple variable $result in load view function for view.

You need to create an associative array for sending data to view from controller. You can use as:

$this->load->model('Driver_model'); $data['result'] = $this->Driver_model->get_shift_data($id);

Now send $data array to your view as:

$this->load->view('driver/report_view.php', $data);

Now you can use var_dump() in view file as:

<?php var_dump($result); ?>

Note that if you want to send any other data from controller to view you can follow the same example.

For more explanation see this example:

$data['result'] = 1;
$data['result2'] = 2;
$data['result3'] = 3;

$this->load->view('yourview', $data);

In view you can get data as:

echo $result; // 1
echo $result2; // 2
echo $result3; // 3
devpro
  • 16,184
  • 3
  • 27
  • 38
  • When I do it your way I get A PHP Error was encountered Severity: Notice Message: Array to string conversion. What is wrong with it? – Isuru Jan 26 '16 at 18:09
  • @isuru bcoz u r using array as a string. Use Print_r() and this is not my way.. its codeginitor standard way – devpro Jan 26 '16 at 19:12