第二页上的php分页无法正常工作

时间:2016-12-09 13:49:50

标签: php mysqli

            $dwdb=mysqli_connect("localhost","root","","dw");
            //this is my page number
            $page=(isset($_GET['page']) && $_GET['page']>0)?$_GET['page']:1 ;
            //this is my cat_id
            $new=(isset($_GET['year']) && $_GET['year']>0)?$_GET['year']:1 ;

        $perpage=2;
        $limit=($page > 1)?($page*$perpage)-$perpage:0;

        $query=mysqli_query($dwdb,"select *from movies where y_id='$new' limit {$limit},{$perpage}");
        while($result=mysqli_fetch_array($query)){
                    $id=$result['m_id'];
                    $name=$result['title'];
                    $img=$result['image'];


    echo"<div><a href='downloadpage.php?yc=$id'>$id.....$name<br><img src='i/image/$img' style='height:200px;width:200px;'/></a></div>";

            }
$query1=mysqli_query($dwdb,"select *from movies where y_id='$new'");
$total=mysqli_num_rows($query1);
$pages=ceil($total/$perpage);

echo "<a href='index1.php?page=1'>".'First Page'."</a>";
for ($i=1; $i<=$pages;$i++){

    echo "<a href='index1.php?page=".$i."'>".$i."</a> ";

    };
  echo        "<a href='index1.php?page=$pages'>Last page</a>";

这是我的代码。问题是,在我的第二页上,我有第一个$ new变量的结果.................................. .................................................. .................................................. .................................

2 个答案:

答案 0 :(得分:1)

  

...我有一个问题,在我的第二页上我有第一个$ new变量的结果

那是因为您没有在分页链接中包含$new变量。因此,每次进入第2,第3,第4 ......页面时,您将获得与$new相同的1值,这是因为此声明,

$new=(isset($_GET['year']) && $_GET['year']>0)?$_GET['year']:1 ;

在分页链接中包含此变量,以便您可以在后续页面中获取它的值。所以你的分页链接部分就像这样:

// your code

echo "<a href='index1.php?page=1&year=".$new."'>".'First Page'."</a>";
for ($i=1; $i<=$pages;$i++){
    echo "<a href='index1.php?page=".$i."&year=".$new."'>".$i."</a> ";
}
echo "<a href='index1.php?page=".$pages."&year=".$new."'>Last page</a>";

答案 1 :(得分:0)

用于分页在文件中使用此代码,名称为:view-paginated.php

$per_page = 10;
$result = mysql_query("SELECT * FROM table");

$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);

if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];

if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}

else
{
$start = 0;
$end = $per_page;
}

}

else
{
$start = 0;
$end = $per_page;
}

echo "<p><a href='view.php'>show all</a> | <b>page:</b> ";

for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='view-paginated.php?page=$i'>$i</a> ";
}

for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results) { break; }
echo  mysql_result($result, $i, 'YOUR COLUMN') ;
}