Welcome Guest, Not a member yet? Register   Sign In
Can't print values in "view". But I can print them ok in "controller" and "model"!
#1

[eluser]behnampmdg3[/eluser]
Hello;
I am sure this is a simple thing. I was wondering why I get this error. Many thanks.
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: p

Filename: views/hello_view.php

Line Number: 3
Controller
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hello extends CI_Controller {
public function page()
  {
   $this->load->model('logic_model');
  
   $this->load->view('header_view');
  
   $p = $this->logic_model->show_hotels();
  
   $this->load->view('hello_view', $p);
  }

}

Model
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Logic_model extends CI_Model
{
  protected $hotel_results = array();
  function show_hotels($a=NULL)
   {
    $query = $this->db->query('SELECT * FROM hotels LIMIT 10');
    foreach ($query->result() as $row)
     {
      $this->hotel_results[]= $row->hotelname;
     }
    return $this->hotel_results;
   }
}

View
Code:
<?php print_r($p);?>

Please note" Print_r($p) shows correct results in controller and model, but not in view!
#2

[eluser]jmadsen[/eluser]
if you look in the system file:

system/core/loader

at:

Code:
public function view($view, $vars = array(), $return = FALSE)
{
    return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}

you'll see that the parameter that passes the data must be an array or object.

The most typical way to pass your data would be:

Code:
$data['p'] = $this->logic_model->show_hotels();
  
   $this->load->view('hello_view', $data);

In your view you can then access $p as a variable
#3

[eluser]LuckyFella73[/eluser]
Code:
// in your controller
// ...
$data['result_hotels'] = $this->logic_model->show_hotels();
$this->load->view('hello_view', $data);

// in your view
print_r($result_hotels);

Look at the manual to get detailed information about that:
http://ellislab.com/codeigniter/user-gui...views.html

Section "Adding Dynamic Data to the View"




Theme © iAndrew 2016 - Forum software by © MyBB