doddsey_65 Posted September 12, 2010 Share Posted September 12, 2010 hi, i want to select the 5 newest records from a table in the database. the date they were entered is in a column called post_time and this is a timestamp(000-00-00 00:00:00). how would i go about this? Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 12, 2010 Share Posted September 12, 2010 Something along these lines... // Query database for latest 5 records $myQuery = mysql_query("SELECT * FROM `YOURDATABASEHERE` ORDER BY `timestamp` DESC LIMIT 5"); // While the query runs we will echo each row while($myQuery = mysql_fetch_assoc($myQuery)) { print_r($myQuery); // Print out each row as an array } That should display the latest 5 rows, just append the "print_r($myQuery);" to format the data recieved ect... Regards, Paul. Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted September 12, 2010 Author Share Posted September 12, 2010 thanks, another thing is i want to display the contents of another table but if the content has more than say 50 characters then i want to replace all remaining characters with three dots to signify a continuation. how would that be done? Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 12, 2010 Share Posted September 12, 2010 Leading line syndrome right? I used to get that... Try this... if(strstrlen($row[fieldname]) > 50) { $leadingLine = trim(substr($row[fieldname],0,50)); $leadingLine = ''.$leadingLine.'...'; } else { $leadingLine = $row[fieldname] } Off the top of my head buddy, not 100% sure it is correct but give it a go Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 12, 2010 Share Posted September 12, 2010 One limitation of the above code snippet is that it doesn't break the line on a full word. This is the typical behavior when getting an excerpt of a line. Otherwise you could end up with something very unintended if a word gets broken in the wrong place. Here is a function I keep in my personal library: function truncateString($string, $length, $ellipse='...') { if (strlen($string) <= $length) { return $string; } return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse; } Just pass the string, the maximum length, and the value to use for the ellipse (optional, default is '...') Quote Link to comment 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.