Page 1 of 1

Reversing Elements In An Array

Posted: Thu Oct 10, 2002 8:18 pm
by Little Spy
i am making an aim subprofile script to set the mood for you, and an trying to get the array to output all the people's screen names in reverse table order because i want the most recent users (the one's most recently) added to the table to be displayed

Code: Select all

$sql = mysql_query("SELECT * FROM userinfo");
  while ($row = mysql_fetch_assoc($sql)) {
    $whoviewed = $rowї'sn'];
    echo $whoviewed . "<br>";
  &#125;
Currently it is outputing the screen names in table order.

Here is my table setup

id INT(3) NOT NULL auto_increment,
sn MEDIUMTEXT,
dateofv MEDIUMTEXT,
hits INT(5) DEFAULT 1,
PRIMARY KEY(id)

Posted: Thu Oct 10, 2002 8:26 pm
by hob_goblin
do this:

Code: Select all

$sql = mysql_query("SELECT * FROM userinfo"); 
  while ($row = mysql_fetch_assoc($sql)) &#123; 
    $whoviewed&#1111;] = $row&#1111;'sn']; 
  &#125;
$whoviewed = array_reverse($whoviewed);
  foreach($whoviewed as $sn)&#123;
     echo $sn."/n<br>";
  &#125;

Posted: Thu Oct 10, 2002 8:29 pm
by Little Spy
thnx that works but i found a little easier way from a freind to just to do this in the query

Code: Select all

$sql = mysql_query("SELECT * FROM userinfo ORDER BY ID DESC");

Posted: Thu Oct 10, 2002 9:16 pm
by hob_goblin
Little Spy wrote:thnx that works but i found a little easier way from a freind to just to do this in the query

Code: Select all

$sql = mysql_query("SELECT * FROM userinfo ORDER BY ID DESC");
yeah, about 5 seconds after i posted i thought of that way as well.

they both work, and it's good to know both, you might need array_reverse() in the future