Welcome Guest, Not a member yet? Register   Sign In
Adjusting for already have an id
#1

[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;
}
}
#2

[eluser]CroNiX[/eluser]
By using the exact same logic that you mentioned in your post, in your code...
Code:
public function get_titles($params = array())
{
  if ( ! isset($params['titles']))
  {
    $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');

//...
}
#3

[eluser]xtremer360[/eluser]
Great however I have one more addon to this question. The question I have is if you notice with the line below its supposed to get the status name of the title. However in the get_title_statuses function it returns an object so I'm trying to figure out how when I need that value I can just return the name.

Code:
$titles = $this->titles_model->get_titles(array('title_status' => array(1,2,3)));
    
for($x = 0; $x < count($titles); $x++)
{
    $titles[$x]->title_status_name = $this->titles_model->get_title_statuses(array('titles' => $titles[$x]->title_status_id));
}

Since the code extended here's the pastebin

http://pastebin.com/h6XZvr4s
#4

[eluser]xtremer360[/eluser]
Any ideas?
#5

[eluser]xtremer360[/eluser]
*bump*




Theme © iAndrew 2016 - Forum software by © MyBB