Jump to content


Photo

Displaying row by rating and date


  • Please log in to reply
5 replies to this topic

#1 87dave87

87dave87
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 23 October 2006 - 12:30 PM

Hi,

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

http://www.emulators...ri2600/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";
}
?>

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 23 October 2006 - 12:46 PM

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 87dave87

87dave87
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 23 October 2006 - 12:54 PM

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?

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 23 October 2006 - 01:01 PM

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 87dave87

87dave87
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 23 October 2006 - 01:05 PM

Ok, but when I display 'updated' after the date format its already changed, and as I dont use newupdated.. it can be removed.

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 23 October 2006 - 01:19 PM

Ok, but when I display 'updated' after the date format its already changed, and as I dont use newupdated.. it can be removed.


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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users