Welcome Guest, Not a member yet? Register   Sign In
Passing data to View
#1

[eluser]Unknown[/eluser]
Hi, apologies for the simple post but I'm trying to pass the following data to view for a calendar:
$data = array (
'title' => "Calendar Application",
$datarray = array (
3 => 'http://example.com/news/article/2006/03/',
7 => 'http://example.com/news/article/2006/07/',
13 => 'http://example.com/news/article/2006/13/',
26 => 'http://example.com/news/article/2006/26/'
)
);

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

The view.php file contains the following line:

<?php echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4), $datarray); ?>

How do I get $datarray to be used by the view?
#2

[eluser]Colin Williams[/eluser]
Code:
$data = array (
    ‘title’ => “Calendar Application”,
    'datarray' => array (
          3 => ‘http://example.com/news/article/2006/03/’,
          7 => ‘http://example.com/news/article/2006/07/’,
          13 => ‘http://example.com/news/article/2006/13/’,
          26 => ‘http://example.com/news/article/2006/26/’
        )
      );

I can't believe your original code didn't throw a syntax error
#3

[eluser]Unknown[/eluser]
Doh!, silly errors, thanks for the help. Now works fine.
#4

[eluser]therealmaloy[/eluser]
Colin

i guess that's a correct array, just that he is assigning it to a variable so it is equivalent to

$datarray = array (
3 => ‘http://example.com/news/article/2006/03/’,
7 => ‘http://example.com/news/article/2006/07/’,
13 => ‘http://example.com/news/article/2006/13/’,
26 => ‘http://example.com/news/article/2006/26/’
);

$data = array (‘title’ => “Calendar Application”, $dataarray);


you could access it in the same scope and/or as long as the variable is visible using $data['title'] and $data[1][x] for your dataarray

correct me if i'm wrong. Smile

but still this would pose a problem for passing the variable to the view, the idea is that you don't have any key that would be translated to an equivalent variable in the view. the solution would be assigning a key to this.

cnother

Colin's code is very much the solution for your problem Smile
#5

[eluser]Colin Williams[/eluser]
I prefer using the compact() function instead of a $data array:

Code:
$title = 'My Title';
$events = array(1 => 'event/1', 6 => 'event/6', 23 => 'event/23');
$this->load->view('calendar', compact('title', 'events'));

This way you see $title and $events variables in both your controller and your view, and they are, of course, the same.
#6

[eluser]kyleect[/eluser]
Couldn't you just do this:

Code:
$data['title'] = "My Title";
$data['events'] = array(1 => 'event/1', 6 => 'event/6', 23 => 'event/23');

Rather than using a function do it? Could just be a personal preference if it all ends up the same way in the view.
#7

[eluser]Colin Williams[/eluser]
It is a personal preference. In fact, I was keen to use the verb, "prefer." And the reason is, when I'm looking at vars in my view file, I see $title, $events, etc. If I use a $data array in the controller, I see $data['title'], $data['events'], etc. I just find it better to see $title here and $title there, $events here and $events there.

And when you think about it, the only reason to hold these variables in an array is to please the views API. So, I prefer to just go about creating multiple local variables, then bring together the ones I need with compact(). Turns out to be more flexible. I don't need different $data arrays for different views, just different compact() calls.

And as far as I know, 2 or 3 calls to compact() for each un-cached request is not going to cripple any app.




Theme © iAndrew 2016 - Forum software by © MyBB