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

PHP Enhancing a Simple PHP Application Paginating a List: Controller Adjusting Invalid Numbers

Don Shipley
Don Shipley
19,488 Points

Challenge task 1 of 1 need help

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 < 0) {
    $count_less_than_one = $number += 1;
  }
  if ($number >= 1 &&  $number <= 999) {
    $count_between_one_and_thousand = $number += 1;
  }
  if ($number > 1000){
    $count_greater_than_thousand = $number += 1;
  }

}

6 Answers

You have slight logic error in your code. Every time an if statement is true, you simply increment the corresponding variable by 1, like so:

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 < 0) {
    $count_less_than_one += 1;
  }
  if ($number >= 1 &&  $number <= 999) {
    $count_between_one_and_thousand += 1;
  }
  if ($number > 1000){
    $count_greater_than_thousand += 1;
  }

}
Don Shipley
Don Shipley
19,488 Points

From another post which was the same as yours and one I wrote on other time yours and mine did not pass yet the same code given to me passed which is the same as your from above. Thank you for your time.

I'm really confused as to how this code passes the challenge. It doesn't meet the requirements of the challenge and there are errors when I paste it in. Only when I change < 0 to < 1 and the 999 to 1000 do I get it to pass.

Don Shipley
Don Shipley
19,488 Points

Ok what do I need to do?

Compare your code with what I have posted above.

Ashley

Don Shipley
Don Shipley
19,488 Points

I even copied and pasted it in the test question and no go.

Don Shipley
Don Shipley
19,488 Points

Sorry been working on this all day. Not sure if I should go on. Do not seem to be learning enough.

Hi Don,

Two of your conditional checks aren't quite right and you modified the assignment statements for the $count variables that already existed as part of the starter code.

$count_less_than_one += 1;
$count_between_one_and_thousand += 1;
$count_greater_than_thousand += 1;

There isn't any problem with these statements so you don't want to modify them. They correctly increment the count variables by 1 each time through the loop.

So you want to wrap each of those in if statements so only one gets incremented each time through the loop.

A couple of your if conditions are off. The first group is numbers less than 1 not numbers less than 0 as you have.

The second group is all numbers between 1 and 1000, inclusive. This means both 1 and 1000 are included in this group.

    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;
      }
    }
Don Shipley
Don Shipley
19,488 Points

Thank you. At first I was using if <=0 on the second at first I used >= 1 || I than read up on php.net and changed the || to && I tried all thes before I posted the one posted was the worst for I added $count_less_than_one = $number += 1; which I should have deleted the $number before posting. My real concern is there should be a way to look up the content of the test questions. It could be a long way back to view and not just the video you just got through watching. I love Treehouse better than linda.com but linda.com seems to go into it a little deeper. I can understand more on Treehouse and having test questions is a great way to learn. Hopefully Treehouse will come up in the library a way to look up what you could be stuck on such as arrays. conditionals. integers etc This would be great having a test question knowing what you need and look it up to make sense. I need to read up more on .htaccess and the redirect of header(). Through this course the header(LOCATION: works on my localhost but not on my life server.On my live server it just comes up as a white page.

Thank you once again for you valuable time.