2014-11-21 69 views
1

有人可以告訴我這個分頁有什麼問題。我試圖在每10條記錄後進行分頁,但失敗。相反,我在同一時間看到所有記錄,而不是分頁。我在這裏錯了。面對分頁問題

查看用戶文件

<form action="tuser.php" method="GET"> 
<table> 
<tr> 
<td>Here You get the Table View of Data</td>  
</tr> 
<tr> 
<td><input type="submit" name="submit" value="Total Number of User"/></td> 
</tr> 
</form> 

編碼文件

<?php 
$con = mysqli_connect("localhost","root","","testsite"); 

if(!$con) 
{ 
    die("Could not connect: ".mysqli_error()); 
} 

/* if(!(isset($pagenum)){ 
$pagenum = 1; 
} 
*/ 
$view = $_GET['submit']; 
if(isset($_GET['submit'])) 
{ 

    $stbl = "SELECT * FROM `register`"; 
    $ssql = mysqli_query($con,$stbl);// OR die("Query Error: ".mysqli_error()); 
    $num = mysqli_num_rows($ssql); 

    echo $num; 
    $pagerow = 10; 

    $total = ceil($num/$pagerow); 

    /* $pagenum = $total; 
    /* if($pagenum < 1){ 
    $pagenum = 1; 
    } 
    else { 
    $pagenum = $total; 
    }*/ 

    $max  = 'limit ' .($pagenum - 1) * $pagerow .',' .$pagerow; 

    $ussql = mysqli_query($con, "SELECT * FROM `register` LIMIT '$max'"); 

    echo "<table border=5 table style=margin-top: 252px; font-size: larger; font-style: oblique; bgcolor=#05fa8c> 
    <tr> 
    <td width=10% height= 10%><b>User Name:</b></td> 
    <td width=10% height= 10%><b>First Name:</b></td> 
    <td width=10% height= 10%><b>Last Name:</b></td> 
    <td width=10% height= 10%><b>Password:</b></td> 
    <td width=10% height= 10%><b>Email:</b></td> 
    <td width=10% height= 10%><b>Role Type:</b></td> 
    <td width=10% height= 10%><b>About:</b></td> 
    <td width=10% height= 10%><b>Edit<b></td> 
    <td width=10% height= 10%><b>Delete</b></td> 
    </tr>"; 

    while($row = mysqli_fetch_array($ssql)) 
    { 
     $rid = $row['id']; 
     $uname = $row['username']; 
     $fname = $row['firstname']; 
     $lname = $row['lastname']; 
     $pwd = $row['password']; 
     $emal = $row['email']; 
     $rol = $row['role']; 
     $abt = $row['profession']; 

     echo "<tr> 
     <td width=10% height= 10%>$uname</td> 
     <td width=10% height= 10%>$fname</td> 
     <td width=10% height= 10%>$lname</td> 
     <td width=10% height= 10%>$pwd</td> 
     <td width=10% height= 10%>$emal</td> 
     <td width=10% height= 10%>$rol</td> 
     <td width=10% height= 10%>$abt</td> 
     <td width=10% height= 10%><a href=edit_records.php?id=$rid>Edit </a></td> 
     <td width=10% height= 10%><a href=delete.php?id=$rid>Delete</a></td> 
     </tr>"; 
    } 
    echo"</table>"; 

    echo " --Page $pagenum of $max-- <p>"; 

    if($pagenum == 1) 
    { 

    } 
    else 
    { 
     echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; 
     echo " "; 
     $previous = $pagenum - 1; 
     echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; 
    } 

    if($pagenum == $max) 
    { 

    } 
    else 
    { 
     $next = $pagenum + 1; 
     echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; 
     echo " "; 
     echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$max'>Last ->></a> "; 
    } 
} 
?> 

+0

錯了,因爲什麼呢?它會出錯? – 2014-11-21 09:59:59

+0

沒有任何錯誤,但所有記錄都顯示在一個頁面中。有些時候我面臨未定義的索引:提交錯誤 – 2014-11-21 10:03:30

回答

1

試試這個簡單的..........分頁

 <html> 
     <head> 
     <style> 
     #content 
     { 
     width: 900px; 
     margin: 0 auto; 
     font-family:Arial, Helvetica, sans-serif; 
     } 
     .page 
     { 
     float: right; 
     margin: 0; 
     padding: 0; 
     } 
     .page li 
     { 
     list-style: none; 
     display:inline-block; 
     } 
     .page li a, .current 
     { 
     display: block; 
     padding: 5px; 
     text-decoration: none; 
     color: #8A8A8A; 
     } 
     .current 
     { 
     font-weight:bold; 
     color: #000; 
     } 
     .button 
     { 
     padding: 5px 15px; 
     text-decoration: none; 
     background: #333; 
     color: #F3F3F3; 
     font-size: 13PX; 
     border-radius: 2PX; 
     margin: 0 4PX; 
     display: block; 
     float: left; 
     } 
     </style> 
     </head> 
     <body> 
     <div id="content"> 
     <?php 
     $query1=mysql_connect("localhost","root",""); 
     mysql_select_db("contractor_leads",$query1); 

     $start=0; 
     $limit=5; 

     if(isset($_GET['id'])) 
     { 
     $id=$_GET['id']; 
     $start=($id-1)*$limit; 
     } 
else 
{ 
$id=1; 
} 

     $query=mysql_query("SELECT * FROM category2 LIMIT $start, $limit"); 
     ?> 
     <table border="1"> 
     <tr> 
     <th>Name</th> 
                <th>Device Id</th> 
                <th>Puppy Count</th> 

     </tr> 
     <?php 
     while($query2=mysql_fetch_array($query)) 
     { 
     echo "<tr><td>".$query2['parent_child_name']."</td> 
     <td>".$query2['parent_child_name']."</td> 
     <td>".$query2['parent_child_name']."</td> 
     </tr>"; 
     } 
     echo "</table>"; 
     $rows=mysql_num_rows(mysql_query("SELECT * FROM category2")); 
     $total=ceil($rows/$limit); 

     if($id>1) 
     { 
     echo "<a href='?id=".($id-1)."' class='button'>PREVIOUS</a>"; 
     } 
     if($id!=$total) 
     { 
     echo "<a href='?id=".($id+1)."' class='button'>NEXT</a>"; 
     } 

     echo "<ul class='page'>"; 
     for($i=1;$i<=$total;$i++) 
     { 
     if($i==$id) { echo "<li class='current'>".$i."</li>"; } 

     else { echo "<li><a href='?id=".$i."'>".$i."</a></li>"; } 
     } 
     echo "</ul>"; 

     ?> 
     </div> 
     </body> 
     </html> 
+0

未定義的ID錯誤,但我appy所有變量和其他正確。 – 2014-11-21 12:09:54

+0

我已更新我的代碼..現在它會工作。 – 2014-11-21 12:17:21