• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Get config values from DB

#11
[eluser]Pascal Kriete[/eluser]
As long as the rest of the application is well designed and the server is set up correctly, it shouldn't be a problem.

#12
[eluser]Colin Williams[/eluser]
The only tax on your server would be the extra query needed to load the config items from the DB. I don't see why the controller needs to be extended at all. If you have a library being autoloaded, you're going to have access to the config items via the library at any time.

#13
[eluser]internut[/eluser]
Can anybody lend a hand on this error after creating a library:

Library: Load_Settings.php

Called using:

$this->load->library('Load_Settings');

Which works fine until I get into a database call.

Error:

load settings gets printed here
Fatal error: Call to a member function query() on a non-object in C:\Program Files\WebServer\Abyss Web Server\htdocs\php\NEW\admin\system\application\libraries\Load_Settings.php on line 8

/libraries/Load_Settings.php:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Load_Settings {

   function Load_Settings() {

      echo "load settings gets printed here";
      $my_config = $this->db->query('SELECT * FROM settings');

      if($my_config->num_rows() > 0) {
         foreach($my_config->row() as $key=>$value) {
            $this->config->set_item($key, $value);
         }
      return true;
      }

      else {
         return false;
       }
    }
}

?>

database is on autoload and works fine. Just not here so I must be missing something.

Help greatly appreciated.

#14
[eluser]Pascal Kriete[/eluser]
Libraries don't extend the CI super object, so you need to grab the reference:
Code:
$CI =& get_instance();

// Now use $CI instead of $this for native libraries
$CI->db->query...

#15
[eluser]internut[/eluser]
Ok going to make the changes. I appreciate your response. Is that the same for models?

Would it be better to do this as a model or library or same?

#16
[eluser]internut[/eluser]
Thanks Inparo making progress.

Why am I only getting the first result from the settings table?

Structure:

Table: settings --> Fields name & setting:


Code:
function Load_Settings() {

      $CI =& get_instance();

      $data['query'] = $CI->db->query("SELECT * FROM settings");     // query time

      if($data['query']->num_rows() > 0) {
         foreach($data['query']->row() as $key=>$value) {
            $CI->config->set_item($key, $value);
            echo "--&gt; $key --&gt; $value<br>\n";
         }
      return true;
      }

      else {
         return false;
       }

Its returning the first record:

--&gt; name --&gt; admin_url
--&gt; setting --&gt; http://www.your-domain.com/mm2/admin.php


Trying to figure out why its not cycling thru them all.

#17
[eluser]internut[/eluser]
Ok I'm switched up to this:

Code:
class Load_Settings {

   function Load_Settings() {

      $CI =& get_instance();

      $query = $CI->db->get('settings');

      foreach ($query->result() as $row) {
          echo "$row->name = $row->setting<hr size=1><br>";

          $CI->config->set_item($row->name, $row->setting);
      }
    }
}

I'm getting all results from the "settings" table.

I'm curious if the code:

Code:
$CI->config->set_item($row->name, $row->setting);

is setting the name = setting variables into $config ?

there is a name = setting of:

username = xxx

Can this username config be called all over now? If so how?

$config[username] ?

#18
[eluser]flojon[/eluser]
[quote author="internut" date="1215503453"]
...
there is a name = setting of:

username = xxx

Can this username config be called all over now? If so how?

$config[username] ?[/quote]

You can get the config values by using something like this:
Code:
$this->config->item('username');

#19
[eluser]internut[/eluser]
flojon!

Nice man. Appreciate that one.

#20
[eluser]flojon[/eluser]
You're welcome! :-)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.