Welcome Guest, Not a member yet? Register   Sign In
passing data to a custom library
#3

[eluser]fesweb[/eluser]
[quote author="skaterdav85" date="1285133410"]
Code:
$url = "http://tinygeocoder.com/create-api.php?q=Perris,CA";
$this->load->library('RemoteConnector', $url);
[/quote]
This is my approach, cribbed from some CI libraries.
Library code:
Code:
// set a bunch of vars for use throughout the library
class Courses_lib {
    var $url    = FALSE;
    var $more_info     = array(
                    'id' => 0,
                    'text' => FALSE,
                    'description' => ''
                );    
// use the constructor to run the initialization function
// pass an array, or nothing, when you load the library
    function __construct($params = array())
    {
        if (count($params) > 0)
        {
            $this->initialize($params);        
        }
    }
// any item in the array can be a string, or an array
// any items passed in the array will overwrite the default var
// all other vars will contain their original value
    function initialize($params = array())
    {
        if (count($params) > 0)
        {
            foreach ($params as $key => $val)
            {
                if (isset($this->$key))
                {
                    $this->$key = trim($val);
                }
            }
        }
    }
// simple method
        function link_link()
        {
            if($this->url != FALSE)
            {
                $output = '<a >url.'">Link</a>';
            }
            else
            {
                $output = 'No URL provided';
             }
             return $output;
        }
// similar, but different method
        function url_link()
        {
            if($this->url != FALSE)
            {
                $output = '<a >url.'">'.$this->url.'</a>';
            }
            else
            {
                $output = 'No URL provided';
             }
             return $output;
        }
In the controller, you can load the library with or without passing params, and you can the re-initialize within the same controller.
Code:
$this->load->library('courses_lib');
echo $this->courses_lib->link_link(); // outputs "No URL provided"

// you can re-initialize and run the method again
$data['url'] = 'http://www.whatever.com';
$this->courses_lib->initialize($data);
echo $this->courses_lib->link_link();
// outputs "<a href="http://www.whatever.com">Link</a>
echo $this->courses_lib->url_link();
// outputs "<a href="http://www.whatever.com">http://www.whatever.com</a>


Messages In This Thread
passing data to a custom library - by El Forum - 09-21-2010, 06:30 PM
passing data to a custom library - by El Forum - 09-21-2010, 07:52 PM
passing data to a custom library - by El Forum - 09-21-2010, 08:25 PM
passing data to a custom library - by El Forum - 09-22-2010, 08:19 AM
passing data to a custom library - by El Forum - 10-08-2010, 07:20 PM
passing data to a custom library - by El Forum - 08-05-2011, 03:06 PM
passing data to a custom library - by El Forum - 08-05-2011, 05:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB