简体   繁体   中英

php pagination, something wrong

Some code from me.

$files = glob("pardod/*.html");
$record_count  = 5;
$total_pages   = ceil(count($files)/$record_count);
$page          = $_GET['page'];
$offset        = ($page-1)*$record_count;
$files_filter  = array_slice($files, $offset,$record_count);

for ($i = 0; $i<$filecount; $i++){
    if ($page){
        $start = ($page - 1) * $record_count;
    }else{
        $start = 0;
    }
}

if($total_pages > 1){
   if($page != 1){
      echo '<a href="pardod.php?page='.($page-1).'">Atpakal</a>';
   }
   if($page != $total_pages){
      echo '<a href="pardod.php?page='.($page+1).'">Uz priekšu</a>';
   }
}

The php pagination dont work, i am just learning how to make, where is a problem?

The *.html files didn't shows :(

Try this code for pagination

  <?php

  $con=mysql_connect("localhost","root","");

  $page=$_REQUEST['page'];

  if ($page < 1)
  {
  $page = 1;
  }
  $resultsPerPage =15;
  $startResults = ($page - 1) * $resultsPerPage;
  $numberOfRows = mysql_num_rows(mysql_query('SELECT * FROM tablename'));
  $totalPages = ceil($numberOfRows / $resultsPerPage);
  echo"<center><table border='1' bordercolor='blue' height='90%' width='90%'> <tr><th     bgcolor='silver'>Name</th><th bgcolor='silver'>Password</th><th      bgcolor='silver'>Question</th><th bgcolor='silver'> Answer</th><th        bgcolor='silver'>Image</th> </tr>";

$i=1;
                                                                                  $result= mysql_query("SELECT * FROM password LIMIT   $startResults,        $resultsPerPage");

 while($row=mysql_fetch_array($result))
       {

   }

   echo"</tr></table></center>";

      echo '<center><a href="?page=1">First</a>&nbsp';

  if($page > 1)
  echo '<a href="?page='.($page - 1).'">Back</a>&nbsp';

  for($i = 1; $i <= $totalPages; $i++)
      {
if($i == $page)
  echo '<strong>'.$i.'</strong>&nbsp';
 else
   echo '<a href="?page='.$i.'">'.$i.'</a>&nbsp';
     }

  if ($page < $totalPages)
  echo '<a href="?page='.($page + 1).'">Next</a>&nbsp;';

   echo '<a href="?page='.$totalPages.'">Last</a></center>';
 ?>

Try This Code

$limit = ( isset($_GET['limit'])) ? $_GET['limit'] : 5;
if (strtolower($limit) == 'all') {
    $limit = 'all';
} else {
    $limit = filter_var($limit, FILTER_SANITIZE_NUMBER_INT);
    if (trim($limit) == '') {
        $limit = 5;
    }
}

$page = ( isset($_GET['page'])) ? $_GET['page'] : 1;
$page = filter_var($page, FILTER_SANITIZE_NUMBER_INT);

$links = ( isset($_GET['links'])) ? $_GET['links'] : 1;
$links = filter_var($links, FILTER_SANITIZE_NUMBER_INT);

Here is the link where step by step described all the process in details.

Simple Pagination in PHP By Learning Ocean Team

if you are using bootstrap, you can use the following

function pagination($page, $count) {
        global $options;
        $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
        $actual_link = trim(str_replace("page=".$page, "", $actual_link), "&");

        $lenght = ceil($count/$options->get("result_per_page"));
        if ($page <= 3) {
            if ($lenght < 5) {
                $first = 0;
                $last = $lenght-1;
            } else {
                $first = 0;
                $last = 4;
            }
        } else if ($page < ($lenght-2)) {
            $first = $page-2;
            $last = $page+2;
        } else if ($page <= $lenght) {
            $first = $page-5;
            $last = $lenght-1;
        }
        if ($page > 0) {
            $prev = $page-1;
            $next = $page+1;
        } else {
            $prev = 0;
            $next = $page+1;
        }

        echo '<span>Viewing page '.($page+1).' of '.$lenght.'</span><br>';
        echo '<nav aria-label="Page navigation example">';
        echo '<ul class="pagination justify-content-center">';
        if ($prev > 0) {
            echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page=0">&laquo;&laquo;</a></li>';
            echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.$prev.'">&laquo;</a></li>';
        }
        for ($i = $first; $i<=$last; $i++) {
            if ($i == $page) {
                $active = ' active';
            } else {
                $active = '';
            }
            echo '<li class="page-item'.$active.'"><a class="page-link" href="'.$actual_link.'&page='.$i.'">'.($i+1).'</a></li>';
        }
        if ($next < $last) {
            echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.$next.'">&raquo;</a></li>';
            echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.($lenght-1).'">&raquo;&raquo;</a></li>';
        }
        echo '</ul>';
        echo '</nav>';
    }

to call the function

$pageNumber = 1;
$totalRowCount = 5000;
pagination($pageNumber, $totalRowCount);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM