php - Next and previous buttons -


i know there libraries etc use sorted im there code.

a little code , it's trying do. have mysql table there various news articles , grouped in categories of news.

i have managed forward button working. looks next news article in same category. works , code below.

//gets next story same story type in line.  $query= "select * news storytype2 = '$storytype2' , id > '$currentid'"; $result = mysql_query($query) or die ("error in query: $query " . mysql_error()); $row = mysql_fetch_array($result);  $num_results = mysql_num_rows($result);  if ($num_results > 0){     echo "<td width=\"20%\"align=\"right\"><a href=\"news.php?id=".$row['id']."\"><img title=\"next news\" src=\"webimg/forwardarrow.png\"/></a></td></tr>"; }else{    echo "<td width=\"20%\"align=\"right\"></td></tr>"; } //end of next button 

however, when try same previous button. ever seem first id of category regardless of iteration is. example, if on news article 10 , try go previous 1 has id of 7 automatically show first news article within category, id 4.

below code.

//gets next story same story type in line.  $query= "select * news storytype2 = '$storytype2' , id < '$currentid'"; $result = mysql_query($query) or die ("error in query: $query " . mysql_error()); $row = mysql_fetch_array($result);  $num_results = mysql_num_rows($result);  if ($num_results > 0){     echo "<td width=\"20%\"align=\"left\"><a href=\"news.php?id=".$row['id']."\"><img title=\"previous news\" src=\"webimg/backarrow.png\"/></a></td>";         }else{    echo "<td width=\"20%\"align=\"left\"></td>"; } //end of next button 

what have done wrong?

thanks

neither of queries correct. "next" code selects any row id higher current, not next one; if next one, it's accident.

you should use order by , limit control row selected:

next:

select *  news  storytype2 = '$storytype2' , id > '$currentid' order id limit 1 

previous:

select *  news  storytype2 = '$storytype2' , id < '$currentid' order id desc limit 1 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -