[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>