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

Jerry Rodriguez
Jerry Rodriguez
5,330 Points

Stuck on PHP Stage 7 - Adjusting Invalid Numbers

I'm stuck. I'm not sure what I'm doing wrong. Any help would be much appreciated!

Should I not be doing conditionals within a loop?

controller.php
<?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 && <= 1000){
        $count_between_one_and_thousand += 1;
      }
      if($number > 1000){
        $count_greater_than_thousand += 1;
      }
    }



include('view.php');

?>

2 Answers

Jerry Rodriguez
Jerry Rodriguez
5,330 Points

I found the answer. I forgot the $number variable in the second if statement.

Wrong if($number >=1 && <= 1000){ $count_between_one_and_thousand += 1;

Correct if($number >=1 && $number<= 1000){ $count_between_one_and_thousand += 1;

This is how I solved it. Used if else if

foreach ($numbers as $number) {
      if($number < 1){
        $count_less_than_one += 1;
      } else if($number <= 1000){
        $count_between_one_and_thousand += 1;
      } else if($number > 1000){
        $count_greater_than_thousand += 1;
      }
    }
Jerry Rodriguez
Jerry Rodriguez
5,330 Points

Thanks Micah! I appreciate the feedback.