printing ordered by query results as a header and table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cruise jente
    New Member
    • Mar 2010
    • 1

    printing ordered by query results as a header and table

    First of all, I'm new to this level of PHP, so my apologies!

    My goal is to create an HTML table that lists the title, type, size, and date of a document sorted by the project to which the document belongs. Like this:



    I can get the data out of MYSQL with

    Code:
    $q = 'SELECT DISTINCT project_name, document_name, document_type, 
    
    document_size, date_last_modified FROM documents LEFT JOIN projects ON 
    
    documents.project_id = projects.project_id ORDER BY project_name ASC, 
    
    date_last_modified DESC';
    I can create the table with project as a column with

    Code:
    if ($r) // ran OK,
    { 
    echo '<table summary="A listing of the project documents"> // Table header.
    <thead>
              <tr>   
               <th>Title</th>
              <th>Type</th>
              <th>Size (KB)</th>
              <th>Date Last Modified</th>
              </tr>
    </thead>';
              
    // Fetch and print all the records
    $bg = '#dodcbc'; //set the bg color darker green
    while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
    $bg = ($bg=='#dodcbc' ? '#eff3e9' : '#dodcbc'); //switch the bg color
    echo '<tr bgcolor="' .$bg . '">
              <td><a href="http://www.p.com/' . $row['filename'] .'"> ' . $row['document_name'] . ' </a></td>
              <td>' . $row['document_type'] . '</td>
              <td>' . $row['document_size'] . '</td>
              <td>' . $row['date_last_modified'] . '</td>
              </tr>';
              }
    echo '</table>'; // Close the table.
    mysql_free_result ($r); // Free up the resources.
    But I'm stumped as to where to go from here. Basically I want to print the project_name as a <h2> before the table with the relevant documents listed in the table.

    I know it must be so simple and basic, but I am completely missing it and have spent hours looking for a solution on the web. What am I missing???

    Many thanks for your help--I truly appreciate it!
    Last edited by cruise jente; Mar 4 '10, 06:40 PM. Reason: take out name
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    I see only 2 ways around it.
    1. You have to query your database for project_name before any of the code you show here.
    2. You have to use some sort of condition within your while loop to echo this header only once.

    Second solution also means you would have to move around your code quite a bit.

    In general its good to avoid conditionals if possible so I would probably opt for No1

    Comment

    Working...