Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing mysql_fetch_array()

Tags:

php

mysql

function outputscores($mysqlquery,$reverse=false)
{
    while($row = mysql_fetch_array($mysqlquery))
    {
        echo '<img src="img/date.png" /> ' . date("j M Y",strtotime($row['timestamp'])) . '
        <img src="img/time.png" /> ' . date("H:i:s",strtotime($row['timestamp'])) . '
        <img src="img/star.png" /> '.$row['score'].$_GET["scoretype"].'<br />';
    }
}

I need to reverse the array if $reverse is set to true, but PHP says that the mysql_fetch_array's output is not a valid array. Neither is $mysqlquery itself.

Is there any help?

Wow I've asked so many questions today ._.

EDIT

$result = mysql_query("SELECT * FROM $gid LIMIT 25") or throwerror(mysql_error());

outputscores($result);

EDIT2 Another possible call:

$result = mysql_query("SELECT * FROM ".$gid.",users WHERE users.lastcheck < ".$gid.".timestamp") or throwerror(mysql_error());

outputscores($result);
like image 935
unrelativity Avatar asked Jul 31 '26 04:07

unrelativity


1 Answers

Edit: Change your sql query to this:

$mysqlquery="SELECT * FROM $gid";

Change your function to this:

function outputscores($mysqlquery,$reverse=false)
{
    if ($reverse==true)
        $mysqlquery.=" ORDER BY id DESC LIMIT 25";
    else
        $mysqlQuery.=" LIMIT 25";
    $result=mysql_query($mysqlquery);
    while($row = mysql_fetch_array($result))
    {
       //Do output here
    }
}

Here, by adding the words ORDER BY id DESC you will make the records be ordered in descending order of the 'id' column in your table. By typing ASC instead of DESC you can have them ordered in ascending order. Also, you can replace 'id' with any other column from your table, for example timestamp, score, etc.

Edit: Alternatively, you could also make a different function for adding the LIMIT and ORDER BY clause to the query. Then you could do something like this:

$reverse=false;//or true
$mysqlquery=addOrderBy("SELECT * FROM $gid",$reverse);
outputScores($mysqlquery);
like image 75
Ali Avatar answered Aug 02 '26 20:08

Ali