database help, sort by date

Discussion in 'Databases' started by izlik, Oct 13, 2007.

  1. #1
    Hello.

    This code bellow is supposed to pick out 10 items from my database and list them wich it do. im trying to make it list them depending on date and need some help as i cant get it to work. right now it list 10 items with a date from last month and i have no clue why, can someone help me make this code able to list the 10 that has the latest date in the database? my date format in the database is like t his "2007-10-10 13:59:21" and the collumn where the date resides is in "added" in the table "games"


    
    <table class="main2" id="table12" border="1" width="102">
    						<tr>
    							<td class="entry" bgcolor="#FFFF00" 
    
    bordercolorlight="#FF6600" bordercolordark="#FF6600">
    						10 Newest games<div class='new games'>
    <?php
    $con = mysql_connect("localhost","login,"password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("database", $con);
    $result = mysql_query("SELECT * FROM games WHERE id > 0 ORDER BY `name` 
    
    LIMIT 10"); 
    
    while($row = mysql_fetch_array($result))
      {
      echo '<a href="http://mypage.com/index.php?params=game/'.$row['id'].'">'.$row['name'].'</a>';
      echo "<br />";
      }
    
    mysql_close($con);
    ?><?for ($i=0;$i<$resultsCount;$i++){
    $num = $i+1;
    echo $num.":".$results[$i];
    }
    ?></div></td>
    						</tr>
    						</table>
    				
    PHP:
     
    izlik, Oct 13, 2007 IP
  2. digipren

    digipren Active Member

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    "SELECT * FROM games WHERE id > 0 ORDER BY added DESC, NAME limit 10" doesn't work ?
     
    digipren, Oct 13, 2007 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Apparently not digipren :( it dont show the games that i added just a few day's ago, it just picks the one at top of the database and shows them insteed wich are way old. someone said it can be becuase the collun "added" is not a "valid date field" if this is true, how can i solve that ?
     
    izlik, Oct 13, 2007 IP
  4. digipren

    digipren Active Member

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    what is the type of added ? String, datetime ?

    and what is <?for ($i=0;$i<$resultsCount;$i++){
    $num = $i+1; echo $num.":".$results[$i]; }
    ?>
    supposed to do ?
     
    digipren, Oct 13, 2007 IP
  5. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #5
    that last bit of code ignore that please, it was pasted by misstake, i have removed that.

    and "added" is a normal collumn in my "games" table. it's supposed to be a datetime
     
    izlik, Oct 13, 2007 IP
  6. digipren

    digipren Active Member

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    "ORDER BY added DESC" should work...
    if you want, dump a few lines of your table and I'll try the query on my side.
     
    digipren, Oct 13, 2007 IP