redarrow Posted March 14, 2006 Share Posted March 14, 2006 I am using the swich statement on my study can you give a way to use the statement in a more advanced way please.an example of the most poular use for it thank you.Can you use it on a dropdown box or forms or whatever thank you.[code]<?$num=40;switch ($num) {case 1;echo" num is 1";break;case 2;echo" num is 2";break;case 3;echo"num is 3";break;case 4;echo"num is 4";break;case 5;echo"num is 5";break;case 6;echo"num is 6";break;case 7;echo"num is 7";break;default:echo"num is not 1 to 10";break;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4925-switch-statement-advance-example-please/ Share on other sites More sharing options...
obsidian Posted March 14, 2006 Share Posted March 14, 2006 here's another simple example, but it's a more useful way to use it:[code]function getTimeDiff($time, $format = 'minutes') { switch($format) { case "minutes": $min = 60; $diff = floor(($time - time()) / $min); break; case "hours": $hour = 60 * 60; $diff = floor(($time - time()) / $hour); break; case "days": $day = 60 * 60 * 24; $diff = floor(($time - time()) / $day); break; } return abs($diff) . " $format difference";}[/code]this is a simple time difference function that lets you specify whether to return the difference in minutes, hours or days. Quote Link to comment https://forums.phpfreaks.com/topic/4925-switch-statement-advance-example-please/#findComment-17359 Share on other sites More sharing options...
redarrow Posted March 14, 2006 Author Share Posted March 14, 2006 Thank you for the example verry useful.Is there a way the case statement can retreve information from a dropdown menu if so can you kindly post a reply thank you all.exampleif you had a drop down menu with urls on, can the user choose the correct menu title then that information comes from the case statement a small example, please if possable tried googleing but no joy. Quote Link to comment https://forums.phpfreaks.com/topic/4925-switch-statement-advance-example-please/#findComment-17369 Share on other sites More sharing options...
obsidian Posted March 14, 2006 Share Posted March 14, 2006 well, you can use a switch statement almost anywhere you could use an if statement, so if i understand your question, you'd want something like this:[code]<?phpif (isset($_POST['submit'])) { switch($_POST['url']) { case "www.google.com": $title = "Google"; break; case "www.cnn.com": $title = "CNN"; break; default: $title = "nothing chosen!"; } echo "You chose: $title!";}?><form name='myForm' action='' method='post'><select name='url'><option value=''></option><option value='www.google.com'>www.google.com</option><option value='www.cnn.com'>www.cnn.com</option></select><br /><input type='submit' value='Check It' name='submit' /></form>[/code]remember that many, if not most cases that you see switch statements used, you could do it just as easily with another method, so be careful not to beat yourself up over using a switch statement for something that could better be handled by another method Quote Link to comment https://forums.phpfreaks.com/topic/4925-switch-statement-advance-example-please/#findComment-17373 Share on other sites More sharing options...
redarrow Posted March 14, 2006 Author Share Posted March 14, 2006 Thank you for your time and example and advice tacken cheers.[!--sizeo:5--][span style=\"font-size:18pt;line-height:100%\"][!--/sizeo--]solved[!--sizec--][/span][!--/sizec--] Quote Link to comment https://forums.phpfreaks.com/topic/4925-switch-statement-advance-example-please/#findComment-17375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.