Welcome Guest, Not a member yet? Register   Sign In
Conditional in a Foreach
#1

[eluser]skribe[/eluser]
Hi:

I have some code that produces a list of people attached to a project as part of the client view. It cycles through the projects and lists the people. Unfortunately I can't figure out a way to test if nobody is attached to the project.

Any suggestions?

Code:
$person=array();
foreach($projects as $project):
    $person = array($project->id => $project->name);
    foreach($person as $id => $name):
         $per[]=anchor('person/view/'.$id,$name);
    endforeach;
<more code>
endforeach;
#2

[eluser]duellsy[/eluser]
Perhaps this?
Code:
$person=array();
foreach($projects as $project):
    $person = array($project->id => $project->name);
    $per = array();
    foreach($person as $id => $name):
         $per[]=anchor('person/view/'.$id,$name);
    endforeach;
    if(sizeof($per) == 0):
        // project has no people... do something about it
    endif;
<more code>
endforeach;
#3

[eluser]CroNiX[/eluser]
Quote:Unfortunately I can’t figure out a way to test if nobody is attached to the project.
can probably be done using a join and count statement in your db query, which you don't show here.
#4

[eluser]skribe[/eluser]
Thanks. I had to wangle it a bit but I got it to work. For some reason $per was always returning a value (and therefore a size greater than 0) even when it appeared to be empty. I ended up checking $project->id and that works a treat.




Theme © iAndrew 2016 - Forum software by © MyBB