JCleaver310
09-12-2011, 10:43 PM
I have been working on building a blog by hand to gain more experience with PHP. I have completed most of the main webpage so far, but I am experiencing a problem with the Order By statement in the comments section.
Here is the code that I believe is causing the problem:
<ul>
<?php
//This will be a stacked query that will first grab the current blogid, then grab the username from the user table and use that username to replace the userid in the comment table, and finally post both the user name and the user's comment (5 most recent)
//Start by grabbing the most recent blog
$query1 = "SELECT * FROM CurrentBlog ORDER BY BlogDate DESC LIMIT 1";
$data1 = mysqli_query($dbc, $query1)
or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query1");
while ($row1 = mysqli_fetch_array($data1)) {
$blogid = $row1['BlogId'];
//Now make a query to grab the userid and the username from the Users table
$query2 = "SELECT * FROM Users";
$data2 = mysqli_query($dbc, $query2)
or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query2");
while ($row2 = mysqli_fetch_array($data2)) {
$userid = $row2['UserId'];
$username = $row2['UserName'];
//Now make a query that grabs the 5 most recent comments, sets the username to the associated user id, and sorts the comments by date desc
$query3 = "SELECT * FROM Comments WHERE BlogID='$blogid' ORDER BY CommentDate DESC LIMIT 5";
$data3 = mysqli_query($dbc, $query3)
or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query3");
while ($row3 = mysqli_fetch_array($data3)) {
$commentuserid = $row3['UserID'];
$commenttext = $row3['CommentText'];
if ($userid == $row3['UserID']) {
$commentuserid = $username;
echo '<li>';
echo '<h3>';
echo $commentuserid;
echo '</h3>';
echo '<p>';
echo $commenttext;
echo '</p>';
echo '<p>';
$Ctimestamp = $row3['CommentDate'];
$Cdate = date("d-m-Y", $Ctimestamp);
echo $Cdate;
echo '</p>';
echo '</li>';
}
}
}
}
?>
</ul>
And here are my results:
User 1
User 1's comment
12-10-2011
User 1
User 1's second comment
12-09-2011
User 2
User 2's comment
12-10-2011
User 3
This might appear as it it user 3's second comment
14-09-2011
User 3
User 3's comment
12-09-2011
Any help would be appreciated.
Here is the code that I believe is causing the problem:
<ul>
<?php
//This will be a stacked query that will first grab the current blogid, then grab the username from the user table and use that username to replace the userid in the comment table, and finally post both the user name and the user's comment (5 most recent)
//Start by grabbing the most recent blog
$query1 = "SELECT * FROM CurrentBlog ORDER BY BlogDate DESC LIMIT 1";
$data1 = mysqli_query($dbc, $query1)
or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query1");
while ($row1 = mysqli_fetch_array($data1)) {
$blogid = $row1['BlogId'];
//Now make a query to grab the userid and the username from the Users table
$query2 = "SELECT * FROM Users";
$data2 = mysqli_query($dbc, $query2)
or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query2");
while ($row2 = mysqli_fetch_array($data2)) {
$userid = $row2['UserId'];
$username = $row2['UserName'];
//Now make a query that grabs the 5 most recent comments, sets the username to the associated user id, and sorts the comments by date desc
$query3 = "SELECT * FROM Comments WHERE BlogID='$blogid' ORDER BY CommentDate DESC LIMIT 5";
$data3 = mysqli_query($dbc, $query3)
or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query3");
while ($row3 = mysqli_fetch_array($data3)) {
$commentuserid = $row3['UserID'];
$commenttext = $row3['CommentText'];
if ($userid == $row3['UserID']) {
$commentuserid = $username;
echo '<li>';
echo '<h3>';
echo $commentuserid;
echo '</h3>';
echo '<p>';
echo $commenttext;
echo '</p>';
echo '<p>';
$Ctimestamp = $row3['CommentDate'];
$Cdate = date("d-m-Y", $Ctimestamp);
echo $Cdate;
echo '</p>';
echo '</li>';
}
}
}
}
?>
</ul>
And here are my results:
User 1
User 1's comment
12-10-2011
User 1
User 1's second comment
12-09-2011
User 2
User 2's comment
12-10-2011
User 3
This might appear as it it user 3's second comment
14-09-2011
User 3
User 3's comment
12-09-2011
Any help would be appreciated.