I am so close to have comments work in my journal/blog. Tonight I slayed some demons that have been hindering me for years. But I hit a block and need some help. Probably something simple.
Here is what I have. If you go to http://www.craiggreenwood.com/journaltest.php you will see the blog in question. I can tell you the first entry has 4 comments. I put them there myself for testing! Next click "Comments" under the first entry you will see my failure. The page, journalcomments.php displays the entry one time for each comment. It also fails to display the Visitor Name or the comment they left (currently the only two things I care about).
I know WHY the entry is being repeated but I can't figure out how to make it only appear once. It has something to do with the fact that I have a while loop happening. When I call the $row of the query it dutifully returns the entry from the row in question. So how do I make it display the row one time only and then loop through the comments? Also is my syntax wrong? I should be seeing the c.VisitorName and c.Comment appear (both have values in the database) but they don't appear. The code I have is below. It's not the cleanest code as the page has grown organically but I hope it helps. Error is around line 65.
Code: Select all
<?php
$page_title = 'Journal';
include ('./includes/header.html');
require_once ('./my_connect.php');
$display = 10; // Number of records to show per page:
if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$query = "select Max(id) from tblJournal order by id ASC";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$id = $row[0];
}
if (isset($_GET['np'])) { // All ready been determined.
$num_pages = $_GET['np'];
} else { // Need to determine.
// Count the number of records
$query = "select Count(*) from tblJournal order by id ASC";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];
// Calculate the number of pages
if ($num_records > $display) { // More than one page
$num_pages = ceil($num_records/$display);
} else {
$num_pages = 1;
}
} // End of np IF.
// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
$query = "
SELECT j.id, date_format( j.date, '%M %e %Y' ) AS date, j.title, j.entry, c.VisitorName, c.DateOfComment, c.Comment
FROM tblJournal AS j
LEFT JOIN tblJournalComments AS c on j.id = c.entryid
where j.id='".$id."'
";
$result = @mysql_query ($query); // Run the query.
// Table Header
echo '<table align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="right">Subscribe to RSS feed here! <a href="http://www.craiggreenwood.com/feed.php" target="_blank"><img src="rssicon.jpg" border="0" width="15px" /></a>
</td>
</tr>
';
// Fetch and print all the records
$bg = '#ffffff'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#ffffff' ? '#ccffff' : '#ffffff'); // Switch the background color
echo '<tr bgcolor="#FFFFFF">
<td align="left">
<h2>' . $row['title'] . '</h2>
<h5>' . $row['date'] . '</h5>
</td>
</tr>
<tr bgcolor="' . $bg . '">
<td align="left">' . $row['entry'] . '</td>
</tr>
<tr bgcolor="' . $bg . '">
<td align="right"><hr width="80%"></td>
</tr>';
echo '
<tr bgcolor="' . $bg . '">
<td align="right">
' . $row['c.VisitorName'] . ' said:<p>' . $row['c.Comment'].'
</a><p></td>
</tr>
';
}
echo '</table>';
mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection
if ($num_pages > 1) {
echo '<br /><p>';
$current_page = ($start/$display)+1; // Determine what page the script is on.
if ($current_page !=1) { // If it's not the first page make a previous button
echo '<a href="../journal.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
}
// Make all the numbered pages
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="../journal.php?s=' . (($display * ($i -1))) . '&np=' . $num_pages . '">' .$i . '</a> ';
} else {
echo $i . ' ';
}
}
if ($current_page != $num_pages) {
echo '<a href="../journal.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
}
echo '</p>';
}
unset($start, $num_pages);
include ('./includes/footer.html');
?>
Craig