Results 1 to 5 of 5

Thread: How to reverse an array

  1. #1
    Join Date
    Mar 2010
    Posts
    360

    How to reverse an array

    Hello,
    This is my problem, I made a shoutbox. But when I retrieve messages if I put an ORDER CSA, I only get the first posts. And if I put an ORDER ESCR, it gives me the good, but the oldest is at the top of the list. In other words how can I reverse the array, because this will solve my problem. Any help is appreciated. Thank you.

  2. #2
    Join Date
    Dec 2009
    Posts
    202

    Re: How to reverse an array

    I can only answer only one part of your question. Can you show us your queries complete? It should look like this:
    SQL - Select
    Code:
    SELECT * FROM shoutbox ORDER BY id
    I think this should work for you.

  3. #3
    Join Date
    Dec 2009
    Posts
    213

    Re: How to reverse an array

    Hello,
    Check out the following code, this may help you

    Code:
    <? Php
    / / ...
    $ rshts = mysql_query('SELECT * FROM shoutbox ORDER BY id DESC LIMIT 0, 8')
    	
    	/ / Can we make a loop to display all results
    	while ( $ shts = mysql_fetch_array($ rshts) )
    	(
    	$ msg = bbcode(htmlspecialchars($ shts['message']));
    	$ pse = htmlspecialchars($ shts['username']);
    	$ hre = date('H \ hi', $ shts[' timestamp ']);
    	$ dt = date_naturelle($ shts['timestamp'])
    	
    	echo '
    	<tr>
    		<td> '.$ pse.'</ td>
    		<td> <a title = "Message posted on '.$ dt.'">'.$ hre.'</ a> </ td>
    		<td style="width: 70%;"> ".$ msg.'</ td>
    	</ tr>
    	
    	';
    	)

  4. #4
    Join Date
    Mar 2010
    Posts
    162

    Re: How to reverse an array

    This is a more practical code, just try it

    Code:
    <? Php
       / / ON LINE HAS A MySQL
    
       $ sq = mysql_query("SELECT * FROM shoutbox");
       $ ness = mysql_num_rows($ sq);
    
       if($ ness > 10) $ lmt = "LIMIT" . ($ ness - (8 + 1)) . ", 10", / / For the first index is 0!
       else $ lmt = "";
    
       $ sq2 = mysql_query("SELECT * FROM shoutbox ORDER BY id ASC" . $ lmt . " ")
    
       while($ msg = mysql_fetch_array($ sq2))
       (
          / / HERE YOU YOUR MESSAGE POSTERS
       )
    ?>

  5. #5
    Join Date
    Mar 2010
    Posts
    202

    Re: How to reverse an array

    The ORDER BY id ASC posters you in the right order, but not the right messages, and the ORDER BY id DESC will show you the good, but in the wrong. It will therefore be a mix of both, should select the latest posts in the correct order. For the "LIMIT x, y, the first row x is 0 so if you have no entries in your database, the latter has a rank (n - 1) (the first row to 0, the second has a rank 1 etc. ...). If you want the last 8 messages, you must take as a starting point for the "LIMIT" the eighth before the final, which will thus rank ((n - 1) - 8). Except that in the code, number of messages n is represented by $ ness. Then, you want 8 posts, which gives:
    Other - Select
    Code:
    LIMIT (($ ness - 1) - 8), 8

Similar Threads

  1. How does Reverse SEO work
    By Haleema in forum Technology & Internet
    Replies: 5
    Last Post: 13-07-2011, 01:47 AM
  2. How the reverse() function can be used in C++
    By BossBattle in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 04:55 PM
  3. Assigning an array to an array
    By MACE in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 05:19 PM
  4. Reverse lookup a SID
    By Kool in forum Active Directory
    Replies: 1
    Last Post: 20-07-2009, 10:53 PM
  5. What is Reverse Engineering?
    By Amol21 in forum Software Development
    Replies: 4
    Last Post: 11-02-2009, 09:04 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,882,019.67185 seconds with 17 queries