分页代码,不能得到结果

时间:2014-09-01 09:01:44

标签: php sql pagination

我尝试将分页代码放入,因此我不会在同一页面上获得大量结果,但我没有收到任何错误,它只是输出整个数据库而不是2个结果。

我每页只需要2个结果,这就是我想要分页的原因。

$sql10 = "SELECT COUNT(id) FROM post WHERE id='".$kat."'";
$res10 = mysql_query($sql10) or die (mysql_error());
$row10 = mysql_fetch_row($res10);
$rows10 = $row10[0];

$page_rows = 2;
$last_page = ceil($rows10/$page_rows);

if($last_page < 1) {
    $last_page = 1;
} 

$pagenum = 1;

if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}

if ($pagenum < 1) {
    $pagenum = 1;
} else if ($pagenum > $last_page) {
    $pagenum = $last_page;
}

$limit = 'LIMIT' .($pagenum - 1) * $page_rows .',' .$page_rows;
$sql = "SELECT id FROM kategorier WHERE id='".$kat."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($res) == 1) {
    $sql2 = "SELECT * FROM emner WHERE kategori_id='".$kat."' ORDER BY emne_svar_dato   DESC";
    $res2 = mysql_query($sql2) or die (mysql_error());
    $paginationcrtrls = '';

    if($last_page != 1) {
        if ($pagenum > 1) {
            $previous = $pagenum - 1;
            $paginationcrtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">previous</a>  &nbsp; &nbsp; ';
            for($i = $pagenum-4; $i < $pagenum; $i++) {
                if($i > 0) {
                    $paginationcrtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
                }
            }
        }

    $paginationcrtrls .= ''.$pagenum.' &nbsp; ';

    for($i = $pagenum+1; $i <= $last_page; $i++) {
        $paginationcrtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';

        if($i >= $pagenum+4) {
            break;
        }
    }

    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationcrtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">next page</a> ';
    }
}

if (mysql_num_rows($res2) > 0) {
    $emner = "";
    $emner .= "<table width='100%' style='border-collapse: collapse;'>";
    $emner .= "<tr><td colspan='3'><a href='index.php'>retur til forum</a>".$logged."<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_assoc($res2)) {
        $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>";
    echo $emner;
} else {
    echo "<a href='index.php'>retuner til forum </a><hr />";
    echo "<p>der er ikke nogen emner i kategorien.".$logged."</p>";
}
} else {
echo "<a href='index.php'>retuner til forum </a><hr />";
echo "<p>du prøver at se en kategori som ikke eksitere.";
}
?>

0 个答案:

没有答案