Welcome Guest, Not a member yet? Register   Sign In
Re use view for various purposes - Help
#11

[eluser]oliur[/eluser]
Oh yes, that looks much more efficient. So I can have something like..

Code:
function readNews(){
...
..
$data['somedata'] = $this->news_model->getAnItemByID();
..
}

function addComment(){
$this->load->view('main',$data);
}

And now main view will have the result set, is this what you are trying to say?

I'd have thought I'd need to make $data either global or public so it can be accessed from other functions within the same controller. Sorry, I guess i need to refresh my knowledge on OO.
#12

[eluser]andrewtheandroid[/eluser]
[quote author="oliur" date="1257812455"]Thanks again. Just have had a quick read on Flashdata, sounds interesting. I'll see if that works.


Well, I am already doing that. I am setting $news_row and $comment_row in my controller and sending the result to the view to be displayed.

But my question is everytime I need to load newsDetails view I need to make those database calls (in the controller). That's not very efficient, is it?[/quote]

I can't see how else you would do it unless you look into something like caching or to put all of the newsDetail information in session data then pulling it from the session data.
#13

[eluser]andrewtheandroid[/eluser]
Yeh that's exactly what i mean.

Code:
function addComment(){

// Show news detail and parse into string
$newDetail_content  = $this->load->view('newDetail',$data,true);

// Generate your form in string format
$form_content = $this->load->view('my_form_view',$data,true);

// Concat your partial views (in formatted html string)
$data['content'] = $newDetail_content.$form_content;

// Any errors here too
$data['errors'] = 'ERROR! Serious!';

// Send to your master template
$this->load->view('main',$data);

}


//---- Inside your master template main.php
<body>
<!-- blah -->

<?php if(isset($errors) { echo $errors; } ?>

<?php if(isset($content)) { echo $content; } ?>

</body>

Note i didnt include the validation statement but i think u can wrap that whole thing with the validation statement in the form_validation guide.
#14

[eluser]oliur[/eluser]
Quote:so just to paraphrase quickly. The use case is.

1. user clicks on link “My news article” ie. from a list of article headings. - correct
2. Load the view showing article details and comments. - correct
3. The user clicks add comment and types message. - correct
4. Validate message. and show appropriate view with new message or error message. - Yes but error or success message should go in between the article details and messages. So, basicaly, I am not removing anything from the artile details page once someone is adding a comment. All I am doing is either displaying a success or error note.

First of all can i suggest that you have a seperate view for adding the comment.
This way you only show the form (you can show the article deatils if you want by using a subview). I guess I was missing this bit. Didn't know you can load a subview from a view. Is that really a great idea to load view from a view?

Rest seems easy to pick. Thanks for your help. Much appreciate it.
#15

[eluser]oliur[/eluser]
[quote author="andrewtheandroid" date="1257815446"]Yeh that's exactly what i mean.

Code:
function addComment(){

// Show news detail and parse into string
$newDetail_content  = $this->load->view('newDetail',$data,true);

// Generate your form in string format
$form_content = $this->load->view('my_form_view',$data,true);

// Concat your partial views (in formatted html string)
$data['content'] = $newDetail_content.$form_content;

// Any errors here too
$data['errors'] = 'ERROR! Serious!';

// Send to your master template
$this->load->view('main',$data);

}


//---- Inside your master template main.php
<body>
<!-- blah -->

<?php if(isset($errors) { echo $errors; } ?>

<?php if(isset($content)) { echo $content; } ?>

</body>

Note i didnt include the validation statement but i think u can wrap that whole thing with the validation statement in the form_validation guide.[/quote]

Brilliant Smile thanks a ton.
#16

[eluser]oliur[/eluser]
[quote author="andrewtheandroid" date="1257815014"][quote author="oliur" date="1257812455"]Thanks again. Just have had a quick read on Flashdata, sounds interesting. I'll see if that works.


Well, I am already doing that. I am setting $news_row and $comment_row in my controller and sending the result to the view to be displayed.

But my question is everytime I need to load newsDetails view I need to make those database calls (in the controller). That's not very efficient, is it?[/quote]

I can't see how else you would do it unless you look into something like caching or to put all of the newsDetail information in session data then pulling it from the session data.[/quote]

I don't know maybe I got over excited with CI and thought there must be a different way doing it. But the outline you set looks great. Especially that formatted html string looks totally new to me.
#17

[eluser]andrewtheandroid[/eluser]
[quote author="oliur" date="1257816115"]

I don't know maybe I got over excited with CI and thought there must be a different way doing it. But the outline you set looks great. Especially that formatted html string looks totally new to me.[/quote]

hey nps. yeh i just started with CI a week ago and the string passing is what I found to be easiest for my project at the moment without using any other template libraries and you can use each view to parse your data into html string which is quite powerful i think.

I think the flash data looks like really good I think need to look into that at some point especially for error messages and notifications.
#18

[eluser]oliur[/eluser]
I have been using CI for a week now as well. But this is really fantastic.

I guess flash data is probably similar to what you see in gmail. Messages appear once you send an email and disappear within a few seconds. I might be wrong but that's what I thought when i skimmed through the doc.

Have you noticed even with codeigniter forum login, it's really that user friendly? If you provide wrong details they show you the error messages on a new page and a link back to the original form.

For my newsdetails page the case is exactly same.

Off topic: If you want to use $data that has result set populated through a model call, in another function within the same controller, would you declare it as public or global?
#19

[eluser]andrewtheandroid[/eluser]
I don't think you should use global vars if you need to pass any information you can do that through methods. What are you trying to do? I think there is a way to do it without using global vars.
#20

[eluser]oliur[/eluser]
Code:
function readNews(){

...make database calls, populate $data etc
$data = get result from the db;

}

function addComments(){
..validated
..show error or sucess msg alongside article details and comments..
therefore, make call to database again ..
$data = get result from the db

}

in my view that's redundancy. So, I was thinking

Code:
function newsDetails() {
..
$data = get result...
}

function addComment(){
use $data
}

In order to use $data in addComment do I need to declare $data in newsDetails public?




Theme © iAndrew 2016 - Forum software by © MyBB