I recently asked a similar question to this about two weeks ago, but I have had no look in getting my problem working the way I need it to.
I have a query that is selecting from 2 tables; tickets and replies. I'm selecting the information from the ticket table, and I'm selecting the information from the replies table which has the ticket id stored in there. Which now comes to my problem.
My query is only displaying tickets that have more than 0 replies, but I need it to display the ticket information even if it doesn't have any replies.
I would like to know (if possible) if there is any way to fix my problem, and if there is a way to make it simpler than I currently have it.
It's a bit messy right now, but here is my is code to query and display the tickets and replies.
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$id = trim($_GET['id']);
$i = "";
$ticket = $db->conn->query("
SELECT * FROM tickets
INNER JOIN replies ON tickets.id = '$id'") or die(mysqli_error($db->conn));
while($rows = $ticket->fetch_assoc()) {
$i++;
if($_SESSION['ticket_username'] == $rows['client']) {
if($i <= 1) {
$status = $rows['status'];
echo '
<h3>'.$rows['subject'].'</h3><hr>
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><small>Created by '.$rows['client'].', '.$timeAgo->inWords($rows['created_at']).'</small></h3>
</div>
<div class="panel-body">'.nl2br($rows['message']).'</div>
</div>
';
}
echo '
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title"><small>Reply from '.$rows['reply_username'].', '.$timeAgo->inWords($rows['reply_time']).'</small></h3>
</div>
<div class="panel-body">'.nl2br($rows['reply_message']).'</div>
</div>
';
} else {
header("Location: index");
}
}
} else {
header("Location: index");
}
Check - The check is a way for you to check if you have written a legal SQL query. Arrow - This is the execute command button. This will send the query to the server and the server will write back the result to you. Square - This is the stop execution command.
If you do not include DISTINCT in a SELECT clause, you might find duplicate rows in your result, because SQL returns the JOB column's value for each row that satisfies the search condition. Null values are treated as duplicate rows for DISTINCT.
Change the query.
Use a LEFT JOIN
instead of an INNER JOIN
$ticket = $db->conn->query("SELECT * FROM tickets
LEFT JOIN replies ON tickets.id = '$id'") or die(mysqli_error($db->conn));
Explanation:
In your case, if you use inner join, it was returning only those tickets who have replies.
Image Referred from here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With