[eluser]xtremer360[/eluser]
This is a reusable function that I'm going to be using elsewhere and I set it up so that it can take in a number of parameters. The question I have is for example how can I move the select clause that selects the title_id down to be not included if the $params['titles'] key is set because at that point I would already have that bit of data and want to prevent selecting it again.
Code:
public function get_titles($params = array())
{
$this->db->select('titles.title_id');
$this->db->select('titles.title_name');
$this->db->select('titles.title_directory_name');
$this->db->select('titles.title_sort_order');
$this->db->select('titles.title_status_id');
$this->db->from('titles');
//checking to see if any $params are attempting to be passed
if(count($params) > 0)
{
//start title status type selection
if (isset($params['title_status']))
{
//passed multiple status types.
//eg $params['title_status'] = array(0, 2);
if (is_array($params['title_status']))
{
$a = 0;
foreach($params['title_status'] as $status_type)
{
if ($a == 0)
{
$this->db->where('titles.title_status_id', $status_type);
}
else
{
$this->db->or_where('titles.title_status_id', $status_type);
}
$a++;
}
}
else
{
//if you only used a string with a single digit.
if (is_numeric($params['title_status']))
{
$this->db->where('titles.title_status_id', $params['title_status']);
}
}
}
//end title status type selection
//start titles sort_order specific selection
if (isset($params['title_sort_order']))
{
if (is_array($params['title_sort_order']))
{
foreach ($params['title_sort_order'] as $title_sort_order)
{
$this->db->where('titles.title_sort_order', $title_sort_order);
}
}
else
{
//if you only used a string with a single digit.
if (is_numeric($params['title_sort_order']))
{
$this->db->where('titles.title_sort_order', $params['title_sort_order']);
}
}
}
//end titles sort_order specific selection
//start titles specific selection
if (isset($params['titles']))
{
if (is_array($params['titles']))
{
foreach ($params['titles'] as $title)
{
$this->db->where('titles.title_id', $title);
}
}
else
{
//if you only used a string with a single digit.
if (is_numeric($params['titles']))
{
$this->db->where('titles.title_id', $params['titles']);
}
}
}
//end titles specific selection
}
//if no params are found query will be made for all titles in the DB regardless it would be like
// SELECT * FROM titles;
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->result();
}
else
{
return false;
}
}