Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Warren Johnson
6,870 PointsCode Challenge: Enhancing a Simple PHP Application > Paginating a List Controller > Adjusting Invalid Numbers (1 of 1)
CHALLENGE: The $numbers array in the code below contains one thousand numbers. I need to count how many of these numbers fit into each of the following categories: (a) how many of them are between 1 and 1000 inclusive, (b) how many of them are less than 1, and (c) how many of them are greater than 1000. I have created a foreach loop to look at each number one after the other, but right now it’s treating every number like it belongs in all three categories. Modify the code below so that the counts are correct.
Code Skeleton: <?php
require_once('model.php'); $numbers = get_numbers();
$count_less_than_one = 0;
$count_between_one_and_thousand = 0;
$count_greater_than_thousand = 0;
foreach ($numbers as $number) {
$count_less_than_one += 1;
$count_between_one_and_thousand += 1;
$count_greater_than_thousand += 1;
}
include('view.php');
?> There is also a 2col/3row table that depicts the number totals in each range category to = 1000
I'm NOT sure about this one: I first understood an approach to the solution to be a count loop in addition to the foreach loop that would go through each incremental number( up to 1000), compare/determine the number within a given range, add 1 to the number via the appropriate increment variable (ie, $count_less_than_one), etc.?? However, what's with the table (that's incidentally NOT declared within the HTML, UNLESS...it is rendered from "view.php'. I'm still not sure how to go about this. Would appreciate a hand. Thanks
5 Answers
Randy Hoyt
Treehouse Guest TeacherYou are correct. We have the code for this broken up into three separate model, view, and controller files. The array is created in [model.php], and the table is displayed in the [view.php]. In this code challenge, we are looking at just the controller code, [controller.php], where we do the counting.
Let me give you a hint: you only want to increase the value in the $count_less_than_one variable if the number is less than one.
Does that help?
Ema Dan
15,109 PointsI'd like to know why
($number >= 1 && $number <= 1000)
works, but
(1 <= $number <= 1000)
doesn't work. Please and thank you. :)
Perry Eising
12,663 Pointsyou need an operator to connect these two statements :)
Ema Dan
15,109 PointsAhhh! Thank you Perry! That makes sense. :)
MUZ140150 Yaso
6,651 PointsThis is what worked for me...
<?php
require_once('model.php'); $numbers = get_numbers();
$count_less_than_one = 0;
$count_between_one_and_thousand = 0;
$count_greater_than_thousand = 0;
foreach ($numbers as $number) {
if ($number < 1) {
$count_less_than_one += 1;
}
if ($number >= 1 && $number <= 1000) {
$count_between_one_and_thousand += 1;
}
if ($number > 1000) {
$count_greater_than_thousand += 1;
}
}
include('view.php');
?>
Randy Hoyt
Treehouse Guest TeacherHey George F , It looks like you have an opening curly brackets when you mean to have a closing curly bracket. Instead of this ...
if ($number < 1){
$count_less_than_one += 1;
{
... you should have this:
if ($number < 1){
$count_less_than_one += 1;
}
With the wrong curly bracket, you have invalid PHP code that can't be executed at all. :-/
Does that help?
georgef
3,614 PointsGood Grief!....Thanks. If I told you how long I checked over this simple code looking at every detail you would laugh.....
Warren Johnson
6,870 PointsYou know what George?? I FEEL that....LOL..Thanks for the feedback.
Randy Hoyt
Treehouse Guest TeacherI probably wouldn't laugh, but I might smile: it can be great to learn through struggling sometimes.
Warren Johnson
6,870 PointsLOL...Thanks Randy You are INDEED, absolutely right about that. Speaking of which, for some reason, I haven't been able to get RUBY and Rails installed RIGHT. ( in order to proceed to the Ruby on Rails course.) I mean, I followed the directions dictated by the course: installed rails (all the scaffolds, etc.) AND ruby. However, as I got into the development and everything, the Rails server kept erroring. So...I'm going to "reverse" my installation, and start over. I just would hope to obtain feedback and direction this time, in order to get it right. Any suggestions??
Randy Hoyt
Treehouse Guest TeacherHey Warren Johnson,
If you run into trouble installing Rails, you should be able to post here on the forums and get some help. (I know getting everything set up and running can be a bit of a hurdle, and that's a problem that would be really great to get figured out.)
Leonard Foster
14,806 PointsI had this as my code I've been reading your comments on here hoping to gain enlightenment yet the code fails and it looks like what you guys have do I need to add a return or a count function outside the already existing functions?
<?php
require_once('model.php');
$numbers = get_numbers();
$count_less_than_one = 0;
$count_between_one_and_thousand = 0;
$count_greater_than_thousand = 0;
foreach ($numbers as $number) {
if ($number < 1){
count $count_less_than_one += 1;
}
if ($number > 0 and $number <= 1000){
count $count_between_one_and_thousand += 1;
}
if ($number > 1000){
count $count_greater_than_thousand += 1;
}
}
include('view.php');
?>
Warren Johnson
6,870 PointsWarren Johnson
6,870 PointsFor some reason, I feel that before I EVEN ASK, that this is NOT quite on the right track??? Although, the feedback that I'm get back after running it says, "You are including too many numbers in category (b). Please try again." ???
<?php
require_once('model.php'); $numbers = get_numbers();
******** if($number <= $count_less_than_one) { ******* $number = $count_less_than_one; $count_less_than_one += 1; } else { $count_between_one_and_thousand += 1; }
include('view.php');
?>
Randy Hoyt
Treehouse Guest TeacherRandy Hoyt
Treehouse Guest TeacherI always find it helpful to try to talk through what you want to accomplish out loud in sentences. Imagine you had a deck of 1000 cards with a single number printed on their face. (That's essentially what this array of numbers is.) You want to sort them into three piles: one pile with numbers less than one, one pile with numbers between 1 and 1000 inclusive, and one pile with numbers greater than 1000. What would you do?
You would go through the cards one at a time. Let's say the number was 5001. What would you ask yourself to determine what pile it goes in? If the number itself is less than 1, then I would put it in one pile. If the number itself is greater than or equal to one and less than or equal to one thousand, then I would put it in the second pile. If the number itself is greater than one thousand, then I would put it in the third pile. Make sense?
A foreach loop goes through the cards one at a time and executes the same code for each of them. This code ...
foreach ($numbers as $number) {... looks at every number in the array one at a time. It loads the one we're currently looking at into a variable called $number, and then it will execute the code inside the foreach loop once for each number.
How do we check if the number we are looking at is less than one? With a conditional, like this:
if ($number < 1) {Anything we write inside that conditional will only get executed if the number is less than one. When the code challenge starts, this code is getting executed for every number:
$count_less_than_one += 1;But we only want it to get executed when the number is less than one! So we put it inside the conditional:
Does that all make sense?
Randy Hoyt
Treehouse Guest TeacherRandy Hoyt
Treehouse Guest TeacherBy the way, you can get your code to format nicely in this forum by typing four back-ticks before it like this:
Warren Johnson
6,870 PointsWarren Johnson
6,870 PointsNOT quite getting there. Does my logic appear correct??
Results: (Less than 1) - 0 (Between 1-1000) - 412 (Greater than 1000) - 0
'''''''''''''''' <?php
require_once('model.php'); $numbers = get_numbers();
''''''''''''''
include('view.php');
Randy Hoyt
Treehouse Guest TeacherRandy Hoyt
Treehouse Guest TeacherThe code inside the foreach loop is going to execute over and over, one time for each number in the array. Since there are one thousand numbers in the array, it will execute one thousand times.
The
$numbervariable contains the current number that we are looking at. It will change each time by the foreach loop automatically; you don't have to change it.The
$count_less_than_one variablecontains the count of numbers that are less than one. If the number is less than one, you want to add one to the current count. Inside the foreach loop, you wouldn't want to set this to zero.The code should essentially look like this:
Here's how I would describe what this code does: "Let's start by setting the variable that will hold our count equal to zero. Let's next loop through all the elements in the $numbers array one at a time. Let's look at the first element first. If it is less than 1, then let's add one to the count variable. Then let's look at the second element. If it is less than 1, then let's add one to the count variable. Then let's look at the third element. If it is less than 1 ... etc."
At the end of this code, we'll have looked at each number one at a time to see if it is less than 1. Every time the number is greater than one, we add one to our count variable. There are actually 234 numbers in that array that are less than one thousand, so at the end of this code the count variable will be equal to 234.
Does that help?
Warren Johnson
6,870 PointsWarren Johnson
6,870 PointsThanks Randy, once I again. As usual, I managed to figure it AFTER I sought help from the forum this LAST time...
georgef
3,614 Pointsgeorgef
3,614 PointsWhy isn't this working:
foreach ($numbers as $number) {
Warren Johnson
6,870 PointsWarren Johnson
6,870 PointsI actually figured it out yesterday...the code should be:
foreach ($numbers as $number) { if ($number < 1){ $count_less_than_one += 1;
} However, thanks for looking it over George. I appreciate your help.
Randy Hoyt
Treehouse Guest TeacherRandy Hoyt
Treehouse Guest TeacherYou don't have to use an
elseif, but that works just fine. (That probably more accurately maps to the sorting cards metaphor, since a card could only go in one pile.) I would have written it like this: