gevo12321 Posted June 23, 2007 Share Posted June 23, 2007 im completely new to php and programing all together and i have a question. I have a mysql database that has a date and info columns i want to be echo the last three rows in chronological order starting from the latest one on top how can i do this? plz help me thank you Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/ Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 use this in ur mysql query: this is just an example. select * from table1 order by id desc limit 3. Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280737 Share on other sites More sharing options...
gevo12321 Posted June 23, 2007 Author Share Posted June 23, 2007 thanks and also is there a way to make the date look like a regular date? in the format: mm/dd/yyyy also could you tell me what im doing wrong in the php trying to display the info <?php include 'connect.php'; $news = mysql_query("SELECT date,title FROM news ORDER BY date desc LIMIT 3") or die(mysql_error()); echo $news; ?> thank you very much ps im trying to display the info on 3 different lines but i have no idea how thx Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280738 Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 Yes use thsi code in sql query date_format('d-m-Y') OR In php. When u get date from db like $date1=$row->appdate; $date=date('d-m-Y',strtotime(#date1)); Echo $date; Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280740 Share on other sites More sharing options...
gevo12321 Posted June 23, 2007 Author Share Posted June 23, 2007 thx for the help yet i have another problem the following code is for only for today's date i guess? <?php include 'connect.php'; mysql_query("SELECT date,title FROM news ORDER BY date desc LIMIT 3") or die(mysql_error()); $date1=$row->appdate; $date=date('m-d-Y',strtotime($date1)); echo $date; ?> because i guess all i get is 06-23-2007 but i'm trying to see the the dates and titles in chronological order im sorry if i dont make sense plz help me Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280745 Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 try this code bcoz u have to fetch ur data 1st than assign it to variables. ok try: <?php include 'connect.php'; $query=mysql_query("SELECT date,title FROM news ORDER BY date desc LIMIT 3") or die(mysql_error()); $row=mysql_fetch_object($query); $date1=$row->date; $title=$row->tiltle; $date=date('m-d-Y',strtotime($date1)); echo $date; echo "<br>"; echo $title; ?> Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280751 Share on other sites More sharing options...
gevo12321 Posted June 23, 2007 Author Share Posted June 23, 2007 thank you very much the following code works <?php include 'connect.php'; $query=mysql_query("SELECT date,title FROM news ORDER BY date desc LIMIT 3") or die(mysql_error()); $row=mysql_fetch_object($query); $date1=$row->date; $title=$row->title; $date=date('m-d-Y',strtotime($date1)); echo $date; echo " "; echo $title; ?> but it doesn't display the last 3 rows it only displays one how can i make it all 3? thank you Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280755 Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 Oh sorry for that u have to use while loop here. <?php include 'connect.php'; $query=mysql_query("SELECT date,title FROM news ORDER BY date desc LIMIT 3") or die(mysql_error()); while($row=mysql_fetch_object($query));{ $date1=$row->date; $title=$row->title; $date=date('m-d-Y',strtotime($date1)); echo $date; echo " "; echo $title; } ?> Try it and let me know it works or not. Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280757 Share on other sites More sharing options...
gevo12321 Posted June 23, 2007 Author Share Posted June 23, 2007 nop didn't work it showed today's date only by the way i really appreciate ur help thx Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280758 Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 What is ur table structure in db, Can u show me that. Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280760 Share on other sites More sharing options...
gevo12321 Posted June 23, 2007 Author Share Posted June 23, 2007 news Field Type Null Default newsnumber int(10) No date date No title varchar(300) No news mediumtext No Indexes: Keyname Type Cardinality Field PRIMARY PRIMARY 4 newsnumber Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280762 Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 try to put newsnumber in ur order clause except of date. Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280763 Share on other sites More sharing options...
gevo12321 Posted June 23, 2007 Author Share Posted June 23, 2007 so u want me to do this? <?php include 'connect.php'; $query=mysql_query("SELECT newsnumber,title FROM news ORDER BY date desc LIMIT 3") or die(mysql_error()); while($row=mysql_fetch_object($query)); { $date1=$row->date; $title=$row->title; $date=date('m-d-Y',strtotime($date1)); echo $date; echo " "; echo $title; } ?> still doesn't work though im sorry im a complete noob at this Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280764 Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 No like this: <?php include 'connect.php'; $query=mysql_query("SELECT * FROM news ORDER BY newsnumber desc LIMIT 3") or die(mysql_error()); while($row=mysql_fetch_object($query)); { $date1=$row->date; $title=$row->title; $date=date('m-d-Y',strtotime($date1)); echo $date; echo " "; echo $title; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280766 Share on other sites More sharing options...
gevo12321 Posted June 23, 2007 Author Share Posted June 23, 2007 still didnt work so i tryed this: <?php require_once('connect.php'); $query=mysql_query("SELECT date,title FROM news ORDER BY date desc LIMIT 3") or die(mysql_error()); while($row=mysql_fetch_array($query)); { print_r($row); $date1=$row['date']; $title=$row['title']; $date=date('m-d-Y',strtotime($date1)); echo $date; echo " "; echo $title; } ?> that didnt work either wow i have no idea what to do Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280769 Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 Newsnumber is primary field mean primary key. If yes than please in ur select statement order by newsnumber not date. Like: SELECT * FROM news ORDER BY newsnumber desc LIMIT 3 Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280772 Share on other sites More sharing options...
gevo12321 Posted June 23, 2007 Author Share Posted June 23, 2007 forget whatever i said before i found the problem we don't need a semicolon after the while statement {} serves as a semicolon and it works now thx very much man couldn't have done it without u Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280775 Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 In every while loop u have to put these {} for condition. But ur problem is solved without it. Glad that u got it working. Quote Link to comment https://forums.phpfreaks.com/topic/56822-solved-last-3-dates/#findComment-280780 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.