Welcome Guest, Not a member yet? Register   Sign In
Help: How i show variables from different functions on the same view?
#1

[eluser]Unknown[/eluser]
How i show variables from different function on the same view?
I have variables in different functions and i wanna show them in only one view ... how can i do that?

Thanks in advance
#2

[eluser]Aken[/eluser]
Can you be more specific? Any variables you want to pass through to a view should be defined in an array, and passed as the second parameter when loading a view.
#3

[eluser]Unknown[/eluser]
Code:
class site extends Controller {

   function index()
   {
        $data = array(
               'main_content' => 'mypage',
           'menu_name' => 'home',
               'title' => 'titolo di mypage',
               'description' => 'descrizione di mypage',
               'keywords' => 'keywords di mypage'
          );
        $this->load->view('includes/template', $data);
   }
  
   function about()
   {
        $data = array(
               'main_content' => 'about',
           'menu_name' => 'about',
               'title' => 'titolo di about',
               'description' => 'descrizione di about',
               'keywords' => 'keywords di about'
          );
        $this->load->view('includes/template', $data);
   }
this is my controller, as you can see there is menu_name in both function ... i'd like to show them (both) in one view. Or have a function that select them in another variable, and then pass it to the view.
#4

[eluser]jedd[/eluser]
You could do it this way:
Code:
class Site extends Controller {   // Fixed case of class name

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

       $this->data = array(
                      'common_item' => 'common string here',
                      );
    }

// In your other methods you could merge_array() with a new construct, overwriting common bits,
// adding other bits, etc.  Obviously you would then pass $this->data to your views.

Note that menu_name does exist in both functions, but has a different value in both functions, so I'm not sure how you can cut down on the number of times you define that variable.


You might also want to hit the [url="/wiki/FAQ"]FAQ[/url], hit the commonly asked question 'I'm repeating lots of bits of code, is there anyway around that?' and then follow the link to my rant. Also accessible from the [url="/wiki"]main wiki[/url] front page if you follow the Approaches yellow brick path. It sounds like what you're trying to do is covered, at least in part, by that.




Theme © iAndrew 2016 - Forum software by © MyBB