954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

php mysql question


BOOK
DATE
TIME
LEAVING FROM
BOAT
DEPTH
DIVE SITE
MIN QUAL

<?
include("conn.php");

$res=mysql_query("SELECT * FROM dives");
if(mysql_num_rows($res)==0) echo "No data in table";


for($i=0;$i/>
<?=$row[date]?>
<?=$row[time]?>
<?=$row[leaving_from]?>
<?=$row[boat]?>
<?=$row[depth]?>
<?=$row[dive_site]?>
<?=$row[min_qual]?>

<?
}

Hi I am using this code to display a mysql database of dates etc on my web page. The problem with it is the dates in the database are in a random order but it would be better if they would be displayed on the webpage in date order. It would cause too many complications to change the order in the database. Is there an easy way to do this please?

bjg5858
Newbie Poster
10 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Try making your select statement into:

SELECT * FROM dives ORDER BY date DESC

replace date with the name of the field that holds the dates in your dives table, and also you can change DESC to ASC depending on which order you want it.

Let me know if it doesn't work.

scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

No that hasn't worked I think that it would but I have got the date field of my database as varchar.
I am trying to change the field but the date format needs to have a YYMMDD format and i want a DDMMYY format. Is there an easy way around this or do i need to write php code to convert user input from DDMMYY to YYMMDD??

bjg5858
Newbie Poster
10 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

can't you set the date field to that format?

Anyways see this page:

http://www.php.net/manual/en/function.date-format.php


Sucks it doesn't work because I was counting on it since I too am going to have to do something similar soon....

scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

This is how you convert date strings from DDMMYY to YYMMDD:

$userinput; //lets assume this variable has a date in DDMMYY format
$format = "y/m/d"; //this is the format we are going to tell date to output the string in. y , d and m will output 2 digit representations of year day and month repectively (case sensitive).

$timestamp = strtotime($userinput); //get a Unix timestamp from the user input so we can use it with the date function.

$new_date = date($format, $timestamp); //$newdate is now a string formatted YY/MM/DD
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

Thanks for your help that's not so bad after all.

bjg5858
Newbie Poster
10 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You