...

View Full Version : Unordered Listing of a Query



jessjenn
12-29-2003, 02:57 AM
I have two tables joined in a query: project and task. A project has many tasks and they are joined by projectid. This is my query:



select project.projectname, task.taskname
from project, task
where project.projectid = task.projectid

What I'm trying to do is list them in an unordered list as follows:


<ul>
<li>Project 1
<ul>
<li>Task 1</li>
<li>Task 2</li>
<li>Task 3</li>
</ul>
</li>
<li>Project 2
<ul>
<li>Task 4</li>
<li>Task 5</li>
<li>Task 6</li>
</ul>
</li>
</ul>


Of course, Project 2 wouldn't be the last project. It'll be for an X number of projects and tasks. How would I loop through this in PHP? Thanks.

raf
12-29-2003, 03:21 AM
Welcome here.

what do you mean by :

What I'm trying to do is list them in an unordered list as follows:

Anyway, the routine is : return the records ordered by project and then by task, like

select project.projectname, task.taskname
from project, task
where project.projectid = task.projectid
order by project.projectname asc, task.taskname asc

or order them on other variables of your choice ...

Then, when you loop through the recordset, you check to see if the previous task belonged to the same project or if it is the first task of a new project. like


echo '<ul>';
$prev='xxxxxxxx' //a dummy to display the first artist
while ($row=mysql_fetch_array($result)) // $result is the resource identifier of your select-query
if ($prev != $row['projectname']){
if ($y >=1){
echo ('</ul></li>')
}
echo ('<li>' . $row['projectname'] . '<ul>') ;
}
echo ('<li>' . $row['taskname'] . '</li>');
$prev = $row['projectname'] ;
}
echo ('</ul></li></ul>');

jessjenn
12-29-2003, 08:16 AM
Thanks for the welcome. :cool: Your code helped me figure it out. I came up with the following solution, although not a pretty one, it works.



$x = "\n";
echo "<ul>$x";
$prev='xxxxxxxx'; // a dummy to display the first artist
$y = 0; $z = 0;
while ($row=mysql_fetch_array($conn1->getResult())) {
if ($prev != $row['projectname']){
if($z>0)
echo "</ul></li>";
$z++;
echo " <li>".$row['projectname']." <ul> $x";
}
echo " <li>".$row['taskname']." </li>$x";
$y++;
$prev = $row['projectname'] ;
if ($y == 1 && $z > 1)
echo " </ul></li>";
else
($y = 1);
}
echo "</ul></li></ul>";

raf
12-29-2003, 10:51 AM
You're welcome.

Glad you've got it running.:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum