Jump to content

For loop and sorting array of months


natsu

Recommended Posts

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>";
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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--;
			}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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--;	
				}
			}
?>

Link to comment
Share on other sites

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 "}".

Link to comment
Share on other sites

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--;
			}
?>

Link to comment
Share on other sites

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.

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.