日期:2014-05-16  浏览次数:21055 次

自己编写的php+mysql实现新闻系统中“上一条”,“下一条”的简单程序
我也是个PHP新手,最近在做一个网站的新闻发布系统中,需要用到“上一条””下一条“这样的效果,就自己编写一个,由于是新手,不足之处,还望高手指正。
$colname_rs = "-1";
if (isset($_GET['id'])) {
  $colname_rs = $_GET['id'];
}
mysql_select_db($database_conn, $conn);
$query_rs = sprintf("SELECT * FROM news WHERE id = %s", GetSQLValueString($colname_rs, "int"));
$rs = mysql_query($query_rs, $conn) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);
$next_sql="SELECT * FROM news WHERE id >$colname_rs ORDER BY id ASC LIMIT 1  ";
$rsnext= mysql_query($next_sql, $conn) or die(mysql_error());
$row_next = mysql_fetch_assoc($rsnext);
$prev_sql="SELECT * FROM news WHERE id <$colname_rs ORDER BY id DESC LIMIT 1  ";
$rsprev= mysql_query($prev_sql, $conn) or die(mysql_error());
$row_prev = mysql_fetch_assoc($rsprev);
$max_sql="SELECT MAX(id) FROM news";
$rsmax = mysql_query($max_sql, $conn) or die(mysql_error());
$row_max = mysql_fetch_array($rsmax);
$min_sql="SELECT MIN(id) FROM news";
$rsmin = mysql_query($min_sql, $conn) or die(mysql_error());
$row_min = mysql_fetch_array($rsmin);
$min_id=mysql_result($rsmin,0);
$max_id=mysql_result($rsmax,0);
switch ($colname_rs)
{
case $min_id :
$ptext="没有了";
$ntext= "<a href=\"index.php?id=".$row_next['id']."\">".$row_next['tiltle']."</a>";
 break;
case $max_id:
$ptext= "<a href=\"index.php?id=".$row_prev['id']."\">".$row_prev['tiltle']."</a>";
$ntext="没有了";
  break;
default:
$ptext= "<a href=\"index.php?id=".$row_prev['id']."\">".$row_prev['tiltle']."</a>";
$ntext= "<a href=\"index.php?id=".$row_next['id']."\">".$row_next['tiltle']."</a>";
}


在需要用到的地方引用如下代码:
上一条:
<?php echo $ptext; ?>

 
下一条:
<?php echo $ntext; ?>

实现过程有些复杂,哪位有更好的方法,请不惜吝教