tags:

views:

586

answers:

3
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);
+3  A: 

You are propably trying to reverse the whole resultset, try something like this:

function outputscores($mysqlres,$reverse=false)
{
   $results = array();
   while($row = mysql_fetch_array($mysqlres)){
    print('I found a result: <pre>'.print_r($row, false).'</pre>'); //debugging purposes
    $results[] = $row;
   }
   if($reverse) { $results = array_reverse($results); }
   foreach($results as $row)
   {
    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 />';
  }
}

Oh yes indeed as soulmerge says, you should feed the function a mysql resource and not an actual query. Thus like this:

$query = "SELECT field FROM table";
$res = mysql_query($query);
output_scores($res, true);

In regard to your comment, make sure your query is actually returning results, test it in phpMyAdmin or something or add a simple print in the while loop as I did above.

Pim Jager
Warning: Invalid argument supplied for foreach() in *snip* on line 55
a2h
The records should be reversed in the SQL query itself, its not efficient to loop through the records twice and reverse them through PHP.
Click Upvote
@click Upvote. It woul indeed be faster to have mysql return them in reverse order. However, the OP specificly asked for the array to be reversed.
Pim Jager
IMO he didn't know that the records could be reversed through SQL, but I agree, you could be right
Click Upvote
The query does not necessarily return results so that's an issue there :/
a2h
AH ok, With the new function just remove the print statement from the while and there should be no warnings anymore since $results is now always an Array, even when there are no results.
Pim Jager
+1  A: 

If I get this right, $mysqlquery should be a mysql query result, not an array. There are two cases where mysql_fetch_array() does not return an array:

  1. Either the parameter is not a query result resource (verify with var_dump($mysqlquery)), or
  2. There is simply no result (mysql_fetch_array() returns FALSE).

I hope you're not passing the query itself to mysql_fetch_array() as the name of the variable implies. You must call mysql_query() before you can fetch anything.

The reversing itself can be done by fetching all rows and calling array_reverse(). Example:

$rows = array();
while ($row = mysql_fetch_array($result)) {
    $rows[] = $row;
}
array_reverse($rows);
soulmerge
I've edited the question to clarify that
a2h
+3  A: 

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);
Click Upvote
I've edited the question to clarify that
a2h
I've edited the answer to clarify
Click Upvote
$mysqlquery .= " ORDER BY ".$gid.".timestamp DESC"; - doesn't seem to be doing the trick
a2h
Another edit ._.
a2h
It should do the trick. You have some typos in your last comment, look closer on Click Upvotes answer.
Björn
a2h, can you add the line: echo $mysqlquery.'<br>'; to the code just before the mysql_query($mysqlquery); line in the showOutput() function, then copy and paste the full query that you get?
Click Upvote
SELECT * FROM reflex LIMIT 25 **first, then** SELECT * FROM reflex,users WHERE users.lastcheck < reflex.timestamp
a2h
And the prob. is that no records are being returned? See if the reflex and users tables are populated, and see if the users.lastcheck is indeed less than the timestamp somewhere. If the reflex table is empty or the where conditions aren't met it won't return any results.
Click Upvote
My god I'm an idiot I forgot to set true
a2h
OK, I give up with that. I'm resorting to outputscores("SELECT * FROM $gid ORDER BY timestamp DESC LIMIT 25"); But thanks for the great help anyway, so I'll mark your answer as correct
a2h
A side note about security :Please, for the sake of the internet and all your users, don't use mysql_query. Please use PDO http://php.net/pdo. It automatically escapes your variables so you don't have SQL exploits.And if you must use mysql_query (for legacy code) make sure to run each variable through http://php.net/mysql_real_escape_string before using it in a query string.
Paul Tarjan