grahamb314 Posted October 14, 2008 Share Posted October 14, 2008 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?? Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/ Share on other sites More sharing options...
Orio Posted October 14, 2008 Share Posted October 14, 2008 You don't need loops: <?php $day = intval($_POST['day']); if($day >= 0 && $day <= 30) $displayday = $day + 1; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665395 Share on other sites More sharing options...
revraz Posted October 14, 2008 Share Posted October 14, 2008 Instad of using Day as your compare, why not just use the DATE with the S paramter? http://us3.php.net/date Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665397 Share on other sites More sharing options...
.josh Posted October 14, 2008 Share Posted October 14, 2008 If all you want is a number to be displayed...what not just do $displayday = $_POST['day'] +1; ? Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665399 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 $displayday = date("jS", strtotime($_POST['day']+1)); echo $displayday; ? Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665401 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 If all you want is a number to be displayed...what not just do Cause he wants it in format 2nd, 3rd, 4th etc... Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665402 Share on other sites More sharing options...
.josh Posted October 14, 2008 Share Posted October 14, 2008 Really? Because he said: I dont card about the rd, th etc, The number would do! Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665410 Share on other sites More sharing options...
grahamb314 Posted October 14, 2008 Author Share Posted October 14, 2008 Instad of using Day as your compare, why not just use the DATE with the S paramter? http://us3.php.net/date I would, but whoever made the DB was silly! Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665412 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 I dont card about the rd, th etc, The number would do! Damnit! In that case just do what CW suggested, add 1... Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665413 Share on other sites More sharing options...
grahamb314 Posted October 14, 2008 Author Share Posted October 14, 2008 If all you want is a number to be displayed...what not just do $displayday = $_POST['day'] +1; ? Haha, Why didn't I think of that! Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665414 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 I haven't tested mine but I think it will add the rd, th, etc... correctly. Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665416 Share on other sites More sharing options...
grahamb314 Posted October 14, 2008 Author Share Posted October 14, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665419 Share on other sites More sharing options...
wildteen88 Posted October 14, 2008 Share Posted October 14, 2008 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]; } Quote Link to comment https://forums.phpfreaks.com/topic/128419-if-else-if-elseif/#findComment-665433 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.