Jump to content

Recommended Posts

This is a follow up from a post that I have already made in the PHP forum (http://www.phpfreaks.com/forums/php-coding-help/problem-with-array_reverse/) as a comment suggested that MySQL may be the way to go.

 

Basically, I am trying to get the most recent five records by date and then echo them in date ascending order. I can succesfully get the right records using

$tpall_games2 = mysql_query("
SELECT * ,  
DATE_FORMAT(`date`,'%W, %D %M %Y') AS showdate, 
HT.team_name as HomeTeam, 
VT.team_name as AwayTeam, 
COMP.comp_short_name as CP
FROM all_games
JOIN teams as HT ON all_games.home_team = HT.team_id
JOIN teams as VT ON all_games.away_team = VT.team_id 
JOIN competitions as COMP ON all_games.comp = COMP.comp_id 
WHERE 
$id IN (all_games.home_team, all_games.away_team) 
AND all_games.home_goals IS NOT NULL 
ORDER BY `date` DESC 
LIMIT 5
");
if (!$tpall_games2) {
    die("Query to show fields from All games Team Page table failed");
};

 

I have tried using array_reverse in the php output but the records still show in descending order.

 

I have also tried this suggestion from the other post

$query1 = "SELECT COUNT(`index_field`) FROM `table` WHERE whatever";
$result1 = mysql_query( $query );
$array1 = mysql_fetch_row($result);

$display_num = 5; // the number of records you want displayed
$start = $array1[0] - $display_num; // Total number of results, minus number to display

$query2 = "SELECT `records` FROM `table` ORDER BY `field` ASC LIMIT $start, $display_num";
// etc . . .

but no records are returned.

 

I have also tried to amend this example that I have seen elsewhere

SELECT * 
FROM (
  SELECT ... 
  FROM ... 
  ORDER BY ID ASC 
  LIMIT 3
) AS sq 
ORDER BY ID DESC

 

but that game me a 'Query to show fields failed' error.

 

Is there a way, via the query that I can reverse the ORDER BY to get the results to show in the right order? The "ORDER BY `date` DESC" is vital as I just want to get the most recent 5 results but then want to order those results by ASC.

 

Thanks in advance for any suggestions.

 

Steve

 

Link to comment
https://forums.phpfreaks.com/topic/226499-reversing-an-order-by-statement/
Share on other sites

why are you using a variable $id in this line?? the IN clause works like so

 WHERE column IN ('value1','value2',....)

 

That would be my naivety in MySQL! I thought that would be the correct way to return results when $id (which is a posted 'team.team_id') was in either of the named columns.

 

Would it be correct syntax to use

WHERE 
$id =  all_games.home_team AND all_games.home_goals IS NOT NULL 
OR $id = all_games.away_team AND all_games.home_goals IS NOT NULL

?

 

The first query gives the correct results (ie. the most recent 5 records by date) but would show

1st Jan 2011
1st Dec 2010
1st Nov 2010
1st Oct 2010
1st Sep 2010

when I need it to show

1st Sep 2010
1st Oct 2010
1st Nov 2010
1st Dec 2010
1st Jan 2011

 

Steve

 

maybe use some brackets

WHERE
($id =  all_games.home_team AND all_games.home_goals IS NOT NULL)
OR ($id = all_games.away_team AND all_games.home_goals IS NOT NULL)

 

and as for reversing the results, try

ORDER BY `date` ASC

and see what happens

oh!

totally misunderstood what you were after

 

I dare say you'll have to pull those most recent 5 and then use some PHP trickery to order them unless someone more learned in MySQL can help?

 

If you're just echoing a simple table from it, add each row value to an array like

$values=array();
while ($row=mysql_fetch_array($sql)) 
{ $values[]= "<tr>
<td>column1: {$row['column1']}</td>
<td>value: {$row['value1']}</td></tr>";

$values=array_reverse($values);
foreach ($values AS $row) {
echo $row;
}

 

... though looking at that situation and how convoluted it has become, I dare say someone else could suggest a much simpler way

 

just realised I forgot to close the while loop

$values=array();
while ($row=mysql_fetch_array($sql)) 
{ $values[]= "<tr>
<td>column1: {$row['column1']}</td>
<td>value: {$row['value1']}</td></tr>";
}
$values=array_reverse($values);
foreach ($values AS $row) {
echo $row;
}

  • 3 weeks later...

This turned out to be extremely simple in the end. All I had to do was wrap the original query in a separate set of brackets and then us another ORDER BY afterwards.

$l5all = mysql_query("(SELECT *, DATE_FORMAT(`date`,'%W, %D %M %Y') AS showdate, HT.team_name as HomeTeam, VT.team_name as AwayTeam, COMP.comp_short_name as CP
FROM all_games
JOIN teams as HT ON all_games.home_team = HT.team_id
JOIN teams as VT ON all_games.away_team = VT.team_id 
JOIN competitions as COMP ON all_games.comp = COMP.comp_id 
WHERE $id IN (all_games.home_team, all_games.away_team) AND all_games.home_goals IS NOT NULL 
ORDER BY date DESC LIMIT 5) ORDER BY date ASC
")

Steve

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.