Jump to content

[SOLVED] Nearest to Date


rascle

Recommended Posts

Hi

I have a mysql database that has two columns:

name and dayadded. I want to display 5 rows that dayadded is most like todays day.

Eg. todays day is the 11th and i have a database like:

name      dayadded

rhys        9

stephen    6

bob          7

tony        7

ryan        8

rod          9

 

then i want the 5 rows thats date are nearest to todays to be displayed

(that would display  bob,tony,ryan,rod,rhys).

 

How would i do this?

I was using:

 

<?php
$day = date("j");
$connect = mysql_connect("localhost","username","password");  
mysql_select_db(b25rasc_userlogin) or die(mysql_error());  
$data = MYSQL_QUERY("SELECT * FROM `games` WHERE dayadded >= $day LIMIT 5");  

while($rows = mysql_fetch_array($data))
{
echo"<font size=\"4\">";
echo $rows['text'];
echo "</font><br />";        
}

?>

 

But that doesnt display anything.

What should i do???

Thanks

 

Link to comment
Share on other sites

Sorry i didnt see that there but:

<?php
$day = date("j");
$connect = mysql_connect("localhost","b25rasc_b25rasc","rasclewelsh");  
mysql_select_db(b25rasc_userlogin) or die(mysql_error());  
$data = MYSQL_QUERY("SELECT  dayadded, abs(dayadded - $day) AS difference
FROM games Order By difference LIMIT 5");  

while($rows = mysql_fetch_array($data))
{
echo"<font size=\"4\">";
echo $rows['text'];
echo "</font><br />";        
}

?>

Still doesnt work + i dont know what to do?

Thanks

Link to comment
Share on other sites

Define: Still doesnt work? What is it doing?

 

$rows['text'] that is being used in the while() loop does not exist because the text column no longer is being selected in the query. You would need to add text to the list of files columns in the SELECT part of the query.

Link to comment
Share on other sites

I've got to ask if you have seen the definition of a SELECT query in the mysql documentation -

 

SELECT

    [ALL | DISTINCT | DISTINCTROW ]

      [HIGH_PRIORITY]

      [sTRAIGHT_JOIN]

     

[sql_SMALL_RESULT] [sql_BIG_RESULT] [sql_BUFFER_RESULT]
      [sql_CACHE | SQL_NO_CACHE] [sql_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [iNTO OUTFILE 'file_name' export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]][/quote]

In -
SELECT * FROM ...

and

SELECT dayadded, abs(dayadded - $day) AS difference FROM ...

 

The select_expr following the SELECT keyword are the items (columns) being selected. * means select all columns. A comma separated list means select the items in that list. You would include text in that comma separated list.

Link to comment
Share on other sites

So how would i add it so that it includes the month and the year.

I am currently using

<?php
$day = date("j");
$month = date("m");
$year = date("Y");
$connect = mysql_connect("localhost","b25rasc_b25rasc","rasclewelsh");  
mysql_select_db(b25rasc_userlogin) or die(mysql_error());  
$data = MYSQL_QUERY("SELECT  text,dayadded, abs(dayadded - $day) AS daydifference
FROM games Order By daydifference LIMIT 5");  

while($rows = mysql_fetch_array($data))
{
echo"<font size=\"4\">";
echo $rows['text'];
echo "</font><br />";        
}

?>

 

So how would i make it be to the nearest ,day ,month and year???

Thanks

Link to comment
Share on other sites

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.