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
$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
if ($r) // ran OK,
{
echo '<table summary="A listing of the project documents"> // Table header.
<thead>
<tr>
<th>Project</th>
<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>' .$row['project_name'] . '</td>
<td><a href="http://www.website.com/admin/uploads/' . $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 take the project column out (I can do that! ) and instead 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???
write an HTML code for the “taking the project column”. Not so hard.
detect the moment when project got changed. Very easy: add a variable to your loop where current project being stored. Then, compare current and previous projects and print out HTML code from the [1] if mismatch
I am not so good with English, yes.
And may be my attempts to explain, not to be a source for copy-paste looks ridiculous.
But I can’t help it. I believe that fishing rod is much more helpful than a fish.
if you add this code at the bottom of your loop (before closing })
$old_proj=$row[‘project_name’];
you will always have a project name from the previous iteration.
So, you can compare current and previous project names
I bet you can write a code that compare if $old_proj equal to $row[‘project_name’] or not.
As a result of this comparison you will know if project name got changed. If so - you can write it fancy style.
All this code must be placed inside of while loop.
The whole idea is very similar to changing row background color, already used in your code
Right now drinking beer all day sounds like the best option!
I agree with the teach a man to fish concept, really I do. Right now I’m desperately trying to get something out the door and am pulling my hair out (and bald is not a good look on me!). I’ll look this over. Maybe a light bulb will come on. One can only hope.
Thanks for that. I was just getting ready to post and ask for a bit more guidance. I had come up with the code below, but it wasn’t working. So, I was going to ask if I had either the right bait or the right pole at least.
You are right about not just giving away the answers. In this case, I really appreciate it. But we do learn better if we have to figure things out ourselves with a bit of (lot of) direction from the pros. Thanks so much for your time. I really appreciate it.
Cheers!
I am not so good with English, yes.
P.S. You’re English is just fine–in fact great. I would be hard pressed to write so eloquently in your native language!
$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';
$r = /*@*/mysql_query ($q, $link); // Run the query.
if (!r) //could not run query
{echo "could not successfully run query ($r) from database: " . mysql_error();
exit;
}
if (mysql_num_rows($r) == 0) //no data
{echo "No rows found, nothing to print, so am out of here.";
exit;
}
if ($r) // ran OK,
{ echo '<table summary="A listing of the project documents">
<thead><tr>
<th>Project</th>
<th>Title</th>
<th>Type</th>
<th>Size (KB)</th>
<th>Date Last Modified</th>
</tr></thead>';
$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>'$old_project=$row['project_name'];
if ($old_project !=$r['project_name'])
{ echo $r['project_name'] ;}
else
{ echo ""; }
echo '
</td>
<td><a href=" $uploads_dir ' . $row['filename'] .'"> ' . $row['document_name'] . ' </a></td>
<td>' . $row['document_type'] . '</td>
<td>' . $row['document_size'] . '</td>
<td>' . $row['date_last_modified'] . '</td>
</tr>';
$old_project=$row['project_name'];
}
echo '</table>'; // Close the table.
// <td>' .$row['project_name'] . '</td>
mysql_free_result ($r); // Free up the resources.
} else { // If it did not run OK.
// Public message:
echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
// Debugging message:
echo '<p>' . mysql_error($link) . '<br /><br />Query: ' . $q . '</p>';
} // End of if ($r) IF.