joestar Posted February 25, 2015 Share Posted February 25, 2015 Hey guys I am new to php and this forum. I need a little help with creating a script. the intended use is when a user visits a url like "myurl.com/?show=house" they are show a picture of housewives. Here is the code I have <?php $keyword=$_GET['show']; if (strpos($keyword,'house') !== false) { $image="housewives.jpg"; } if (strpos($keyword,'mickey') !== false) { $image="mickey.jpg"; } else { $image = "default.jpg"; } ?> the issue I a having is default.jpg is always shown to the user unless the url is "myurl.com/?show=mickey" if anyone know how to solve this issue please help a noob out. Link to comment https://forums.phpfreaks.com/topic/294878-help-with-a-simple-script-that-switches/ Share on other sites More sharing options...
requinix Posted February 25, 2015 Share Posted February 25, 2015 The else is attached to the second if, and only that. Since $keyword doesn't contain "mickey", it executes. You need an else if: <?php $keyword=$_GET['show']; if (strpos($keyword,'house') !== false) { $image="housewives.jpg"; } else if (strpos($keyword,'mickey') !== false) { $image="mickey.jpg"; } else { $image = "default.jpg"; } ?> Link to comment https://forums.phpfreaks.com/topic/294878-help-with-a-simple-script-that-switches/#findComment-1506656 Share on other sites More sharing options...
QuickOldCar Posted February 25, 2015 Share Posted February 25, 2015 <?php if(isset($_GET['show']) && trim($_GET['show']) != ''){ $keyword = trim($_GET['show']); switch ($keyword) { case "house": $image = "$keyword.jpg"; break; case "mickey": $image = "$keyword.jpg"; break; default: $image = "default.jpg"; break; } }else{ $image = "default.jpg"; } echo $image; ?> Link to comment https://forums.phpfreaks.com/topic/294878-help-with-a-simple-script-that-switches/#findComment-1506659 Share on other sites More sharing options...
joestar Posted February 25, 2015 Author Share Posted February 25, 2015 Wow talk about speed!.. thanks guys/gals, ill let you know how it worked out in a min... Link to comment https://forums.phpfreaks.com/topic/294878-help-with-a-simple-script-that-switches/#findComment-1506660 Share on other sites More sharing options...
QuickOldCar Posted February 25, 2015 Share Posted February 25, 2015 If you don't want to create a pile of if/else or switches for each one and name them the same try the code below. Assumes images are in a directory named images if(isset($_GET['show']) && trim($_GET['show']) != ''){ $keyword = trim($_GET['show']); $image = "/images/".$keyword.".jpg"; $image_location = $SERVER['DOCUMENT_ROOT'].$image; if(!file_exists($image_location)){ $image = "/images/default.jpg"; } } else { $image = "/images/default.jpg"; } Link to comment https://forums.phpfreaks.com/topic/294878-help-with-a-simple-script-that-switches/#findComment-1506663 Share on other sites More sharing options...
joestar Posted February 25, 2015 Author Share Posted February 25, 2015 Thanks for the extended code QuickOldCar. I don't mind adding the else manually mainly because each keyword might be slightly different, for example, it might say "mickey mouse" instead of just "mickey" and I would have to create a jpg for mckey mouse and mickey. But I might be wrong as I am quite new to php. btw the code worked a charm. Link to comment https://forums.phpfreaks.com/topic/294878-help-with-a-simple-script-that-switches/#findComment-1506668 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.