Jump to content

if () {} else if elseif.....


grahamb314

Recommended Posts

hi all

I have to echo 1st, 2nd, 3rd 4th etc depending what the value is in the DB

(Eg 0=1st, 1=2nd 2=3rd 3=4th.... up to 30=31st)

The only way I know to do it is like this:

if ( $_POST["day"] == 0  } {
$displayday = "1st"}
else if ( $_POST["Day"] == 1) {$displayday = "2nd";}
else if ( $_POST["Day"] == 2) {$displayday = "3rd";}
else if ( $_POST["Day"] == 3) {$displayday = "4th";}

 

I dont card about the rd, th etc, The number would do!

If there a quicker way of typing this out? like a loop??

Link to comment
https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/
Share on other sites

Well thanks guys this easy one is solved!

I do however have a similar problem!

 

I ahve a bunch of times:

  <select name="slot">
          <option value="0">0900 - 0930</option>
          <option value="1">0930 - 1000</option>
          <option value="2">1000 - 1030</option>
          <option value="3">1030 - 1100</option>
          <option value="4">1100 - 1130</option>
          <option value="5">1130 - 1200</option>
          <option value="6">1200 - 1230</option>
          <option value="7">1230 - 1300</option>
          <option value="8">1315 - 1345</option>
          <option value="9">1345 - 1415</option>
          <option value="10">1415 - 1445</option>
          <option value="11">1445 - 1515</option>
          <option value="12">1515 - 1545</option>
          <option value="13">1545 - 1615</option>
          <option value="14">1615 - 1645</option>
          <option value="15">1645 - 1715</option>
          <option value="16">1715 - 1745</option>
          <option value="17">1745 - 1815</option>
          
        </select>

 

I have used post to get the option value form the html page and write it to the DB in the int format.

However, I want to print out the original "1745 - 1815" as an echo.

 

any easy way to do this rather than lots of else ifs?

(Warning, there is a gap in some of the times (a 15 mins break before 13:15)

 

Link to comment
https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665419
Share on other sites

any easy way to do this rather than lots of else ifs?

Add all your times in an array eg

$times = array('0900 - 0930',
                '0930 - 1000',
                '1000 - 1030',
                etc
              );

Now when you retrieve the time from your database you'll do

 

$time_key = 3;

if(isset($times[$time_key]))
{
    $time = $times[$time_key];
}

Link to comment
https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665433
Share on other sites

Archived

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

×
×
  • Create New...

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.