在分页中计算表格的行数?

时间:2014-07-11 07:02:51

标签: php html wordpress

我在下面的代码中遇到了行数问题。这意味着,在每个页面中的分页行中,从头开始计算。例如,明确:

P1:如果我们在分页的第一页上,则行数是:

1 Columns1
2 Columns2
3 Columns3
4 Columns4

P2:如果我们在分页的第一页上,则行是计数:

1 Columns1
2 Columns2
3 Columns3
4 Columns4

我需要继续行计数为1234 5678 我使用这个代码

$sql = mysql_query("select  *  from (two_make left join two_model on  two_make.make_id = two_model.make_id) left join two_variant on two_model.model_id = two_variant.model_id;");

$nr = mysql_num_rows($sql); 
if (isset($_GET['pn'])) { 
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']);  
} else { 
    $pn = 1;
} 
$itemsPerPage = 20; 
$lastPage = ceil($nr / $itemsPerPage);

if ($pn < 1) { 
    $pn = 1; 
} else if ($pn > $lastPage) { 
    $pn = $lastPage; 
} 

$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

$sql2 = mysql_query("select * from (two_make left join two_model on  two_make.make_id = two_model.make_id) left join two_variant on two_model.model_id = two_variant.model_id ORDER BY make ASC $limit"); 

$paginationDisplay = ""; 
if ($lastPage != "1"){

    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';

    if ($pn != 1) {
        $previous = $pn - 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
    } 
     $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
    } 
}

$outputList = ''; ?>
<form name="form1" method="POST" action="">
<table width="578" border="1" align="center" id="menu">
<h3 style="color:#68ADD4;"><u>Two Wheeler List</u></h3>
    <tr>
    <th style="color:#68ADD4;">Si.no</th>
    <th style="color:#68ADD4;">Make</th>
    <th style="color:#68ADD4;">Model</th>
    <th style="color:#68ADD4;">Variant</th>
    <th style="color:#68ADD4;">Select</th>
       </tr>

<?php
$i=0;
 while($row=mysql_fetch_array($sql2))
 {
 $i++;
?>
<tr>
    <td><?php echo $i ;?></td>
    <td><?php echo $row['make'];?></td>
    <td><?php echo $row['model'];?></td>
    <td><?php echo $row['variant'];?></td>
    <td><center><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['variant_id'];?>"></center></td>
</tr>

<?php  } ?> 

1 个答案:

答案 0 :(得分:1)

而不是echo $ i;

echo(($ itemsPerPage * $ pn)+ $ i);

其中$ itemsPerpage是每页的项目数,$ pn是您的页码