CodeIgniter Forums
View functions accessing viewdata - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: View functions accessing viewdata (/showthread.php?tid=22913)



View functions accessing viewdata - El Forum - 09-23-2009

[eluser]Unknown[/eluser]
In my view, I am trying to keep things simple by writing some functions to do some common things.

I have something like this, where $viewdata[sortcurrent] is passed from my controller:

Code:
function sorted_by( $sortnumber, $displaytext ) {
   if ( $sortnumber == $sortcurrent ) {
   ...
   }
}

Even if I add global $sortcurrent, I still can't seem to access that variable from the function contained in my view. (It isn't exactly a global, but it is local in scope.)

To work around this, I ended up using:
Code:
function sorted_by( $sortnumber, $displaytext, $sortcurrent )

and everywhere sorted_by is called, I pass in $sortcurrent. This works, but is there a better or easier way to access the $sortcurrent from inside a function?


View functions accessing viewdata - El Forum - 09-23-2009

[eluser]InsiteFX[/eluser]
var $viewdata = array();

Try that.

Enjoy
InsiteFX


View functions accessing viewdata - El Forum - 09-24-2009

[eluser]bigtony[/eluser]
You should avoid putting function definitions in a view. Put it in a helper instead and then get the view to invoke the function (remember to load the helper in the controller) :
Code:
// This is in the view
<?php echo sorted_by($sortnumber, $displaytext); ?>
The above should be able to read your viewdata.