CodeIgniter Forums
Model query with two possible inputs - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Model query with two possible inputs (/showthread.php?tid=2359)



Model query with two possible inputs - El Forum - 07-31-2007

[eluser]kirkaracha[/eluser]
I have a list of countries I can access publicly or via a password-protected admin backend. I want to access the countries by URL name from the front end for SEO/usability reasons, and I want to access them by ID on the backend (in case I need to edit the URL name). Here's the query from my model:
Code:
function get_country_info($country_id = null,$country_url_name = null) {
    $this->db->select('
        country_id,
        country_name,
        country_url_name
    ');
    $this->db->from('countries');
    if($country_id != null){
        $this->db->where('country_id',$country_id);
        $this->db->limit('1');
    }
    if($country_url_name != null){
        $this->db->where('country_url_name',$country_url_name);
        $this->db->limit('1');
    }
    return $this->db->get();
} // get_country_info
This works fine if I use ID, but doesn't return any records when I use the URL name. Is there something wrong with my model? How should I do this?

Thanks.


Model query with two possible inputs - El Forum - 07-31-2007

[eluser]Michael Wales[/eluser]
Everything looks right to me - are you calling the function correctly?
Code:
$country = get_country_info(null, 'my_url_name');



Model query with two possible inputs - El Forum - 07-31-2007

[eluser]obiron2[/eluser]
alternatively reconfigure the function to take $param_type and $param_ value and pass through whether you are selecting by ID or URL_name in param type. Obviously this is only practical if you have only one parameter.

Or create two functions...


Model query with two possible inputs - El Forum - 07-31-2007

[eluser]kirkaracha[/eluser]
walesmd's suggestion fixed it. (I was setting $country_name to null and calling it with both $country_name and $country_id.)