上一篇文章对我没那么帮助。但是这段代码会给我3个我喜欢的结果,但不知何故它不会显示我链接所以我只得到第一页atm。所以我觉得我在这里缺少一些东西,我是php和学习的新手。
<?php
include_once("connect.php");
$kat = $_GET['kat'];
$sql = "SELECT COUNT(id) FROM kategorier WHERE id='".$kat."'";
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
$rows = $row[0];
$page_rows = 3;
$last = ceil($rows/$page_rows);
if($last < 1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$sql = "SELECT * FROM emner WHERE kategori_id='".$kat."' ORDER BY emne_svar_dato DESC $limit";
$query = mysql_query($sql);
$textline1 = "Testimonials (<b>'".$rows."'</b>)";
$textline2 = "Page <b>'".$pagenum."'</b> of <b>'".$last."'</b>";
$paginationCtrls = '';
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> ';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
}
}
}
$paginationCtrls .= ''.$pagenum.' ';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
}
}
$emner = "";
$emner .= "<table width='100%' style='border-collapse: collapse;'>";
$emner .= "<tr><td colspan='3'><a href='index.php'>retur til forum</a><hr /></td></tr>";
$emner .= "<tr style='background-color: #dddddd;'><td>emne titel</td><td width='65' align='center'>besvar</td><td width='65' align='center'>set</td></tr>";
$emner .= "<tr><td colspan='3'><hr /></td></tr>";
while($row = mysql_fetch_array($query)){
$tid = $row['id'];
$titel = $row['emne_titel'];
$set = $row['emne_visninger'];
$dato = $row['emne_dato'];
$lavet = $row['emne_laver'];
$emner .= "<tr><td><a href=vis_emne.php?kat=".$kat."&tid=".$tid.">".$titel."</a><br /><span class='post_info'>post af: ".$lavet." den ".$dato."</span></td><td align='center'>0</td><td align='center'>".$set."</td></tr>";
$emner .= "<tr><td colspan='3'><hr /></td></tr>";
}
$emner .="</table>";
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
div#pagination_controls{font-size:21px;}
div#pagination_controls > a{ color:#06F; }
div#pagination_controls > a:visited{ color:#06F; }
</style>
</head>
<body>
<div>
<h2><?php echo $textline1; ?> Paged</h2>
<p><?php echo $textline2; ?></p>
<p><?php echo $emner; ?></p>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>