Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unordered Listing of a Query

    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:

    Code:
    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:
    Code:
    <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.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    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>'); 

  • #3
    New Coder
    Join Date
    Dec 2003
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the welcome. Your code helped me figure it out. I came up with the following solution, although not a pretty one, it works.

    PHP Code:
    $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 == && $z 1
           echo 
    "   </ul></li>";
       else 
              (
    $y 1); 
    }
    echo 
    "</ul></li></ul>"

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're welcome.

    Glad you've got it running.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •