CodeIgniter Forums
how do I prevent error display if missing argument in URL? - 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: how do I prevent error display if missing argument in URL? (/showthread.php?tid=33522)



how do I prevent error display if missing argument in URL? - El Forum - 08-29-2010

[eluser]netrox[/eluser]
I noticed that if I have this URL like this

www.example.com/profile/id/4

it will display the profile of 4.

But if someone enters with this URL

www.example.com/profile/id

Where id function is missing an argument, it generates an error. I don't want that error displayed at all. I tried "(!isset($id))" condition but it generated the error before it could reach that condition.

How do I stop that? And redirect the page back to the index function of the given class?


how do I prevent error display if missing argument in URL? - El Forum - 08-29-2010

[eluser]Joseph Wensley[/eluser]
You can try giving it a default, use something like

Code:
function id($id = NULL)
{
    if($id === NULL){
        //redirect
    }
}



how do I prevent error display if missing argument in URL? - El Forum - 08-29-2010

[eluser]netrox[/eluser]
Thank you! That worked. Smile


how do I prevent error display if missing argument in URL? - El Forum - 03-04-2011

[eluser]BandonRandon[/eluser]
I know this is a little bit of an older post but I am using CI 2.0 and found this method to be simpler. This was the first post I found when searching how to do this so I thought I whould share how I'm doing it.
Code:
public function my_function($id='') //set the field here so it won't return false
    {    
        if($id =='') // if it's empty
        {
            //redirect or show content if empty here
        }
    }

Hope this helps