PHP - Latest Selection Question

Asked

Viewed 42 times

1

Good,

I created a crypt that shows all user messages limited to 3. My object is that messages show like this each time they are recent.

     MSG hora: 12:30 - OLA
     MSG hora: 12:31 - OLA
     MSG hora: 12:32 - OLA

The problem is that when there is a new message and there are already 3, it just does not update, shows the 3 old ones, only past +2 messages is that it shows but in ASC form (top to bottom) example:

     MSG hora: 12:32 - OLA
     MSG hora: 12:31 - OLA
     MSG hora: 12:30 - OLA

And you should always show the latest on the 3rd here:

     MSG hora: 12:30 - OLA
     MSG hora: 12:31 - OLA
     Mais recente ->>> MSG hora: 12:32 - OLA






     $message_from = "SELECT msg_content, msg_from, msg_date FROM public_messeger_reply WHERE msg_to = '". $_SESSION['u_id'] ."' AND msg_reply_id = '". $order_detail['ads_id'] ."' ORDER BY msg_date DESC LIMIT 3";
     $to_query = $con->query($message_from);


     if($to_query->num_rows > 0) {
       echo "<blockquote>";
        while($fetch_to = $to_query->fetch_assoc()) {
              echo "<p><b>Negociante:</b> ". $fetch_to['msg_content'] ." <small>". dateName($fetch_to['msg_date']) ."</small></p>";
              } 
      echo "</blockquote>";
     }
  • 1

    Now, but the DESC you are using will show in descending order. It should not change to ASC?

  • But ASC shows from the oldest, I want the latest records...

  • When only 3 works with ASC, but from 4 no longer works.

  • Exactly...dvd.....

  • How do I make it work...?

  • and how is the type of date column?

  • the eDATETIME column

  • Strange, ORDER BY msg_date DESC LIMIT 3 here worked well, being that msg_date is a type column DATETIME

  • @Exact leocaracciolo!

  • the last returned here on my site visit table was 2018-05-27 22:03:09

  • now 2018-05-27 22:14:49

  • Are you sure msg_date is the column whose type is DATETIME?

  • Yes I do. in DESC it shows right... I hit from TOP to BOTTOM, I wanted UP that is ASC, but it does not show the recent... :(

  • 1

    got it, you want to reverse the order in the presentation

Show 9 more comments

2 answers

2


Use a subquery to pick only the last 3 records, and then sort them ascendantly (ASC):

SELECT *
FROM (SELECT msg_content, msg_from, msg_date FROM public_messeger_reply
     WHERE msg_to = '". $_SESSION['u_id'] ."'
     AND msg_reply_id = '". $order_detail['ads_id'] ."'
     ORDER BY msg_date DESC 
     LIMIT 3) t
ORDER BY msg_date ASC;
  • Just doesn’t show anything....

  • ... Uai, Perai

  • JA DEUU, just corrected the variable :D

  • Yeah, I was getting worried because the code is correct rss

  • for you to ponder, save in an array and invert the elements of it

  • Thanks a brother :D

Show 1 more comment

0

Other option: - for those who have difficulty building query but know well php!

if($to_query->num_rows > 0) {
   echo "<blockquote>";
    while($fetch_to = $to_query->fetch_assoc()) {
      //concatena acrescentando uma virgula ao final de cada loop  
      $result .= "<p><b>Negociante:</b> ". $fetch_to['msg_content'] ." <small>". dateName($fetch_to['msg_date']) ."</small></p>,";
    } 

    //retira a ultima virgula       
    $result=substr_replace($result, ',', -1);

    // Quebra o texto nas "," e transforma cada pedaço numa matriz
    $divisor = explode(",", $result);

    // Inverte os pedaços
    $reverso = array_reverse($divisor);

    // Junta novamente a matriz em texto
    $final = implode(" ", $reverso); // Junta com espaço

    echo $final;      

  echo "</blockquote>";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.