[eluser]zoreli[/eluser]
Hi
First of all, I would like to thanks this community for pointing me in right direction. Special thanks goes to
Joost Van Veen from codeigniter.tv, and to member of this forum,
Mauricio de Abreu Antunes
I am trying to accomplish some tasks keeping the same logic, but I need some help here.
I have the following problem. Very often, I will need to use join, and I guess, that I will have to write new method to my_model, but simply said, I am not on that level yet, so i need help in writing this new method. Here is what I am trying to accomplish:
Code from my_model:
Code:
class MY_Model extends CI_Model
{
public $table_name = '';
public $primary_key = '';
public $primaryFilter = 'intval'; // html entitites for string keys
public $order_by = '';
public function __construct()
{
parent::__construct();
}
public function get($ids = FALSE)
{
// Set the flag - if we passed signle id we should return a single record
$single = $ids == FALSE || is_array($ids) ? FALSE : TRUE;
// Limit the results to one or more ids
if($ids !== FALSE)
{
// $ids should be always be an array
is_array($ids) || $ids = array($ids);
// sanitize ids
$filter = $this->primaryFilter;
$ids = array_map($filter, $ids);
$this->db->where_in($this->primary_key, $ids);
}
// Set order by if it was not already set
count($this->db->ar_orderby) || $this->db->order_by($this->order_by);
// Return the results
$single == FALSE || $this->db->limit(1);
$method = $single ? 'row_array' : 'result_array';
return $this->db->get($this->table_name)->$method();
}
public function get_by($key,$val = FALSE, $orwhere = FALSE, $single = FALSE)
{
// Limit the results
if(! is_array($key))
{
$this->db->where(htmlentities($key), htmlentities($val));
} else {
$key = array_map('htmlentities',$key);
$where_method = $orwhere = TRUE ? 'or_where' : 'where';
$this->db->$where_method($key);
}
// Return the results
$single == FALSE || $this->db->limit(1);
$method = $single ? 'row_array' : 'result_array';
return $this->db->get($this->table_name)->$method();
}
public function get_key_value($key_field,$value_field,$ids = FALSE)
{
// get records
$this->db->select($key_field . ', '. $value_field);
$result = $this->get($ids);
// turn results into key=>value pair array
$data = array();
if(count($result) > 0)
{
if($ids != FALSE && ! is_array($ids))
{
$result = array($result);
}
foreach($result as $row)
{
$data[$row[$key_field]] = $row[$value_field];
}
}
return $data;
}
public function get_assoc($ids = FALSE)
{
$result = $this->get($ids);
// turn results into an associative array
if($ids != FALSE && ! is_array($ids))
{
$result = array($result);
}
$data = $this->to_assoc($result);
return $data;
}
public function to_assoc($result = array())
{
$data = array();
if(count($result) > 0)
{
foreach($result as $row)
{
$tmp = array_values(array_slice($row,0,1));
$data[$tmp[0]] = $row;
}
}
return $data;
}
I have the table name faq, which looks like this:
Code:
faqid int(11)
catid int(11)
question text
question_en text
answer text
answer_en text
sorder int(11)
visible tinyint
As you can imagine, the catid represent the id, and I would like to display the name of that id, which is located in faq_categories table. This is something that I will have to do very often, in products, services, news...etc, so I need to write a method for that.
Anyone can help me with instruction & examples how to write such methods, using same logic that is already used in my_model?
Regards, Zoreli