分页下一个和上一个按钮

时间:2017-09-05 07:44:51

标签: php

你好,伙计们能帮帮我吗? 我的分页有问题 我只想禁用下一个和上一个 按钮在我的分页中,如果是的话 没有项目     

                $db_host = "localhost";
                $db_username = "root"; 
                $db_pass = ""; 
                $db_name = "final";

                $con= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); 

                mysqli_select_db($con, "final") or die ("no database"); 

                  $pagination_sql = "SELECT * FROM `ongoing` WHERE approved='approve'";
                  $run_pagination = mysqli_query($con, $pagination_sql);

                  $count = mysqli_num_rows($run_pagination);

                  $total_pages = ceil($count/$per_page);

                    echo "<ul class='pagination'>";
                    echo "<li class='page-item'><a class='page-link' href='ongoing.php?page=".($page-1)."' class='button'>
                            <span aria-hidden='true'>&laquo;</span>
                            <span class='sr-only'>Previous</span>
                          </a></li>";

                  for($i=1;$i<=$total_pages;$i++){ 

                    echo'<li><a class="page-link" href="ongoing.php?page='.$i.'">'.$i.'</a></li>';

                  };

                    echo "<li class='page-item'><a class='page-link' href='ongoing.php?page=".($page+1)."' class='button'>
                            <span aria-hidden='true'>&raquo;</span>
                            <span class='sr-only'>Next</span>
                          </a></li>";
                    echo "</ul>"; 
            ?>

这是每页的条件

$per_page = 10;
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
  $page= 1;
}
$start_from = ($page-1) * $per_page;

2 个答案:

答案 0 :(得分:1)

我认为你只需要调整你的代码如下:

 echo "<ul class='pagination'>";

if($page == 1) {
    $disable_prev = 'disabled';
    $prev_url = "javascript:void(0);";
} else {
    $disable_prev = '';
    $prev_url = "ongoing.php?page=".($page-1);
}

echo "<li class='page-item ".$disable_prev."'><a class='page-link' href='".$prev_url."' class='button'>
        <span aria-hidden='true'>&laquo;</span>
        <span class='sr-only'>Previous</span>
      </a></li>";

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

if($page+1 == $total_pages) {
    $disable_next = '';
    $next_url = "ongoing.php?page=".($page+1);
} else {
    $disable_next = 'disabled';
    $next_url = "javascript:void(0);";
}

echo "<li class='page-item ".$disable_next."'><a class='page-link' href='".$next_url."' class='button'>
        <span aria-hidden='true'>&raquo;</span>
        <span class='sr-only'>Next</span>
      </a></li>";
echo "</ul>"; 

然后你还可以添加一些CSS来使禁用的li比其他

更少可见或褪色

答案 1 :(得分:0)

尝试:

if($i != NULL){ //NULL or Empty
    //Code here
}