2017-07-13 64 views
0

你好,我有以下代碼序列號。和分頁。 分頁正在工作,但當我轉到下一頁時,序列號從1開始。我在每頁中限制了10行,所以我想從SN開始下一頁。從第3頁開始11-20,21-30。請幫幫我。分頁和序列號php mysql

<?php include_once("../database/config.php"); 
 

 
$start=0; 
 
$limit=10; 
 
$sn=$start; 
 

 
if(isset($_GET['mainmdsid'])) 
 
{ 
 
\t $mainmdsid=$_GET['mainmdsid']; 
 
\t $start=($mainmdsid-1)*$limit; 
 
} 
 
else{ 
 
\t $mainmdsid=1; 
 
} 
 
\t $result = mysqli_query($mysqli, "SELECT *from item , mainmds , school WHERE empid=$empid AND mainmds.itmid=item.itmid AND mainmds.scid=school.scid LIMIT $start, $limit"); \t  
 
    
 
\t  
 
?> 
 
\t \t \t <table align="center" id="customers"> 
 

 
\t <tr> 
 
\t \t <th>SN.</th> 
 
\t \t <th>Item Name</th> 
 
\t \t <th>Quantity</th> 
 
\t \t <th>Status</th> 
 
\t \t <th>If unsatisfactory provide comments </th> 
 
\t \t <th>Date</th> 
 
\t \t <th>School Name</th> 
 
\t \t <th>Variant</th> 
 
\t \t <th>Thana</th> 
 
\t \t 
 
\t \t <th>If others then please specify </th> 
 
\t \t <th>Remove</th> \t \t \t 
 
\t \t 
 
\t </tr> 
 
\t 
 
\t <?php 
 
\t //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array 
 
\t while($res = mysqli_fetch_array($result)) { 
 
\t $sn+=1; 
 
\t 
 
\t $sdate = $res['sdate']; 
 
\t $fmdate = strtotime($sdate); 
 
\t 
 
\t \t \t 
 
\t \t echo "<tr>"; 
 
\t \t echo "<td>".$sn."</td>"; 
 
\t \t echo "<td>".$res['itmname']."</td>"; 
 
\t \t 
 
\t \t \t if (strpos($empbranch,"NFO")!==false) { 
 
\t \t echo "<td>".$res['DNFO']."</td>"; \t 
 
\t \t } 
 
\t \t else if (strpos($empbranch,"DFO")!==false) { 
 
\t \t echo "<td>".$res['DDFO']."</td>"; \t 
 
\t \t } 
 
\t \t else if (strpos($empbranch,"CO")!==false) { 
 
\t \t echo "<td>".$res['DCO']."</td>"; \t 
 
\t \t } 
 
\t \t else if (strpos($empbranch,"SHARP")!==false) { 
 
\t \t echo "<td>".$res['DSHARP']."</td>"; \t 
 
\t \t } 
 
\t \t else if (strpos($empbranch,"Other")!==false) { 
 
\t \t echo "<td>".$res['DOther']."</td>"; \t 
 
\t \t } 
 
\t \t echo "<td>".$res['istatus']."</td>"; 
 
\t \t echo "<td>".$res['usstatus']."</td>"; 
 
\t \t 
 
\t \t echo "<td>".$res['sdate']."</td>"; 
 
\t \t echo "<td>" .date('d M Y', $fmdate). "</td>"; 
 
\t \t echo "<td>".$res['ivariant']."</td>"; \t 
 
\t \t echo "<td>".$res['scthana']."</td>"; 
 
\t \t echo "<td>".$res['oscname']."</td>"; 
 
\t \t 
 
\t \t \t 
 
\t \t 
 
\t \t \t 
 
\t \t 
 
\t \t echo "<td><a href=\"delete.php?mainmdsid=$res[mainmdsid]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td></tr>"; \t \t 
 
\t } 
 
\t ?> 
 
\t </table> 
 
\t 
 
\t <?php 
 
//fetch all the data from database. 
 
$rows=mysqli_num_rows(mysqli_query($dbconfig,"SELECT *from item , mainmds , school WHERE empid=$empid AND mainmds.itmid=item.itmid AND mainmds.scid=school.scid")); 
 
//calculate total page number for the given table in the database 
 
$total=ceil($rows/$limit);?> 
 
<table align="center" border="1"> 
 
<?php if($mainmdsid>1) 
 
{ 
 
\t //Go to previous page to show previous 10 items. If its in page 1 then it is inactive 
 
\t echo "<tr><td><a href='?mainmdsid=".($mainmdsid-1)."' class='button'>PREVIOUS</a></td>"; 
 
} 
 
if($mainmdsid!=$total) 
 
{ 
 
\t ////Go to previous page to show next 10 items. 
 
\t echo "<td><a href='?mainmdsid=".($mainmdsid+1)."' class='button'>NEXT</a></td>"; 
 
} 
 
?> 
 

 
<?php 
 
//show all the page link with page number. When click on these numbers go to particular page. 
 
\t \t for($i=1;$i<=$total;$i++) 
 
\t \t { 
 
\t \t \t if($i==$mainmdsid) { echo "<td>".$i."</td>"; } 
 
\t \t \t 
 
\t \t \t else { echo "<td><a href='?mainmdsid=".$i."'>".$i."</a></td>"; } 
 
\t \t } 
 
?> 
 
</tr> 
 
</table>

+0

請注意,沒有訂單的限制是相當無意義的 – Strawberry

回答

1

只動$sn初始化如果條件

$start=0; 
$limit=10; 

if(isset($_GET['mainmdsid'])) 
{ 
    $mainmdsid=$_GET['mainmdsid']; 
    $start=($mainmdsid-1)*$limit; 
} 
else{ 
    $mainmdsid=1; 
} 
$sn=$start; 
+0

謝謝你讓我清楚。 –

+0

pl批准答案,如果我的解決方案適合你 –

0

更改後的代碼這...這將工作

<?php include_once("../database/config.php"); 

    $start=0; 
    $limit=10; 

    if(isset($_GET['mainmdsid'])) 
    { 
     $mainmdsid=$_GET['mainmdsid']; 
     $start=($mainmdsid-1)*$limit; // here we are multiplying page number with limit. 
    } 

    $sn=$start; 

    if(isset($_GET['mainmdsid'])) 
    { 
     $mainmdsid=$_GET['mainmdsid']; 
     $start=($mainmdsid-1)*$limit; 
    } 
    else{ 
     $mainmdsid=1; 
    } 
      $result = mysqli_query($mysqli, "SELECT *from item , mainmds , school WHERE empid=$empid AND mainmds.itmid=item.itmid AND mainmds.scid=school.scid LIMIT $start, $limit");  


    ?> 
       <table align="center" id="customers"> 

     <tr> 
      <th>SN.</th> 
      <th>Item Name</th> 
      <th>Quantity</th> 
      <th>Status</th> 
      <th>If unsatisfactory provide comments </th> 
      <th>Date</th> 
      <th>School Name</th> 
      <th>Variant</th> 
      <th>Thana</th> 

      <th>If others then please specify </th> 
      <th>Remove</th>   

     </tr> 

     <?php 
     //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array 
     while($res = mysqli_fetch_array($result)) { 
     $sn+=1; 

     $sdate = $res['sdate']; 
     $fmdate = strtotime($sdate); 


      echo "<tr>"; 
      echo "<td>".$sn."</td>"; 
      echo "<td>".$res['itmname']."</td>"; 

       if (strpos($empbranch,"NFO")!==false) { 
      echo "<td>".$res['DNFO']."</td>"; 
      } 
      else if (strpos($empbranch,"DFO")!==false) { 
      echo "<td>".$res['DDFO']."</td>"; 
      } 
      else if (strpos($empbranch,"CO")!==false) { 
      echo "<td>".$res['DCO']."</td>";  
      } 
      else if (strpos($empbranch,"SHARP")!==false) { 
      echo "<td>".$res['DSHARP']."</td>"; 
      } 
      else if (strpos($empbranch,"Other")!==false) { 
      echo "<td>".$res['DOther']."</td>"; 
      } 
      echo "<td>".$res['istatus']."</td>"; 
      echo "<td>".$res['usstatus']."</td>"; 

      echo "<td>".$res['sdate']."</td>"; 
      echo "<td>" .date('d M Y', $fmdate). "</td>"; 
      echo "<td>".$res['ivariant']."</td>"; 
      echo "<td>".$res['scthana']."</td>"; 
      echo "<td>".$res['oscname']."</td>"; 





      echo "<td><a href=\"delete.php?mainmdsid=$res[mainmdsid]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td></tr>";   
     } 
     ?> 
     </table> 

     <?php 
    //fetch all the data from database. 
    $rows=mysqli_num_rows(mysqli_query($dbconfig,"SELECT *from item , mainmds , school WHERE empid=$empid AND mainmds.itmid=item.itmid AND mainmds.scid=school.scid")); 
    //calculate total page number for the given table in the database 
    $total=ceil($rows/$limit);?> 
    <table align="center" border="1"> 
    <?php if($mainmdsid>1) 
    { 
     //Go to previous page to show previous 10 items. If its in page 1 then it is inactive 
     echo "<tr><td><a href='?mainmdsid=".($mainmdsid-1)."' class='button'>PREVIOUS</a></td>"; 
    } 
    if($mainmdsid!=$total) 
    { 
     ////Go to previous page to show next 10 items. 
     echo "<td><a href='?mainmdsid=".($mainmdsid+1)."' class='button'>NEXT</a></td>"; 
    } 
    ?> 

    <?php 
    //show all the page link with page number. When click on these numbers go to particular page. 
      for($i=1;$i<=$total;$i++) 
      { 
       if($i==$mainmdsid) { echo "<td>".$i."</td>"; } 

       else { echo "<td><a href='?mainmdsid=".$i."'>".$i."</a></td>"; } 
      } 
    ?> 
    </tr> 
    </table> 
+0

謝謝,真的非常有幫助。 –

0

在點擊每關下一頁按鈕,增加限制$start從0到+10。 並再次調用您的數據庫查詢,並且將前一個按鈕遞減值$start設置爲-10。 並且還使用變量currentPage並在開始時將其值設置爲1。

下面是示例代碼,您可以使用:

$currentPage = 1; 
$start = 0; 
$recordPerPage= 10; 

//First query your database here, after that do some work on previous and next button clicked 

if(array_key_exists('next',$_POST)){ 
    $start = (currentPage * recordPerPage) + 1; 
    currentPage++; 
} 
if(array_key_exists('previous',$_POST)){ 
    if (currentPage == 2) { 
     offSet = 0; 
     currentPage--; 
    } else { 
     offSet = offSet - recordPerPage; 
     currentPage--; 
    } 
} 

這種方式可以處理當前頁面,限制和抵消。記得在點擊下一個和上一個按鈕時調用數據庫查詢

+0

非常感謝 –

+0

標記答案是正確的,如果你已經解決了你的問題 –