Jump to content

Adjusting For Multiple Function Parameters


Xtremer360

Recommended Posts

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. That way I can also adjust it if the other information is already known.

 

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;
}
}

Link to comment
Share on other sites

I guess you can add conditional statements (if statements) to test if certain data is provided like this:

 


$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');

if (empty($params[‘titles’])) {
$this->db->select('titles.title_id');
}

$this->db->from('titles');

// Rest of your code

 

Is that what you are after?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.