Xtremer360 Posted November 7, 2012 Share Posted November 7, 2012 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 https://forums.phpfreaks.com/topic/270434-adjusting-for-multiple-function-parameters/ Share on other sites More sharing options...
Andy123 Posted November 9, 2012 Share Posted November 9, 2012 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 https://forums.phpfreaks.com/topic/270434-adjusting-for-multiple-function-parameters/#findComment-1391318 Share on other sites More sharing options...
Xtremer360 Posted November 12, 2012 Author Share Posted November 12, 2012 Here's a better idea I have this now and want to find out how I can return a result when its dealing with an array. http://pastebin.com/wurNpLju Link to comment https://forums.phpfreaks.com/topic/270434-adjusting-for-multiple-function-parameters/#findComment-1391891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.