Welcome Guest, Not a member yet? Register   Sign In
Help with helpers
#1

[eluser]gmask[/eluser]
Alright so I have this simple db query:
Code:
// Get global settings
$query = $this->db->get('settings');
if ($query->num_rows() > 0)
{
       $row = $query->row_array();
       $settings['site_title'] = $row['title'];
       $settings['site_tagline'] = $row['tagline'];
       $settings['site_location'] = $row['location'];
       $settings['site_comments'] = $row['comments_enable'];
}
Because this information is going to be called in most of my functions, and every single one of my views, I want to stop repeating myself. How can I put it into a helper (if that's even what I should do) to make it so I don't have to repeat the code for all of my functions (which then pass it to the view)?
#2

[eluser]Met[/eluser]
you can extend the default controller and place it in there, or as you say, you can put it in a helper.

Set the helper to autoload, then simply

Code:
//if not autoloading ~
$this->load->helper('my_helper');

$foo=$this->my_helper->my_function();

//etc
#3

[eluser]gmask[/eluser]
Hi Met,
I tried what you suggested, but something's gone wrong. My helper:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

function settings()
{
    // Get global settings
    $CI = & get_instance();
    $query = $CI->db->get('settings');
    
    if ($query->num_rows() > 0)
    {
        $row = $query->row_array();
        $settings['site_title'] = $row['title'];
        $settings['site_tagline'] = $row['tagline'];
        $settings['site_location'] = $row['location'];
        $settings['site_comments'] = $row['comments_enable'];
    }
    return $settings;
}

?>
My controller:
Code:
function index()
    {
        $settings = $this->snappy->settings();
        $this->load->view('index', $settings);
    }
But it returns this error:
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: Blog::$snappy

Fatal error: Call to a member function settings() on a non-object in ...\controllers\blog.php on line 22
#4

[eluser]gyo[/eluser]
Generally helpers are not Classes, they are just procedural functions to be used everywhere.

Code:
$this->load->helper('my_helper');

$my_settings = settings();
#5

[eluser]gmask[/eluser]
Right, that's why I thought making a helper with this function would be the best way to do this.

Could you suggest a better way? Or is there something I should change about the helper I have now?

Thanks very much for your help.
#6

[eluser]Met[/eluser]
sorry my example probably confused you. posted it as $this->my_helper->my_function out of habit more than anything.

yes, helpers are just functions rather than a class, so as gyo / suashi posted, should do the trick nicely.
#7

[eluser]gmask[/eluser]
Thanks a ton, guys!
#8

[eluser]gyo[/eluser]
As I see it, settings() should be in a model.

Helpers are more intended to be used in views (or if you need a simpler approach rather than object oriented).
#9

[eluser]InsiteFX[/eluser]
What's wrong with using a MY_Controller?

InsiteFX
#10

[eluser]gmask[/eluser]
[quote author="InsiteFX" date="1283288569"]What's wrong with using a MY_Controller?[/quote]
I don't understand.
Quote:As I see it, settings() should be in a model.

Helpers are more intended to be used in views (or if you need a simpler approach rather than object oriented).
Ok I see what you mean. I made the following changes:
my helper:
Code:
function settings()
{
    // Get global settings
    $CI = & get_instance();
    $query = $CI->db->get('settings');
    
    if ($query->num_rows() > 0)
    {
        $row = $query->row_array();
        $site['title'] = $row['title'];
        $site['tagline'] = $row['tagline'];
        $site['location'] = $row['location'];
        $site['comments'] = $row['comments_enable'];
    }
    return $site;
}
My view:
Code:
<?php $setting = settings(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8"&gt;
    &lt;title&gt;&lt;?php echo $setting['title']; ?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<div id="header">
    <a href="&lt;?php echo $setting['location']; ?&gt;"><img src="logo.jpg" /></a>
    <p>&lt;?php echo $setting['tagline']; ?&gt;</p>
</div>
And it all works. Is this the way you would do it? Essentially I just want to have a set of variables that I can call globally (because they are global blog settings).

Is there a way to do this better? Or at least a way to make the variables prettier? I'd like to be able to just call one variable for each, instead of the array thing. Like $site_title for the site title, etc.




Theme © iAndrew 2016 - Forum software by © MyBB