Jump to content


Photo

switch statement advance example please.


  • Please log in to reply
4 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 14 March 2006 - 02:06 PM

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.

<?
$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;

}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 14 March 2006 - 02:17 PM

here's another simple example, but it's a more useful way to use it:
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";
}

this is a simple time difference function that lets you specify whether to return the difference in minutes, hours or days.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 14 March 2006 - 02:30 PM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 14 March 2006 - 02:39 PM

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:
<?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>

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
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 14 March 2006 - 02:46 PM

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--]
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users