【问题标题】:pagination and serial number php mysql分页和序列号php mysql
【发布时间】:2017-12-17 19:53:15
【问题描述】:

您好,我有以下带有序列号的代码。和分页。 分页正在工作,但是当我转到下一页时,序列号从 1 开始。我在每页中限制了 10 行,所以我想从 SN 开始下一页。 11-20, 21-30 从第 3 页开始。请帮帮我。

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

$start=0;
$limit=10;
$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>

【问题讨论】:

  • 请注意,没有 order by 的限制是毫无意义的

标签: javascript php mysql pagination


【解决方案1】:

只需在 if 条件后移动您的 $sn 初始化

$start=0;
$limit=10;

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

【讨论】:

    【解决方案2】:

    把你的代码改成这个...它会工作

    <?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>
    

    【讨论】:

    • 谢谢,真的很有帮助。
    【解决方案3】:

    在单击每个下一页按钮时,将$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--;
        }
    }
    

    这样你可以处理当前页面,限制和偏移。请记住在单击下一个和上一个按钮时调用 DB Query

    【讨论】:

    • 非常感谢
    • 标记答案正确,如果你已经解决了你的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2011-02-06
    • 2013-05-25
    • 2018-01-12
    相关资源
    最近更新 更多