I need some help structuring my php. I am trying to get 3 different searches to work. one search is a regular text search which is default and is what is in the else statement. Here is a barebones of what I am doing to get the 2 searches to work. As you can see the script checked if media "all" was selected and if not, it uses the else statement to show the default search.
if(isset($_GET['media']) &&
$_GET['media'] == 'all')
开发者_JS百科 { //Code goes here for media search for all media results }
else{ //code goes here for default search}
This works fine for the 2 searches but I have a third search I want to use but dont know how to implement it correctly.
the third search is for only media
if(isset($_GET['media']) &&
$_GET['media'] == 'only')
{ //Code goes here for media search for only media results }
Basically all I am trying to figure out is how to get all 3 searches to work. I know why the first 2 will work because the IF and ELSE but I am not exactly sure how I would add in the ONLY media search. Can anyone give me some suggestions? thanks.
This is the cleanest method, and works faster by using a switch statement instead of a conditional if/else
block. Also, you can expand it further by just adding new cases (so you don't have to re-factor your if/else
blocks in the future):
$media = isset($_GET['media']) ? $_GET['media'] : 'no_media';
switch($media) {
case 'only':
// search for only media
break;
case 'all':
// search for text and media
break;
default:
// search for just text, or the default.
break;
}
Using a switch, PHP does not have to evaluate $_GET['media']
over and over.
I think this is what you mean: You can use elseif as well.
if(isset($_GET['media']) && $_GET['media'] == 'all')
{
// All media search
}
else if(isset($_GET['media']) && $_GET['media'] == 'only')
{
// Only media search
}
else
{
// Default search
}
see here
Perhaps this:
if(isset($_GET['media']) && $_GET['media'] == 'all')
{ //Code goes here for media search for all media results }
else if(isset($_GET['media']) && $_GET['media'] == 'only')
{ //Code goes here for media search for only media results }
else
{ //code goes here for default search }
The elseif should work:
if(isset($_GET['media']) && $_GET['media'] == 'all') { //Code goes here for media search for all media results } elseif(isset($_GET['media']) && $_GET['media'] == 'only') { //Code goes here for media search for only media results } else{ //code goes here for default search }
you use the elseif(isset($_GET['media']) && $_GET['media'] == 'only')
精彩评论