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

Prince Sargbah
Prince Sargbah
2,551 Points

How to automatically determine the status of all rows in a table

Hi, have two tables, one for campaigns, and the other for jobs. Jobs have status row. Jobs status are Pending by default when created. Clients can log in and approve jobs. The job table is linked to the campaign tables. A campaign can have more than one jobs. So I want to automatically display campaign as COMPLETED if all jobs linked to that Campaign is Approved. Please is the code I have. However, one 1 job in a Campaign is Approve and another is not, it still show the status of the campaign as Completed. Please help me to determine how to automatically show Campaign status as Completed only if all the jobs of such Campaign are Approved. Jobs under specific Campaigns are linked by JobCode. Please help. Thanks

---------------------------- Code -------------------------------------<br> $Query02 = $db->query("SELECT COUNT(*) as counted FROM Jobs WHERE JobCode = '$JobCode' AND Status = 1"); $Result02 = $Query02->fetch(PDO::FETCH_OBJ); echo "<td><center>"; if ( $Count > 0 ) { echo "<span class='label label-info'>Completed</span>"; } else { echo "<span class='label label-success'>Processing</span>"; } echo "</center></td>";

Jake Lundberg
Jake Lundberg
13,965 Points

Just to make sure I understand correctly, your job status has only 2 states: Pending, and Approved, correct? If this is the case, you simply need to test for a Pending status. If even one Pending status is found, your code should spit out "Pending", else "Approved".

Hope this helps.

2 Answers

Prince Sargbah
Prince Sargbah
2,551 Points

I already have the Else statement in the code. I'm trying to automatically determine the status of the Campaign by the jobs in the Jobs Table. For example, I want to display COMPLETED on the Campaign only if all the Jobs linked to that Campaign is Approved. But the code I have displaying Completed even if all the jobs link to that specific Campaign is not Approved.

Ahmed Aly
Ahmed Aly
468 Points

What is the $count variable? I think the problem is around this variable, because you made the script to display "completed" status, if the $count equals to 1 or more. You need to fix this $count part, and make it equal to the number of jobs..

So you need to check for the number of jobs in that campaign against the number of the approved jobs... if they are equal, then it would display completed, otherwise display processing!