Welcome Guest, Not a member yet? Register   Sign In
View files with common data
#11

[eluser]Body[/eluser]
Thanks very much for all.

So, as i understand, the best way is to calculate rating for all blogs users in controller, and then just output all data in views (without any calculations in view file)? But how should I deal with userinfo block if i want to use it in many other pages?

Is it good practice:

My main controller now look like this:
Code:
$this->load->library('RatingCounter');

// Hardcoded. In real programm this data is fetched from database.
$content['blogEntries'] = array(
      0 => array(
                 'id' => 5,
                 'title' => 'Some title',
                 'content' => 'Some content',
                 'User' => array (
                                  'id' => 2,
                                  'nickname' => 'body',
                                  'points' => '25'
                           )
           ),
      1 => array(
                 'id' => 6,
                 'title' => 'Some title 2',
                 'content' => 'Some content 2',
                 'User' => array (
                                  'id' => 3,
                                  'nickname' => 'test',
                                  'points' => '13'
                           )
           )
);

for ($i = 0, $size = count($content['blogEntries']); $i < $size; $i++) {
    $content['blogEntries'][$i]['User']['rating'] = $this->ratingcounter->get_rating($content['blogEntries'][$i]['User']['points']);
}

$this->load->view('blog_view', $content);

Main view file:
Code:
&lt;?php foreach ($blogEntries as $blogEntry): ?&gt;
    <h1>&lt;?=$blogEntry['title']?&gt;</h1>

    &lt;?php $this->load->view('userinfo', $blogEntry['User']); ?&gt;

    <br /><br />
&lt;?php endforeach; ?&gt;

Userinfo view file:
Code:
&lt;!-- USERINFO --&gt;
    Username: &lt;?=$User['nickname']?&gt;<br />
    Rating: &lt;?=$User['rating']?&gt;
    &lt;!-- /USERINFO --&gt;

I move userinfo in a separate view file, because I want to use this userinfo in other pages.
#12

[eluser]Bramme[/eluser]
You're passing the $blogEntry['User'] array to your userinfo view file. The same happens when you pass $content['blogEntry'] to your blog_view. You don't access $content['blogEntry'] in your blog_view, you access $blogEntry.

So you should use $nickname and $rating in your userinfo view file.

If you want to keep using $blogEntry['nickname'] in your userinfo view file, don't pass an array in the second argument when loading the view file from your blog_entry view. Just do $this->load->view('userinfo'), the variables get passed along automatically.
#13

[eluser]Body[/eluser]
[quote author="Bramme" date="1220638731"]You're passing the $blogEntry['User'] array to your userinfo view file. The same happens when you pass $content['blogEntry'] to your blog_view. You don't access $content['blogEntry'] in your blog_view, you access $blogEntry.

So you should use $nickname and $rating in your userinfo view file.
[/quote]

I think you're not right.
Yes, when I'm passing $content['blogEntries'] to a view file, I access $blogEntries. So, when I'm passing $blogEntry['User'], i should access $User.

[quote author="Bramme" date="1220638731"]
If you want to keep using $blogEntry['nickname'] in your userinfo view file, don't pass an array in the second argument when loading the view file from your blog_entry view. Just do $this->load->view('userinfo'), the variables get passed along automatically.
[/quote]

Automatically passing is not working - error "Undefined variable: blogEntry ". :/
#14

[eluser]Bramme[/eluser]
Hmm, could always be I'm mistaken. It'd surprise me though, seeing as I've used embedded view in the past and not adding an array as second parameter for the load->view function worked fine.




Theme © iAndrew 2016 - Forum software by © MyBB