natsu Posted November 10, 2011 Share Posted November 10, 2011 I got 3 problems I am not able to do with the following PHP code 1) The sort function is not sorting the months in order like how I want it? Why? 2) How would I use a for loop to display the array of months? 3) How would I use a while loop and switch statements to display the array of months with # of days in each? <?php $calendar = array("Janurary", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); print_r ($calendar); echo "<h1>FOR loop</h1>"; echo "(put for loop code here)"; echo "<br>"; sort($calender); echo "<br>"; print_r ($calendar); echo "<h1>WHILE loop, SWITCH statement</h1>"; echo "(put while loop code here)"; echo "<br>"; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 10, 2011 Share Posted November 10, 2011 If you want help with your homework you need to at least make an attempt. Then if you have a problem, show the code you have written and what your expected results are and what the actual results are. Quote Link to comment Share on other sites More sharing options...
natsu Posted November 11, 2011 Author Share Posted November 11, 2011 I did attempt for hours lol.. And I fixed the sort btw, I only need to know how to do the other 2. The For loop and the while/switch Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 11, 2011 Share Posted November 11, 2011 I did attempt for hours lol.. And I fixed the sort btw, I only need to know how to do the other 2. The For loop and the while/switch Have you looked at the manual for the for() and while() loop controls? There are detailed descriptions and example scripts. Again, you need to at least show an attempt before we will help you with homework. Quote Link to comment Share on other sites More sharing options...
natsu Posted November 11, 2011 Author Share Posted November 11, 2011 Yes I did read the manual Ok here is my attempt that I tried to do for the while/loop <?php $i = 11; while($i => 0) { switch ($i): case 0: echo "Janurary has 31 days"; break; case 1: echo "Feburary has 28, or 29 days"; break; case 2: echo "March has 30 days"; break; case 3: echo "April has 31 days"; break; $i--; } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 11, 2011 Share Posted November 11, 2011 Your comparison operator in the while condition is incorrect. "=>" is a type of assignment operator. You want to see of the value is "greater than or equal". You'll find the correct comparison operator here: http://php.net/manual/en/language.operators.comparison.php Also, your switch() is using a colon (":") to indicate the start of the case statements for the switch which is the alternative syntax for control structures (http://www.php.net/manual/en/control-structures.alternative-syntax.php), but you never close it. Look at the examples for the switch statement. Most use the curly braces for enclosing the case statements (which is the primary method for defining control structures). So, you can use curly braces or look at the example that uses the colon at the beginning and look for the correct way to terminate the block. http://php.net/manual/en/control-structures.switch.php Quote Link to comment Share on other sites More sharing options...
natsu Posted November 11, 2011 Author Share Posted November 11, 2011 Ok I got this so far... No errors but I am not looping through, I am trying to get all the case's displayed one by one <?php $i = 11; while($i >= 0) { switch ($i) { case 0: echo "Janurary has 31 days"; break; case 1: echo "Feburary has 28, or 29 days"; break; case 2: echo "March has 30 days"; break; case 3: echo "April has 31 days"; break; $i--; } } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 11, 2011 Share Posted November 11, 2011 You're not getting any errors, because the script is never completing. You have a loop that will continue to run as long as $i is greater than or equal to 0. $i starts with a value of 11 and you do have a line of code to decrement $i. But, look where you put that line! It ONLY gets executed for case 3:. Therefore, the line to decrement $i never gets executed and $i retains its value of 11 and the loop will run continuously. You need to put the $i--; where it will get executed on each iteration of the loop - i.e. after the switch block of code which ends with the first "}". Quote Link to comment Share on other sites More sharing options...
natsu Posted November 11, 2011 Author Share Posted November 11, 2011 Thank you. It works now But it displays it backwards (from dec - jan), I tried changing the $i variable number and the case #'s but it still appears backwards.... <?php $i = 11; while($i >= 0) { switch ($i) { case 0: echo "Janurary has 31 days <br>"; break; case 1: echo "Feburary has 28, or 29 days <br>"; break; case 2: echo "March has 30 days <br>"; break; case 3: echo "April has 31 days <br>"; break; case 4: echo "May has 30 days <br>"; break; case 5: echo "June 31 days <br>"; break; case 6: echo "July has 30 days <br>"; break; case 7: echo "August has 31 days <br>"; break; case 8: echo "September has 30 days <br>"; break; case 9: echo "October has 31 days <br>"; break; case 10: echo "November has 30 days <br>"; break; case 11: echo "December has 31 days <br>"; break; } $i--; } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 12, 2011 Share Posted November 12, 2011 Thank you. It works now But it displays it backwards (from dec - jan), I tried changing the $i variable number and the case #'s but it still appears backwards.... Well, yeah. You start $i at 0, then decrease the value by 1 on each iteration. So, on the first loop $i = 11 (December) on the next loop it equals 10 (November), etc. So, logically if you want to start at January you need to start with 0 and go to 11. The order of the case statements has no bearing on what you are trying to do. If you didn't use the "break" on each case statement, the order of the case statements has an impact - but you don't want to do that for your problem. So, you need to start with 0. That should be pretty easy to change, right? Just use $i = 0; Then, since you want to go UP to 11, you need to change the line that decrements $i to instead increment it by one. That's simple too $i++; Now, the only problem is that you need to change the while() loop condition. Currently it is while($i >= 0). But since we are starting at 0 and incrementing, that condition would never return false, so the loop would continue forever. So, if we start from 0 and go up, when would you want the loop to exit? You could write the while condition to be while $i is less than or equal to 11 or just use while $i is less than 12. I'll let you fix the actual code. Just look at the link I provided previously for the proper format. Quote Link to comment 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.