CodeIgniter Forums
Getting variables from database into library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Getting variables from database into library (/showthread.php?tid=50536)



Getting variables from database into library - El Forum - 03-29-2012

[eluser]Unknown[/eluser]
Please forgive me for my no doubt ignorance. I am just at the end of my ropes.

I am working on a project that was developed by another developer and I am just wanting to make some modifications.

All I need to do is pull variables from the database into a library.

Code:
class ClassYouTubeAPI{

  /**
  *  developerKey
  */
  
  public $developerKey = 'youtube_developer_key';
  
  /**
  *youtube username
  */
  public $username =  'youtube_id';
  
  /**
  *password
  */
  
  public $pass =   'youtube_password';

The above is directly from the library file. I just want to pull the 3 variables (youtube_developer_key,youtube_id,and,youtube_password) directly from the database. I know how to do this with a regular php mysql connection. I just can't for the life of me figure out how to accomplish this with codeingniter particularly into a library file.

Any help would be very much appreciated.

Thanks, Jereme


Getting variables from database into library - El Forum - 03-29-2012

[eluser]csotelo[/eluser]
if you use a constructor where you can get the data? for example:

Code:
public $youtube = array();

public function __construct()
{
  $CI =& get_instance();
  $CI->load->model('youtube_model');

  $this->youtube['developerKey'] = $CI->youtube_model->get_key();
  $this->youtube['username'] = $CI->youtube_model->get_user();
  $this->youtube['pass'] = $CI->youtube_model->get_pass();
}


function my_key()
{
   return $this->youtube['developerKey'];
}



Getting variables from database into library - El Forum - 03-29-2012

[eluser]Unknown[/eluser]
Thanks so much for the reply.

As I am just learning can you elaborate just a little.

Would the code that you gave me be able to go directly into the library file? Also is the database table that I am pulling from?

Again thanks so much for your help and patience with a noob.

Jereme


Getting variables from database into library - El Forum - 03-29-2012

[eluser]csotelo[/eluser]
The example can go into your class or library Youtube_api {}, please read the official manual to learn how to create libraries correctly.

$CI->load->model('youtube_model');

This loads the data model that manages your queries to the data in your database

$CI->youtube_model->get_key();
$CI->youtube_model->get_user();
$CI->youtube_model->get_pass();

Are queries that must exist in this model, which will return the data you want.

"my_key" function is an example that shows that you can use that data in any other function or need.