Jump to content

Archived

This topic is now archived and is closed to further replies.

87dave87

Displaying row by rating and date

Recommended Posts

Hi,

This is my page which displays data in one of my database tables: -

http://www.emulators.cc/windows/atari2600/test.php

As you can see, the rows are listed by the rating (im not sure how its done this - is this because there is more text in the rating field as there is more html code for each star?). If the rating is the same as another row I then want the rows displayed in order of rating and which is the most up to date record.

At the moment it is not recognizing the year in the date field.

The php code for the above page is: -

<?php 
$database="database"; 
mysql_connect ("localhost", "user", "pass"); 
@mysql_select_db($database) or die( "Unable to select database");
$sql = mysql_query("SELECT emulator, version, DATE_FORMAT(updated, '%d %b %Y') AS updated, rating, readme, screenshot, download FROM windows_atari2600 order by rating desc, updated desc");
while ($get_info = mysql_fetch_row($sql)) { 
  echo "<tr>"; 
  foreach ($get_info as $field) echo "<td>$field</td>\n";
  echo "</tr>\n";
}
?>

Share this post


Link to post
Share on other sites
well, since you're selecting your date_format as 'updated', you're overloading that column name, and the order by is sorting based on the string result. you need to name the formatted date a different name so you can still order by updated.

Share this post


Link to post
Share on other sites
Ok, ive done that and it works: -

<?php 
$database="irrm3a_emulators"; 
mysql_connect ("localhost", "irrm3a", "reallydo3t"); 
@mysql_select_db($database) or die( "Unable to select database");
$sql = mysql_query("SELECT emulator, version, DATE_FORMAT(updated, '%d %b %Y') AS newupdated, rating, readme, screenshot, download FROM windows_atari2600 order by rating desc, updated desc");
while ($get_info = mysql_fetch_row($sql)) { 
  echo "<tr>"; 
  foreach ($get_info as $field) echo "<td>$field</td>\n";
  echo "</tr>\n";
}
?>

Do I really need the AS newupdated part? Its not actually doing anything is it?

Share this post


Link to post
Share on other sites
the "AS newupdated" is simply renaming the column you pull out. you then can use the 'newupdated' result in your script to display the formatted date.

Share this post


Link to post
Share on other sites
Ok, but when I display 'updated' after the date format its already changed, and as I dont use newupdated.. it can be removed.

Share this post


Link to post
Share on other sites
[quote author=87dave87 link=topic=112427.msg456263#msg456263 date=1161608717]
Ok, but when I display 'updated' after the date format its already changed, and as I dont use newupdated.. it can be removed.
[/quote]

sure. what i posted above is the only advantage to renaming a column. otherwise, it's completely optional. if you're not using the formatted date, you can actually remove the whole DATE_FORMAT function and only select updated by itself.

Share this post


Link to post
Share on other sites

×

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.