Domanda

I'm trying to make it so messages are displayed like this

*Old Message

Sooner Message

Newer Message

Latest Message*

I tried a few different ways. By putting

  SELECT * FROM place_chat WHERE whereto = '".mysql_real_escape_string($where)."' ORDER BY     id DESC LIMIT 7"

^ But that just displays the results with the latest message at the top, so I tried this.

  SELECT * FROM place_chat WHERE whereto = '".mysql_real_escape_string($where)."' ORDER BY     id ASC LIMIT 7" 

^ But then I released that's just going to ONLY display the oldest messages rather than adjust the way it displays and to be sure and tested it. It just displayed the oldest messages.

Could someone please explain the method of doing this? I've been trying to do it for a while now.

È stato utile?

Soluzione

You need to get the newest 7 records first, then reorder them:

SELECT *
FROM
(
    SELECT *
    FROM place_chat 
    WHERE whereto = mysql_real_escape_string($where)
    ORDER BY id DESC
    LIMIT 7
) newest_place_chat
ORDER BY id ASC

Altri suggerimenti

Since you set the tag 'php' I assume you query the database from within a php script that also converts the query result into an output, most likely a html table in this case.

You can simply turn around the order in which you traverse the result array inside php. That way you don't have to change anything in your query.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top