0

I can't passing the variables in view page

this is my controller code

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Management extends CI_Controller {

        public function __construct()
       {
            parent::__construct();
            if($this->session->userdata('level') != 1){
                redirect('');
            } 
        }


        public function hotels()
        {   

                $this->load->model('model_hotels');

                $ss['most_view_hotels'] = '23';
                $this->load->view("management/header_mng_view");
                $this->load->view('management/hotels_mng_view' , $ss , true);
                $this->load->view("management/footer_mng_view");
        }

}

this is the error

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: most_view_hotels

Filename: management/hotels_mng_view.php

Line Number: 22

hotels_mng_view.php

<?php 
  echo $most_view_hotels;
  foreach($most_view_hotels as $value):
?>
  <div class="row">
  <div class="cell en">adsf</div>
  <div class="cell en">1212</div>
  <div class="cell en">12</div>
  <div class="cell en">32</div>
  </div>
<?php endforeach;?>
3
  • 2
    Show management/hotels_mng_view.php Commented Nov 1, 2012 at 12:12
  • Since most_view_hotels is not an array the foreach should display an error. But still it would not be the one given in your post. Commented Nov 1, 2012 at 13:06
  • yeah you are right , but when I want to echo it , it said: undeifined varible.. Commented Nov 2, 2012 at 11:44

4 Answers 4

1

In your controller:

$ss['most_view_hotels'] = '23';
$this->load->view("management/header_mng_view");
$this->load->view('management/hotels_mng_view',$ss);
$this->load->view("management/footer_mng_view");

In your view:

<?php echo $most_view_hotels?>
Sign up to request clarification or add additional context in comments.

1 Comment

I did check this runs correctly on my server. But I see you now added your view code to the question. $most_view_hotels is a scalar value (23), so why are you treating it as an array in your view?
0

In you're template you would refer to the item as...

$most_view_hotels

So we need to verify this is what you are doing in the view management/hotels_mng_view.php

1 Comment

That is what he is doing. "Undefined variable: most_view_hotels" The problem is the use of the third "true" argument. That returns the view as string rather than sending the view to the browser. codeigniter.com/user_guide/general/views.html
0

As said on Codeigniter manual:

There is a third optional parameter lets you change the behavior of the function
so that it returns data as a string rather than sending it to your browser.
This can be useful if you want to process the data in some way. If you set 
the parameter to true (boolean) it will return data. 
The default behavior is false, which sends it to your browser.

What this means is that your line

$this->load->view('management/hotels_mng_view' , $ss , true);

actually returns result of that view as string to php instead of sending it to browser.

The answer to your actual question ( undefined variable ), relies in in management/hotels_mng_view.php, possibly a typo.

Comments

0

I think the problem is with third parameter, ie true.

As codeigniter documentation says:

There is a third optional parameter lets ..... 
Remember to assign it to a variable if you want the data returned

And in your code you are not assigning, returned data to any code. I suggest you to try something like this:

public function hotels()
        {   

           $this->load->model('model_hotels');
           $data = array();
           $ss['most_view_hotels'] = '23';
           $data['header'] = $this->load->view("management/header_mng_view",,true);
           $data['body'] = $this->load->view('management/hotels_mng_view' , $ss , true);
           $data['footer'] = $this->load->view("management/footer_mng_view");
           $this->load->view("template",$data);
        }

 /*template view file*/
 <?php
    echo $header;
    echo $body;
    echo $footer;
 ?>

Hope this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.