• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you use a model's output...

#1
[eluser]nubianxp[/eluser]
How do you use a model's output on one controller, but have it display on several views?

For example, I have a model that queries a welcome message and I want to display the data on the footer area (footer.php) so it shows up on every page, what should be the content of my controller? footer.php is called by views/index.php using <?=$this->load->view('footer')?>

Pretty trivial, but I'm just starting out with PHP, I know the first part of the controller file, but I don't know how to pass the $data to the footer.php, problem is, I get a variable undefined error.

#2
[eluser]ggoforth[/eluser]
As far as I know (and I don't know all that much) you need to pass that piece of data to the view. So if your view is being loaded in by another view, you need to pass an array with your welcome message in your controller. It would look something like this:

Code:
//your controller
<?
     class Myclass extends Controller{
          function __construct(){
              parent::Controller();
          }
          function index(){
              $data['welcome_msg'] = array('welcome'=>'my welcome message goes here');
              $this->load->view('my_view',$data);
          }
     }
?>

//your first view
<?
     //load the footer
     $this->load->view('footer',$welcome_msg);
?>

//your footer
<?
     <?=$welcome?>
?>

That should accomplish what your trying to do. You can also load your footer into a variable within your controller and then just echo it out, but I was try to stay on track with what your already doing. Either way is fine in my opinion, as long as it makes it easier for you to keep track of.

Greg

#3
[eluser]nubianxp[/eluser]
[quote author="ggoforth" date="1238898068"]As far as I know (and I don't know all that much) you need to pass that piece of data to the view. So if your view is being loaded in by another view, you need to pass an array with your welcome message in your controller. It would look something like this:
...
That should accomplish what your trying to do. You can also load your footer into a variable within your controller and then just echo it out, but I was try to stay on track with what your already doing. Either way is fine in my opinion, as long as it makes it easier for you to keep track of.

Greg[/quote]

Thanks, I think I missed this one:

Code:
//your first view
<?
     //load the footer
     $this->load->view('footer',$welcome_msg);
?>

//your footer
<?
     <?=$welcome?>
?>

cool, will try that out.
BTW, do I need to place this:
Code:
$data['welcome_msg'] = array('welcome'=>'my welcome message goes here');
on all my controllers or just the main one?

Thanks again.

#4
[eluser]ggoforth[/eluser]
Well, you need to place that piece in any controller you want it to show up on. This is not necessarily the best way to do this I might add (imho). It's going to lead to a lot of extra code that may become difficult to manage down the road (what happens when you want to change the message, etc...) You might want to look into session variables, and load the message once, and simply echo it out on your pages where ever you want it. A lot less code to manage and a bit more flexible.

Greg

#5
[eluser]nubianxp[/eluser]
Thanks and yes I was hoping on not having to put it on the other controllers, as I have a lot of them.
Anyway, I was trying to test it using the welcome message and afterward I'll be adding a news box popup
on every page, the data is loaded in from a hidden div inside the footer area and is shown as a popup.
It will contain a lot of small info (e.g. <?=$title?> <?=$blurb?> etc. ) so is it better to just load them
from another file or something? I'm a real noob, and this is the first time I've tried implementing a site
using a PHP framework, in fact using PHP for the first time since I'm more on the front-end.

Really appreciate your help.

#6
[eluser]jedd[/eluser]
Hi nubianxp.

Check out [url="http://ellislab.com/forums/viewthread/109698"]this thread[/url] - for one way of doing this.

#7
[eluser]nubianxp[/eluser]
hey jedd, thanks, will try that as well.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.