mysql - PHP - limit 10 offset 1 if rows are greater than 20 -
i'm displaying top 10 newest , last 10 oldest topics after 10 newest database. i'm able display need in top 10 topics.my problem when have 21 topics in database, last 10 topics displayed according want,but when have 20 topics,the last topic in newest still in oldest.to make more clear,here reference picture.
-this happen when have 21 or more topics in database-
-this happen when have 20 topics in database-
i don't want topic repeated happen when have 20 topics. here code fetching last 10 topics database:
// fetching last 10 topics forum function history() { $sql = "select * ( select f_id id, f_title name,f_dept dept,f_last_view last_view forum order last_view asc limit 10 offset 1 ) `table` order last_view desc "; //-run query against mysql query function $result=mysql_query($sql) or die(mysql_error()); $history = array(); //-create while loop , loop through result set while (($row = mysql_fetch_assoc($result)) !== false){ $history[] = array( 'id' => $row['id'], 'name' => $row['name'], 'dept' => $row['dept'], 'last_view' => $row['last_view'], ); } return $history; }
p.s. know mysql_* deprecated please bear me. thank in advance
your question little unclear - want newest 10 (which have working) , oldest 10? or newest 10 , next newest 10?
i'll assume mean newest 10 , next newest 10.
your problem appears in understanding of offset keyword in sql. offset causes number of results skipped.
based on this, sorting results ascending last view (which assume form of timestamp) - oldest newest, picking 10 of them, starting @ second one.
if wanted 11-20th newest results try
order last_view desc limit 10 offset 10
within query.
also see this stackoverflow question more detailed answer
Comments
Post a Comment