简体   繁体   中英

Not Getting Any Data From Database Codeigniter

I am still having trouble with my variables with codeigniter.

What I am after is just to be able to do this echo $this->configs->getData('config_meta_title'); and would return that value on that row

I have tried many ways but no luck. Not able to get any data?

public function getData($key) {

    // Group = Config 

    // Key Would Be Example $this->configs->getData('config_meta_title');

    // echo $this->configs->getData('config_meta_title'); returns value on this row.

    $query = $this->CI->db->get_where('setting', array('group' => 'config', 'key'=> $key))->result_array();


    if ($query->num_rows > 0) {
        return  $query->row('value');
    } else {
        return false;
    }
   }

Try this fallowing code ,It 'll work perfectly..

  $this->CI->db->select('*');
 $this->CI->db->from('settings');
 $this->CI->db->where(array('group' => 'config', 'key'=> $key));
$query = $this->CI->db->get();
  return $query->result();

I got made library file now so got it all working I had to add a few things now just working on model to suite

Controller

if (trim($this->input->post('config_meta_title'))) {
    $data['config_meta_title'] = $this->input->post('config_meta_title');
} else {
    $data['config_meta_title'] = $this->configs->get('config_meta_title');
}

Lib

class Configs {

    private $data = array();

   public function __construct(){        
        $this->CI =& get_instance();
   }

    public function get($key) {

   if ($key) {

      $value = $this->get_config($key);

      if ($value) {

         return $value;

      } else {

         return false;

      }

      } else {

         return false;

      }

        return (isset($this->data[$key]) ? $this->data[$key] : null);
    }

    public function set($key, $value) {
        return $this->set_key($key, $value);
    }

    public function has($key) {
        return isset($this->data[$key]);
    }

    function get_config($key) {
      if ($key) {

      $data = $this->CI->db->get_where('setting', array('group' => 'config', 'key' => $key))->result_array();

      if (!empty($data)) {

      return $data[0]['value'];

      } else {

      return false;

      }

      } else {

         return false;

      }
   }

   function set_key($key, $value) {

      $checkExitKey = $this->CI->db->get_where('setting', array('group' => 'config', 'key' => $key))->result_array();

      if (!empty($checkExitKey)) {

         return $this->CI->db->update('setting', array('value'=>$value), array('group' => 'config', 'key' => $key));  

      }  else {

         $data = array(
            'website_id' => 0,
            'group' => 'config',
            'key' => $key,
            'value' => $value
         );

         return $this->CI->db->insert('setting', $data);
      }
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM