2015-04-19 68 views
0

我一直试图让分页工作,我必须在一定程度上,但它只显示一页结果。分页。为什么只有一个页面显示?

我在我的数据库中有18个房源,并设置为每页显示6个。

任何人都可以看到什么问题是?

<?php include('includes/configsql.php'); 

//include header template 
include('layout/header.php'); 

//include navbar template 
include('layout/navbar.php'); 


?> 

<?php 
$con = mysqli_connect($db_hostname,$db_username,$db_password,$db_database); 

$sql = "SELECT COUNT(id) FROM basic WHERE status='active'"; 
$query = mysqli_query($con, $sql); 
$row = mysqli_fetch_row($query); 

$rows = $row[0]; 
$rows = mysqli_num_rows($query); 

$page_rows = 6; 
$last = ceil($rows/$page_rows); 

if($last < 1){ 
    $last = 1; 
} 

$pagenum = 1; 

if(isset($_GET['pn'])){ 
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']); 
} 

if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
} 

$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows; 
$sql = "SELECT id, name, address, telephone, email, category FROM basic WHERE status='active' ORDER BY name ASC $limit"; 
$query = mysqli_query($con, $sql); 

$textline1 = "Basic Listing (<b>$rows</b>)"; 
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>"; 

$paginationCtrls = ''; 
if($last != 1){ 
    if ($pagenum > 1) { 
     $previous = $pagenum - 1; 
     $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; '; 
     for($i = $pagenum-4; $i < $pagenum; $i++){ 
      if($i > 0){ 
       $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; '; 
      } 
     } 
    } 

    $paginationCtrls .= ''.$pagenum.' &nbsp; '; 
    for($i = $pagenum+1; $i <= $last; $i++){ 
     $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; '; 
     if($i >= $pagenum+4){ 
      break; 
     } 
    } 
    if ($pagenum != $last) { 
     $next = $pagenum + 1; 
     $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> '; 
    } 
} 
$list = ''; 
while($row = mysqli_fetch_array($query)){ 
    $id = $row["id"]; 
    $name = $row["name"]; 
    $category = $row["category"]; 
    $list .= '<p><a href="business.php?id='.$id.'">'.$name.' &nbsp;|&nbsp;'.$category.' </a> - Click the link to view this business</p>'; 
} 

mysqli_close($con); 
?> 


<div id="wrapper"> 


<div id="bizlist"> 

<div id="pagination"> 
    <h2><?php echo $textline1; ?></h2> 
    <p><?php echo $textline2; ?></p> 
    <p><?php echo $list; ?></p> 
    <div id="pagination_controls"><?php echo $paginationCtrls; ?></div> 
</div> 

</div> 



<?php 
//include footer template 
include('layout/footer.php'); 
?> 

回答

0

我认为你必须删除行:

$rows = mysqli_num_rows($query); 

它在脚本的开始。

另一种方式是,如果你修改的第一行是这样的:

$sql = "SELECT * FROM basic WHERE status='active'"; 

,并删除接下来的3行

$query = mysqli_query($con, $sql); 
$row = mysqli_fetch_row($query); 
$rows = $row[0]; 

你必须选择一个以上,但不能同时:)

+0

非常感谢您....删除以下行之一的诀窍: $ rows = mysqli_num_rows($ query); 再次感谢....非常感谢。 – heartsfan

相关问题