Wrong switch statement please help fix it

Started on the old forum in February 2011 · view the original in the archive · old links redirect here

3 replies · 304 views · started 15 years ago · 1 min read

2 participants
s3nzo Developer · 156 posts #1

Hello guys i've coded a death calculator but it looks like some is wrong with my month switch statement here's the code

<?php
echo '<html>
        <head>
          <title>jnrWap.co.cc | Death calculator results</title>
          <link rel="stylesheet" type="text/css" href="../downloads/style.css"/>
          </head>
        <body>
      <div class="title"><img src="../logo.png" alt="jnrWap"/></div>';
$fname = $_POST['fname'];
$dd    = $_POST['dd'];
$mm    = $_POST['mm'];
$yy    = $_POST['yy'];
$sname = $_POST['sname'];
$city  = $_POST['city'];
$place = $_POST['place'];
$yr    = date("Y");
$yr1   = date("Y") + 1;
  echo '<div class="dl">';
include("../admob.php");
  echo '</div><div class="dl2">';
 if($dd > 31 || $mm > 12 || $yy > $yr)
  {
    echo 'You entered a wrong Date of birth <a href="index.php">start again</a><br>';
  }
 else 
    {
       echo $fname.' '.$sname.' Will die on this day ';
       $dday  = rand(1,28);
  
       echo $dday.' ';
/*Problem must be somewhere around here, instead of getting a month displayed i get a month and a number like 14 March3 2014 instead of 14 March 2014
*/
        $dmnth = rand(1,12);

        switch($dmnth)
 {
 case 1 : echo 'January';
  break;
 case 2 : echo 'February';
  break;
 case 3 : echo 'March';
  break;
 case 4 : echo 'April';
  break;
 case 5 : echo 'May';
  break;
 case 6 : echo 'June';
  break;
 case 7 : echo 'July';
  break;
 case 8 : echo 'August';
  break;
 case 9 : echo 'September';
  break;
 case 10 : echo 'October';
  break;
 case 11 : echo 'November';
  break;
 default : echo 'December';
  break;
} 

 echo $dmnth.' ';
$dyp   = $yr + 20;
$dyr   = rand($yr1,$dyp);
 
echo $dyr.' I\'m sorry to break the news<br><div class="dl">';
}
include("../buzz.php");
  echo '</div>';
include("../foot.php");
?>

Oh and if you want a demo please go to jnrWap

frostymarvelous Senior · 266 posts #2

Your switch statement is correct. The problem is the echo $dmnth. After the switch. You echoed the month already in the switch. You did not reassign the dmnth var so echoing it echoes the month number as well.

New code

 
e c h o  ' ' ;//remove $dmnth
$ d y p = $ y r + 2 0 ;
$ d y r = r a n d ( $ y r 1 ,
$ d y p ) ;

Perfection comes at a cost

I accept liberty!

s3nzo Developer · 156 posts #3

Thanks frostymarvelous it worked and i had more than one error and i fixed them all now

frostymarvelous Senior · 266 posts #4

You welcome bro. Btw, you could use a simple mktime and date function to replace your switch statements. Little by little, you will get there.

Perfection comes at a cost

I accept liberty!

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close