CodeIgniter Forums
Suggestions Please For Library File - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Suggestions Please For Library File (/thread-61024.html)



Suggestions Please For Library File - El Forum - 08-31-2014

[eluser]riwakawd[/eluser]
I would like to know what would be best way to be able to get my key column and my value column and have it so I could do something like

key would be example: item_keywords
value would be the content i.e. 'webdesign, movies' example
Code:
if (null !== ($this->input->post(some_key'))) {
  
   $data['some_key'] = $this->input->post('some_key');

} else {

// On view would display "content of value" selected from database.

   $data['some_key'] = $this->item->get('some_key');

}

I have these codes below but does not get the key and value.

Code:
private $data = array();

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

public function get($key) {
   return (isset($this->CI->data[$key]) ? $this->CI->data[$key] : null);
}

public function set($key, $value) {
   $this->CI->data[$key] = $value;
}

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

Any suggestions please.


Suggestions Please For Library File - El Forum - 08-31-2014

[eluser]Tim Brownlaw[/eluser]
$this->input->post returns a value or FALSE...(Read the User guide to check the Return Values on things like this...) unless you are testing that elsewhere and setting it to null when it's False ie does not exist!!!

Code:
if ($this->input->post('some_key') !== FALSE) {
// Hopefully we are performing some validation as we're just grabbing the post value here
   $data['some_key'] = $this->input->post('some_key');
} else {
// On view would display "content of value" selected from database.
// What do we do if 'some_key' does not exist in the database? NULL is returned
   $data['some_key'] = $this->item->get('some_key');
}

Where/How is $data being populated... You refer to a database so I'm guessing you load this up from somewhere else using your set method or another you've not shown here.
I am also guessing that this is your item library you are calling from the above code and that the private property that you have declared... $data is the one you are really wanting to refer to!

Code:
private $data = array();
function __construct() {
}
public function get($key) {
   return isset($this->data[$key]) ? $this->data[$key] : null;
}
public function set($key, $value) {
   $this->data[$key] = $value;
}
public function has($key) {
   return isset($this->data[$key]);
}