PHP问题分页

时间:2009-07-26 11:59:41

标签: php pagination

我如何才能使我所在的页面无法点击?

$page = (isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);
$limit = ($page - 1) * 15;
$sql = mysql_query("SELECT * FROM log LIMIT $limit, 15");
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM log"),0);
$totalpages = ceil($totalres / 15);

for ($i = 1; $i <= $totalpages; $i++) {
  $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
}

<p><?php echo $pagination ?></p>

4 个答案:

答案 0 :(得分:3)

if ($i == $page)
  $pagination .= "$i ";
else
  $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";

答案 1 :(得分:1)

if内有for

if ( $i != $page) {
    //Your code.
} 
else 
{ 
    //Same page.
    $pagination .= "&nbsp;" . $page . "&nbsp;"
}

答案 2 :(得分:1)

只需要修改for循环以检查当前页面是什么。

for ($i = 1; $i <= $totalpages; $i++) 
{
    if ($i != $page)
        $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
    else
        $pagination .= " " . $i . " ";
}

答案 3 :(得分:0)

@RedBlueThing我很快就看到了你发布的帖子,虽然我从另一个stackoverflow question with a little modifying of the code得到了这个想法。

<?php
echo "<section>";

$link = mysqli_connect("localhost", "root", "root", "user_db");

$page = (isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);


$offset = 4;
$x = 5;

$limit = ($page - 1) * $offset; 

$sql = mysqli_query($link,"SELECT * FROM cfp_blogs;");
$totalres = mysqli_num_rows($sql);
$totalpages = ceil($totalres / $offset);

    $stmt = mysqli_query($link, "SELECT * FROM blogs LIMIT $limit, $offset;");
        if($stmt->num_rows > 0){
            while($obj = mysqli_fetch_object($stmt)) { 
                echo "<h1>$obj->title</h1>";
                echo "<p>$obj->content</p>";
                $stmt2 = $link->query("SELECT COUNT(id) AS Tot FROM blogs");
                if($stmt2->num_rows > 0){

                while ($obj2 = $stmt2->fetch_object()){
                        echo "<p>($obj2->Tot)</p>";
                    }
                }
                else
                echo "<p>(0)</p>";
                $stmt2->close();

            }
        }
        else {
            echo "<section class='error'><h1>There was a problem with the database.</h1></section>";
        }

    mysqli_free_result($stmt);

for ($i = $page - $x; $i < $page; $i++)
{
    if ($i >= 1) { 
        $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
    }
}

$pagination .= " " . $i . " ";

for ($i = $page + 1; $i <= $page + $x; $i++)
{
    if ($i <= $totalpages) {
        $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
    }

}

/*for ($i = 1; $i <= $totalpages; $i++) 
{
    if ($i != $page){
        $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
    }
    else{
        $pagination .= " " . $i . " ";
    }
}*/

mysqli_close($link);

echo $pagination;

echo "</section>";
?>

我唯一得不到的是“下一个”或“上一个”按钮。如果有人愿意添加这些按钮,请试一试。其他明智的.....享受..... :)这是我回馈stackoverflow社区帮助我的方式。