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 CRUD Operations with PHP Reading and Writing Reports Summarizing Project Time

Qasim Hafeez
Qasim Hafeez
12,731 Points

Don't understand setting $project_id=$item['project']

I don't understand how this part works. When I remove this line of code, it creates a title header row for each item so, I know what it does but, I don't know how. Can anyone explain?

 <?php 
                        $total = $project_id = 0;
                        foreach(get_task_list() as $item){
                            if($project_id != $item['project_id']){
                                $project_id = $item['project_id'];
                                echo "<thead>";
                                echo "<th>" . $item['project'] . "</th>";
                                echo "<th>Date</th>";
                                echo "<th>Time</th>";
                                echo "</thead>";
                            }

                            $total += $item['time'];

                            echo "<tr>";
                            echo "<th>" . $item['title'] . "</th>";
                            echo "<th>" . $item['date'] . "</th>";
                            echo "<th>" . $item['time'] . "</th>";
                            echo "</tr>";



                        }
Qasim Hafeez
Qasim Hafeez
12,731 Points

I think I might understand it now. The variable $project_id starts off at 0. The first time the loop runs $project_id will not be equal to the first $item's 'project_id' property and the loop will create the header row. Then it will add the task information for that project. When the loop runs again, if the next $item's 'project_id' is still the same as the $project_id variable that was set in the previous iteration of the loop, the next task for that project will be added. If the next $item's 'project_id' property is NOT the same as the $project_id variable, the code to create a new header will run and then the code to add the a task for the new project will run. Correct?

Alex Bauer
Alex Bauer
9,426 Points

From what I understand, it's saying that if the project_id = 1 which means it's not its original value of 0, then it will change that number to 1 and use the product_id of 1 in the sql qeury. The loop then runs again using the next element (project_id = 2) and changes it again.

Qasim Hafeez on Nov 29, 2016 I think I might understand it now. The variable $project_id starts off at 0. The first time the loop runs $project_id will not be equal to the first $item's 'project_id' property and the loop will create the header row. Then it will add the task information for that project. When the loop runs again, if the next $item's 'project_id' is still the same as the $project_id variable that was set in the previous iteration of the loop, the next task for that project will be added. If the next $item's 'project_id' property is NOT the same as the $project_id variable, the code to create a new header will run and then the code to add the a task for the new project will run. Correct?

Yes, that is how I understand it. I don't know if you are still active, if so feel free to post your response as an answer and mark accepted so those in class notes can see there is an answer to this question.