Jump to content

How to order a column based on another column?


Lukeidiot

Recommended Posts

Hey there,

 

I am trying to order my messages by last message received instead of last message created.

 

(Live Site if you want to try a live preview: https://snaps.im) send a message to: admin

 

Here is how it is currently:

D0Ne7aX.png

 

zjb4uWA.png

 

Here is my mysql table:

6Isizke.png

 

I basically need it to order exactly how imessages are stored on an iphone or phone. 

Edited by Lukeidiot
Link to comment
Share on other sites

Rather basic. Add ORDER BY column ASC or DESC. You have too many queries. Just use a join. You are also opening and closing php unnecessarily.

 

More importantly, you hang pictures on a wall. Next time post your code.

 

<?php
        $sql_messages = mysqli_query($connect, "SELECT DISTINCT group_id FROM message_group WHERE username = '$username' || recipients = '$username' ORDER BY id DESC;");
        $num_messages = mysqli_num_rows($sql_messages);
        if($num_messages == 0){ ?>
        <div id="messagegroups">
        <div align="center" style="padding: 5px;" id="nomsg">You have no messages at this time.</div>
        <div id="messagegroupholder">
        </div>
        <?php } else { ?>
        <div id="messagegroups">
           <?php while($row_messages = mysqli_fetch_assoc($sql_messages)){ ?>
           <?php
           $snm = mysqli_query($connect, "SELECT * FROM message_group WHERE group_id = '$row_messages[group_id]' ORDER BY id DESC");
           $rm = mysqli_fetch_assoc($snm);
           $nm = mysqli_num_rows($snm);


           $ur = mysqli_query($connect, "SELECT * FROM users WHERE username = '$rm[recipients]' ORDER BY id DESC;");
           $rr = mysqli_fetch_assoc($ur);


           $um = mysqli_query($connect, "SELECT * FROM messages WHERE group_id = '$row_messages[group_id]' ORDER BY id DESC;");
           $rrr = mysqli_fetch_assoc($um);


           ?>
           <div id="messagegroupholder">
            <div class="qo cj ca js-msgGroup groupclick" id="msg" data-id="<?php echo $rm['group_id']; ?>">
                 <a class="b">
                  <div class="qf">
                    <span class="qj">
                    <img class="cu qh" src="./img/<?php echo $rr['image']; ?>" style="height: 42px; width: 42px;">
                    </span>
                    <div class="qg">
                      <strong><?php echo $rr['username']; ?></strong> and <strong><?php echo $nm - 1 ?> others</strong>
                      <div class="aof">
                        <?php echo substr($rrr['message'], 0, 60); ?> …
                      </div>
                    </div>
                  </div>
                </a>
              </div>
              </div>
          <?php } ?>
        <?php } ?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.