分页下拉

时间:2017-12-27 04:24:32

标签: php

我想在分页中添加下拉菜单,这样如果有超过10个页面,它将存储在下拉列表中,以便任何人都可以从中选择任何页面。

这是我的PHP代码,显示此输出: 1 2 3 4 5 6 7 8 9 10 11 ........... 100

$sql = "SELECT * FROM `new_data`"; 
$rs_result = mysqli_query($con,$sql); 
$total_records = mysqli_num_rows($rs_result);  //count number of records
$total_pages = ceil($total_records / $num_rec_per_page); 

echo "<a href='View.php?page=1'>".'|<'."</a> "; // Goto 1st page  

for ($i=1; $i<=$total_pages; $i++) { 
        echo "&nbsp;<a href='View.php?page=".$i."'>".$i."</a> "; 
}; 
echo "&nbsp;<a href='View.php?page=$total_pages'>".'>|'."</a> ";//Goto last page

我想要的输出: 1 2 3 4 5 6 7 8 9 10下拉列表(包含所有页面11到100)

2 个答案:

答案 0 :(得分:0)

$sql = "SELECT * FROM `new_data`"; 
$rs_result = mysqli_query($con,$sql); 
$total_records = mysqli_num_rows($rs_result);  //count number of records
$total_pages = ceil($total_records / $num_rec_per_page); 

echo "<a href='View.php?page=1'>".'|<'."</a> "; // Goto 1st page  
$array = [];
for ($i=1; $i<=$total_pages; $i++) { 
        $array[$i] = "<a href='View.php?page=".$i."'>".$i."</a>"; 
}; 
echo "&nbsp;<a href='View.php?page=$total_pages'>".'>|'."</a> ";//Goto last page

在数组中添加所有页面信息,然后用数组填充下拉列表我只更改了for循环代码...你只需使用$ array弹出你的下拉列表;

答案 1 :(得分:0)

我使用你的PHP代码用php完成了它。这是我的代码:

$sql = "SELECT * FROM `new_data`"; 
$rs_result = mysqli_query($con,$sql); //run the query
$total_records = mysqli_num_rows($rs_result);  //count number of records
$total_pages = ceil($total_records / $num_rec_per_page); 

echo "<a  href='View.php?page=1'>".'FIRST <<'."</a> "; // Goto 1st page  

for ($i=1; $i<=$total_pages; $i++) 
  { 
    if($total_pages<=10)
       {
         echo "&nbsp;<a style='color:#333;' href='View.php?page=".$i."'>".$i."</a> "; 
       }
else
{
    for ($i=1; $i<=10; $i++) 
     { 
        echo "&nbsp;<a style='color:#333;' href="View.php?page=".$i."'>".$i."</a> "; 
     };
    echo "<select class='mySelectBox' onchange='location = this.options[this.selectedIndex].value;'>";
     for ($i=11; $i<=$total_pages; $i++) 
     { 
            echo "<option value= View.php?page=".$i.">".$i."</option>";
     };
      echo "</select>";
}
}; 
echo "&nbsp;<a href='View.php?page=$total_pages'>".'........>> LAST'."</a> "; // Goto last page