Jump to content

Archived

This topic is now archived and is closed to further replies.

redarrow

switch statement advance example please.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


example

if 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.

Share this post


Link to post
Share on other sites
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]
<?php
if (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

Share this post


Link to post
Share on other sites
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--]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.