Welcome Guest, Not a member yet? Register   Sign In
include external php in the view through a loop and pass variables to it
#1

[eluser]Spredemann[/eluser]
OK so I have been using codeIgniter for a little more than a week and I have gotten stuck for the first time...

What I want to achieve is:

- display a user profile page with info grabbed from the db (done)
- display comments that other users have posted on the profile (trouble!!!)

The thing is... this profile page is all done in parts, where the sidebar is one thing, the main profile where the info is displayed is another, header another, and so on...

The comments are also a separate php file containing its html formatting, like a template. The reason why I'm doing so is to be able to easily change the design in one file and it will update the way comments are displayed in both profile pages, pictures, etc.

In this comment template I have variables related to the commenter information, plus the comment itself.

Ok so the main question is pretty much what the title says:

How can I include these comments in my view, through a loop, while each time sending to the include the data from the commenter and the comment itself?

ps:the loop part is not the problem, mostly the data sending and if there is any other obvious way to do this that I'm not seeing...

thanks in advance!
#2

[eluser]vbsaltydog[/eluser]
Why are you using an include to display comment data? Thats what views are for. You might want to look into view partials with HMVC.
#3

[eluser]Spredemann[/eluser]
as I said, my page is made up of parts, like the default header and footer, plus pieces like the navigation sidebar, the main content area, a right sidebar with friends and stuff.

I thought that doing so would be useful to be able to reuse the same templates over and over and when I modify the template, it will be modified everywhere. I don't know if that is a good practice or common as I am also kind of new to php, coming from an actionscript background.

Anyway, as the comments are right in the middle of the html code, I thought that I would put some php code into the view itself to loop through the data sent by the controller containing a multidimensional array of comments... Is that a good idea or is there another way that I'm missing???

The header and footer are being called by the controller though..

I have found that doing a
Code:
$this->load->view ('viewpath', $data)
also works in the view.. Now the question is... is this good practice? is this how people do?! I'm lost!
#4

[eluser]vbsaltydog[/eluser]
Use a template library.
#5

[eluser]CroNiX[/eluser]
Code:
//get our header data
$header_data = array('title' => 'My Page Title');

//assign the output of the header view to a variable
$data['header'] = $this->load->view('header', $header_data, TRUE);

//get our comment data for the user
$comment_data = $this->comment_controller->get_comments($user);
//assign the output of that to a variable
$data['comments'] = $this->load->view('comment_view', $comment_data, TRUE);

//now send both "views" to our master template and immediately output:
$this->load->view('master_template', $data);

//"master_template.php"
<div id="header">&lt;?php echo $header; ?&gt;</div>
//...blah blah...

//now cycle through the comments data and output them
<div id="comments">
&lt;?php foreach($comments as $comment): ?&gt;
<div>&lt;?php echo $comment['title']; ?&gt;</div>
<div>&lt;?php echo $comment['author']; ?&gt;</div>
<div>&lt;?php echo $comment['comment']; ?&gt;</div>
&lt;?php endforeach; ?&gt;
</div>
#6

[eluser]Spredemann[/eluser]
thanks a lot!!!

that approach looks great!

I'm gonna try it right now
#7

[eluser]TWP Marketing[/eluser]
[quote author="Spredemann" date="1328467201"]OK so I have been using codeIgniter for a little more than a week and I have gotten stuck for the first time...

What I want to achieve is:

- display a user profile page with info grabbed from the db (done)
- display comments that other users have posted on the profile (trouble!!!)

The thing is... this profile page is all done in parts, where the sidebar is one thing, the main profile where the info is displayed is another, header another, and so on...

The comments are also a separate php file containing its html formatting, like a template. The reason why I'm doing so is to be able to easily change the design in one file and it will update the way comments are displayed in both profile pages, pictures, etc.

In this comment template I have variables related to the commenter information, plus the comment itself.

Ok so the main question is pretty much what the title says:

How can I include these comments in my view, through a loop, while each time sending to the include the data from the commenter and the comment itself?

ps:the loop part is not the problem, mostly the data sending and if there is any other obvious way to do this that I'm not seeing...

thanks in advance!
[/quote]

@Spredemann
You are using partial views, which I think is a good way to handle this.
Rather than using php includes, use the CI loader class to create partial views.
Pass data to the views using the second parameter ($data) of the loader class method:

$this->load->view('file_name', $data, true/false)
NOTE THE THIRD PARAMETER!

In your controller, load the comments info (as an array) for the particular profile into the $comments_data array. You may do this with database calls to your model methods (MVC) or read them directly in the controller (not really MVC but it will work).

Code:
...
$comments_data = array(
...
$comments => array(...),
...
);

//Now create the partial page view(s) and save them as vars in the data array, NOTE THE THIRD PARAMETER:
...
$data = array(); // page data array
$data['header_view']   = $this->load->view['header_view', $header_data, TRUE );
$data['comments_view'] = $this->load->view['comments_view', $comments_data, TRUE );
$data['footer_view']   = $this->load->view['footer_view', $footer_data, TRUE );

// NOW send the whole page to the browser
$this->load->view('page_view', $data ); // NOTE THE THIRD PARAMETER IS DEFAULT FALSE HERE

In your comments_view above, loop through the $comments array in whatever order you wish and echo this to the browser.

Code:
...
foreach( $comments as $comment ){
echo ...
echo $comment['text'];
echo ...
}
...

The page_view is like your template (it is a template).
Code:
...
echo $header_view;
echo $comments_view;
echo $footer_view;
...
I apologize if this is redundant but I don't know exactly how you've coded your application.
Rather than using PHP includes, I recommend using partial views, which are explained in the user guide.
#8

[eluser]Spredemann[/eluser]
@ TWP Marketing

I see what you did there :lol: . I hadn't noticed the third parameter until now and I can really see how it is useful.

Right now there are some questions in my mind about other details related to this approach but I guess I will first try it and see for myself.

Anyway this really looks good and saves me from using the template parser which seems to make the page take longer to load as the documentation says.




Theme © iAndrew 2016 - Forum software by © MyBB