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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.