Welcome Guest, Not a member yet? Register   Sign In
Problem passing data to my library from controller to run common functions
#1

[eluser]Unknown[/eluser]
Hello. I'm frustrated, and I'm sure it's me, because I'm new to CI. Help would be appreciated.

Background:
I have the infrastructure of the site and the various pages displaying nicely but I want to work on being DRY (Don't Repeat Yourself) so I want to start using my own library for common code.

I have one controller for each view and I'm utilizing several templates that result in a different look, when desired. All is good but I now want to 'move' my controller specific, yet virtually identical, code to my library.

Info I'm attempting to move from controller to library, for now:
-Banner information (which banner to display, img location, id, link, etc.)
-Other images that are used in header, but change depending on webpage or theme
-May move meta information control from controller to db

Problem:
I'm attempting to pass two simple variables from the controller to my library. I use an array, as outlined in the users guide (& other sources), but I keep getting the following error:

Message: Undefined variable: bannernumber
Filename: libraries/banner.php


Some controller code:
Code:
$params = array('bannernumber' => '2', 'badgenumber' => '1');
$this->load->library('banner', $params);
$this->banner->select($params);

Some banner code:
Code:
class Banner {

function select($params)
{

  $bn = $this->$bannernumber;

  // Code to choose img, set id for CSS, set link

  }

}

I know it's all me but I thought it would be wise to get another set of eyes on it to let me know what thing, or things, I need to rectify.

Your suggestions are welcome.

Thanks!


#2

[eluser]MasterHernan[/eluser]
Your banner code:
Code:
class Banner {

function select($params)
{

  $bn = $this->$bannernumber;

  // Code to choose img, set id for CSS, set link

  }

}

Fixed code:
Code:
class Banner {

function select($params)
{

  $bn = $params['bannernumber'];

  // Code to choose img, set id for CSS, set link

  }

}

You were sending the array to the function and the function was receiving it as a private function variable, the object variable that you are looking for was not defined. You don't need to pass the $params again if you grab them in the __construct function and save it to the object since you are submitting it when you call the library.

For more information check out:
http://ellislab.com/codeigniter/user-gui...aries.html
#Passing Parameters When Initializing Your Class




Theme © iAndrew 2016 - Forum software by © MyBB