PHP Messages Get Senders name

Status
Not open for further replies.

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Hello everyone,
As I stated on my previous post I stated I'm doing a messaging system and, on this dropdown I want to show sender's name and I need query for that.
I couldn't figure it out.
What I've tried:
PHP:
<?php
if($message['receiver_id'] == $user['id']) {
    // im receiver, show sender's name 
    $qs = $db->query("SELECT * FROM users WHERE id = '{$messages['receiver_id']}'"); 
    $sender = $db->assoc($qs);
        }
echo $sender['username']; ?>
and it didn't work.
What shall I do?
Thanks.
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Let me see all your code again please
PHP:
<?php
include('../config.php'); 

$qu = $db->query("SELECT * FROM users WHERE username = '{$_SESSION['username']}'"); 
$user = $db->assoc($qu);

    $get_messages = $db->query("SELECT * FROM messages WHERE receiver_id = '{$user['id']}' AND sender_id = '{$user['id']}'
"); 
    $queryr = $db->query("SELECT * FROM messages WHERE receiver_id = '{$user['id']}'"); 
    $is_read = $db->assoc($queryr);
   
    while($message = $db->assoc($get_messages)){
if($message['receiver_id'] == $user['id']) {
    // im receiver, show sender's name 
    $qs = $db->query("SELECT * FROM users WHERE id = '{$messages['receiver_id']}'"); 
    $sender = $db->assoc($qs);
        }

?>
        <li class="media">
                                    <div class="media-left">
                                        <img src="assets/images/placeholder.jpg" class="img-circle img-sm" alt="">
                                       
                                    <?php if($is_read['is_read'] > 0) { ?><span class="badge bg-danger-400 media-badge"><i class="icon-exclamation"></i>  <?php } ?>
                                    </div>

                                    <div class="media-body">
                                        <a href="#" class="media-heading">
                                            <span class="text-semibold">
                                            <?= $sender['username']; ?>
                                            </span>
                                            <span class="media-annotation pull-right">
                                        </a>

                        <span class="text-muted"> 

                                        </span>
                                    </div>
                                </li>
                            <?php 
       
        } 
        ?>
 

JayC

Well-Known Member
Aug 8, 2013
5,505
1,401
Same issue as stated in previous post you didn't correct it

get_messages = $db->query("SELECT * FROM messages WHERE receiver_id = '{$user['id']}' AND sender_id = '{$user['id']}'

Why are you selecting only rows where you are the sender and receiver. It should be OR not AND.
 

JayC

Well-Known Member
Aug 8, 2013
5,505
1,401
it actually didn't help, and i'm choosing all rows because I use all them.
So you're going to only select the rows where you are the sender and receiver? How the fuck do u even have the developer tab and you can't even do this simple php

K. Back to this

1) Do you have records in the messages table
2) if you're selecting records where you are the sender and the receiver , why are u then checking to see if u are the receiver?
3) Check to see if u have record in the table where your user ID is both sender and receiver
 
it actually didn't help, and i'm choosing all rows because I use all them.
If you say receiver = userid AND sender = userid
It will return messages u send urself.

If you say receiver = userid OR sender = userid
It will return messages u have sent or received from anyone
 

Zodiak

recovering crack addict
Nov 18, 2011
453
417
PHP:
include('../config.php');

$qu = $db->query("SELECT * FROM `users` WHERE `username` = '".$_SESSION['username']."' LIMIT 1");
if($qu->num_rows <= 0) {
    die("no user row found.");
}
$user = $qu->fetch_object();

    $get_messages = $db->query("SELECT * FROM `messages` WHERE `receiver_id` = '".$user->id."' OR `sender_id` = '".$user->id."'
");
if($get_messages->num_rows <= 0) {
    die("No messages to be displayed.");
}
    $getMessage = $db->query("SELECT * FROM `messages` WHERE `receiver_id` = '".$user->id."' LIMIT 1");
    if($getMessage->num_rows <= 0) {
        die("Selected message doesn\'t exist.");
   }
  
   $selectedMessage = $db->fetch_object($getMessage);
  
    while($message = $get_messages->fetch_object()){
if($message->receiver_id == $user->id) {
    // im receiver, show sender's name
    $qs = $db->query("SELECT * FROM `users` WHERE `id` = '".$messages->receiver_id."' LIMIT 1");
    if($qs->num_rows >= 1) {
    $sender = $qs->fetch_object();
    }
}

?>
        <li class="media">
                                    <div class="media-left">
                                        <img src="assets/images/placeholder.jpg" class="img-circle img-sm" alt="">
                                      
                                    <?php if($getMessage->is_read'] > 0) { ?><span class="badge bg-danger-400 media-badge"><i class="icon-exclamation"></i>  <?php } ?>
                                    </div>

                                    <div class="media-body">
                                        <a href="#" class="media-heading">
                                            <span class="text-semibold">
                                            <?= $sender->username; ?>
                                            </span>
                                            <span class="media-annotation pull-right">
                                        </a>

                        <span class="text-muted">

                                        </span>
                                    </div>
                                </li>
                            <?php
      
        }

1. fetch_object > fetch_assoc.
2. Your queries are fucking disgusting.
3. Your code needs working on.

Not sure if the code above will even work with your code, but hey, I tried.
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
PHP:
include('../config.php');

$qu = $db->query("SELECT * FROM `users` WHERE `username` = '".$_SESSION['username']."' LIMIT 1");
if($qu->num_rows <= 0) {
    die("no user row found.");
}
$user = $qu->fetch_object();

    $get_messages = $db->query("SELECT * FROM `messages` WHERE `receiver_id` = '".$user->id."' OR `sender_id` = '".$user->id."'
");
if($get_messages->num_rows <= 0) {
    die("No messages to be displayed.");
}
    $getMessage = $db->query("SELECT * FROM `messages` WHERE `receiver_id` = '".$user->id."' LIMIT 1");
    if($getMessage->num_rows <= 0) {
        die("Selected message doesn\'t exist.");
   }
 
   $selectedMessage = $db->fetch_object($getMessage);
 
    while($message = $get_messages->fetch_object()){
if($message->receiver_id == $user->id) {
    // im receiver, show sender's name
    $qs = $db->query("SELECT * FROM `users` WHERE `id` = '".$messages->receiver_id."' LIMIT 1");
    if($qs->num_rows >= 1) {
    $sender = $qs->fetch_object();
    }
}

?>
        <li class="media">
                                    <div class="media-left">
                                        <img src="assets/images/placeholder.jpg" class="img-circle img-sm" alt="">
                                   
                                    <?php if($getMessage->is_read'] > 0) { ?><span class="badge bg-danger-400 media-badge"><i class="icon-exclamation"></i>  <?php } ?>
                                    </div>

                                    <div class="media-body">
                                        <a href="#" class="media-heading">
                                            <span class="text-semibold">
                                            <?= $sender->username; ?>
                                            </span>
                                            <span class="media-annotation pull-right">
                                        </a>

                        <span class="text-muted">

                                        </span>
                                    </div>
                                </li>
                            <?php
   
        }

1. fetch_object > fetch_assoc.
2. Your queries are fucking disgusting.
3. Your code needs working on.

Not sure if the code above will even work with your code, but hey, I tried.
Things to note:
1. this isn't mysqli, i created my own MySQL class.
2.It returns no messages to display but i have a message: Messages table: Users table:



Edit: fixed,had to edit some code and its working like a charm now. Thanks, Zodiak!


Sorry been spamming this but, For some reason it don't show any info about sender, any clues why?
Edited code:
PHP:
<?php
include('../config.php');

$qu = $db->query("SELECT * FROM `users` WHERE `username` = '".$_SESSION['username']."' LIMIT 1");
if($db->num($qu) <= 0) {
    die("no user row found.");
}
$user = $db->assoc($qu);

    $get_messages = $db->query("SELECT * FROM `messages` WHERE `receiver_id` = '".$user['id']."' OR `sender_id` = '".$user['id']."'
");
if($db->num($get_messages) <= 0) {
    die("No messages to be displayed.");
}
    $getMessage = $db->query("SELECT * FROM `messages` WHERE `receiver_id` = '".$user['id']."' LIMIT 1");
    if($db->num($get_messages) <= 0) {
        die("Selected message doesn\'t exist.");
   }
 

 
    while($message = $db->assoc($get_messages)){


    // im receiver, show sender's name
    $qs = $db->query("SELECT * FROM `users` WHERE `id` = '".$messages['sender_id']."' LIMIT 1");
  
    $sender = $db->assoc($qs);
  


?>
        <li class="media">
                                    <div class="media-left">
                                        <img src="<?= $sender['avatar']; ?>" class="img-circle img-sm" alt="">
                                    
                                    <?php if($message['is_read'] > 0) { ?><span class="badge bg-danger-400 media-badge"><i class="icon-exclamation"></i>  <?php } ?>
                                    </div>

                                    <div class="media-body">
                                        <a href="#" class="media-heading">
                                            <span class="text-semibold">
                                            <?= $sender['username']; ?>
                                            </span>
                                            <span class="media-annotation pull-right">
                                        </a>

                        <span class="text-muted">
<?= substr($message['text'], 0,20); ?>
                                        </span>
                                    </div>
                                </li>
                            <?php
    
        }
 

Zodiak

recovering crack addict
Nov 18, 2011
453
417
Things to note:
1. this isn't mysqli, i created my own MySQL class.
2.It returns no messages to display but i have a message: Messages table: Users table:



Edit: fixed,had to edit some code and its working like a charm now. Thanks, Zodiak!


Sorry been spamming this but, For some reason it don't show any info about sender, any clues why?
Edited code:
PHP:
<?php
include('../config.php');

$qu = $db->query("SELECT * FROM `users` WHERE `username` = '".$_SESSION['username']."' LIMIT 1");
if($db->num($qu) <= 0) {
    die("no user row found.");
}
$user = $db->assoc($qu);

    $get_messages = $db->query("SELECT * FROM `messages` WHERE `receiver_id` = '".$user['id']."' OR `sender_id` = '".$user['id']."'
");
if($db->num($get_messages) <= 0) {
    die("No messages to be displayed.");
}
    $getMessage = $db->query("SELECT * FROM `messages` WHERE `receiver_id` = '".$user['id']."' LIMIT 1");
    if($db->num($get_messages) <= 0) {
        die("Selected message doesn\'t exist.");
   }
 

 
    while($message = $db->assoc($get_messages)){


    // im receiver, show sender's name
    $qs = $db->query("SELECT * FROM `users` WHERE `id` = '".$messages['sender_id']."' LIMIT 1");
 
    $sender = $db->assoc($qs);
 


?>
        <li class="media">
                                    <div class="media-left">
                                        <img src="<?= $sender['avatar']; ?>" class="img-circle img-sm" alt="">
                                   
                                    <?php if($message['is_read'] > 0) { ?><span class="badge bg-danger-400 media-badge"><i class="icon-exclamation"></i>  <?php } ?>
                                    </div>

                                    <div class="media-body">
                                        <a href="#" class="media-heading">
                                            <span class="text-semibold">
                                            <?= $sender['username']; ?>
                                            </span>
                                            <span class="media-annotation pull-right">
                                        </a>

                        <span class="text-muted">
<?= substr($message['text'], 0,20); ?>
                                        </span>
                                    </div>
                                </li>
                            <?php
   
        }
PHP:
while($message = $db->assoc($get_messages)){


    // im receiver, show sender's name
    $qs = $db->query("SELECT * FROM `users` WHERE `id` = '".$messages['sender_id']."' LIMIT 1");

$messages to $message

Code:
$qs = $db->query("SELECT * FROM `users` WHERE `id` = '".$message['sender_id']."' LIMIT 1");
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
PHP:
while($message = $db->assoc($get_messages)){


    // im receiver, show sender's name
    $qs = $db->query("SELECT * FROM `users` WHERE `id` = '".$messages['sender_id']."' LIMIT 1");

$messages to $message

Code:
$qs = $db->query("SELECT * FROM `users` WHERE `id` = '".$message['sender_id']."' LIMIT 1");
I'm such a moron. thanks zodiak! ur best!
 
Status
Not open for further replies.

Users who are viewing this thread

Top