CodeIgniter Forums
View files with common data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: View files with common data (/showthread.php?tid=11348)

Pages: 1 2


View files with common data - El Forum - 09-05-2008

[eluser]Body[/eluser]
Hello.
Sorry for my english and sorry if this question have been asked already - my research was not successfull. Smile

Look, i want to use some block of view-code in many parts of my website.

Imagine for simplify i have this data:
Code:
$this->load->library('RatingCounter');

$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'
                           )
           )
);

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

In this view file i have this loop:

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

    &lt;!-- USERINFO: This code i want to use in other views, i need to load it dynamic --&gt;
    Username: &lt;?=$blogEntry['User']['nickname']?&gt;<br />
    Rating: &lt;?php echo $this->ratingcounter->get_rating($blogEntry['User']['points']); ?&gt;
    &lt;!-- /USERINFO --&gt;

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

So, i want to use userinfo-block code in other pages too. How can I use this userinfo-block code in this page and in other pages? Note, that i use $this->ratingcounter->get_rating() function here, so i can't just use helpers.

Thanks for advices.


View files with common data - El Forum - 09-05-2008

[eluser]xwero[/eluser]
It would be better if you got the rating in the controller
Code:
// ...
'points' => $this->ratingcounter->get_rating('13'),
// ...
By doing this you can isolate the fetching of the data and the calling of the ratingcounter library.

For the question how to show this in multiple pages you can check out the wick library. The library makes it possible to call controllers in other controllers. So all you have to do is make a controller with a method named userratings for example.


View files with common data - El Forum - 09-05-2008

[eluser]Body[/eluser]
[quote author="xwero" date="1220616059"]It would be better if you got the rating in the controller
Code:
// ...
'points' => $this->ratingcounter->get_rating('13'),
// ...
By doing this you can isolate the fetching of the data and the calling of the ratingcounter library.
[/quote]

It's better way if my array $content['blogEntries'] is hardcoded like in my example. In real programm this array is load from database. And, if I want to include 'points' variable to this array, i need to loop it in controller for 'points' variable assignment, and then to loop it again in view-file for output. Not sure that it's good.


View files with common data - El Forum - 09-05-2008

[eluser]xwero[/eluser]
Sure it will lead to a minor performance hit but the idea is to keep the data manipulations out of the view file. View file code should only be there to display the content but if you think the performance will be affected too much i'm not stopping you to put it in the view file.


View files with common data - El Forum - 09-05-2008

[eluser]wiredesignz[/eluser]
xwero is correct, Your view should not become a controller. You dont need to iterate twice if you give this some thought.

The point is to keep display logic and business logic seperate.


View files with common data - El Forum - 09-05-2008

[eluser]wiredesignz[/eluser]
The thing about MVC is that it's not always the easiest or fastest method to do a process.

However you will be thankful if you ever need to alter a process, only one component will need to be changed, rather than all the other parts that depend on it as well.


View files with common data - El Forum - 09-05-2008

[eluser]Sumon[/eluser]
I have a block of code which might help you to construct an array in model where data are from different tables. Although we can read data from multiple table in many ways, but the following code is more useful than that. Because you can apply any logic very easily.

Here goes the controller
Code:
$Data['PhotoComments']=$this->Photo->PhotoCommentsByPhotoId($PhotoId);
$this->load->view('photo/view_photo',$Data);

Model
Code:
$query = $this->db->query("SELECT Com.*, Login.mem_id, Login.mem_primary_photo, Profile.mem_first_name, Profile.mem_last_name FROM image_gallery_image_comment Com, member_login Login, member_profile Profile WHERE Com.image_id='$PhotoId' AND Com.mem_id=Login.mem_id AND Profile.member_id=Login.mem_id");
if ($query->num_rows() > 0)
{
    $i=0;
    foreach($query->result() as $val):
            //Apply any logic here
        foreach($val as $Key=>$Value):
            $Comments[$i][$Key]=nl2br($Value);//echo $key."|"$val."<br>";
        endforeach;
        $i++;
    endforeach;
    return $Comments;
}
else
    return FALSE;

Finally View:
Code:
for($i=0;$i<count($PhotoComments);$i++)
{
  echo $PhotoComments[$i]['mem_last_name'];
}
Hope this help you.


View files with common data - El Forum - 09-05-2008

[eluser]Bramme[/eluser]
I agree with wiredesignz and xwero that you shouldn't put any logic in your views. Keep them stupid, but there is however a very easy way to solve your problem:

change your view into
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;
and your userinfo view would look like this:
Code:
&lt;!-- USERINFO:  --&gt;
    Username: &lt;?=$nickname?&gt;<br />
    Rating: &lt;?php echo $this->ratingcounter->get_rating($points); ?&gt;
    &lt;!-- /USERINFO --&gt;
&lt;?php endforeach; ?&gt;



View files with common data - El Forum - 09-05-2008

[eluser]xwero[/eluser]
Bramme that is not exactly moving the logic outside the view Smile

I'm not sure how the rating is calculated but if it involves another query with that field value he could use a subquery. Or he could store the rating in a field and just get that. I don't think there is a need to calculate it real time.


View files with common data - El Forum - 09-05-2008

[eluser]Bramme[/eluser]
[quote author="xwero" date="1220623900"]Bramme that is not exactly moving the logic outside the view Smile

I'm not sure how the rating is calculated but if it involves another query with that field value he could use a subquery. Or he could store the rating in a field and just get that. I don't think there is a need to calculate it real time.[/quote]yeah, I know that :p But it's a quick and easy (but a little dirty) solution. And it's basically all the TS asked for...