I'm trying to display some records from the database with the same column value. It's being displayed but the problem is it's being displayed multiple times depending on how many rows did that particular column value has.
Example:
ID | NAME | TOPIC | COMMENT
===x======x=======x========
1 | Jane | ABC | hello
2 | Doe | ABC | hello
3 | Mary | ABC | hello
4 | Pop | DEF | hello
5 | Tris | DEF | hello
If I try to display these records by TOPIC, it appears like this.
Topic: ABC
ID | NAME | TOPIC | COMMENT
===x======x=======x========
1 | Jane | ABC | hello
2 | Doe | ABC | hello
3 | Mary | ABC | hello
ID | NAME | TOPIC | COMMENT
===x======x=======x========
1 | Jane | ABC | hello
2 | Doe | ABC | hello
3 | Mary | ABC | hello
ID | NAME | TOPIC | COMMENT
===x======x=======x========
1 | Jane | ABC | hello
2 | Doe | ABC | hello
3 | Mary | ABC | hello
It displays three times because there are 3 entries with that topic. I want to display the records once if I choose a topic.
Here's my code:
<?php
if(isset($_POST['sortBtn']))
{
$sortEvent = $_POST['sort_event'];
$sortQuery = "SELECT event_topic FROM tbl_comment";
$sortResult = mysqli_query($dbcon, $sortQuery);
while($row = mysqli_fetch_assoc($sortResult))
{
if($sortEvent == $row['event_topic'])
{
$query = "SELECT * FROM tbl_comment WHERE event_topic = '". $sortEvent . "'";
$result = mysqli_query($dbcon, $query);
echo "<table class='tbEvents' border='1'>";
echo "<tr>";
echo "<th class='tHead'>#</th>";
echo "<th class='tHead'>User's Name</th>";
echo "<th class='tHead'>Event Topic</th>";
echo "<th class='tHead'>Comment</th>";
echo "<th class='tHead'>Date / Time</th>";
echo "</tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td class='tData'>". $row["user_number"] ."</td>";
echo "<td class='tData'>". $row["user_nickname"] ."</td>";
echo "<td class='tData'>". $row["event_topic"] ."</td>";
echo "<td class='tData'>". $row["user_comment"] ."</td>";
echo "<td class='tData'>". $row["date_time"] ."</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
echo "</table>";
}
}
}
?>
I tried doing GROUP BY, ORDER BY, DISTINCT, and nothing works. It's either being displayed the same way, or I get an error.
EDIT:
If I add ORDER BY or GROUP BY, I get this error:
Trying to get property of non-object
on this line of code:
if ($result->num_rows > 0)