【问题标题】:count row for table in pagination?在分页中计算表格的行数?
【发布时间】:2014-07-18 20:30:42
【问题描述】:

我对以下代码中的行数有疑问。这意味着,在表格中的每一页分页行中,从头开始计算。例如,明确的:

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

【问题讨论】:

  • 谁能解开我的疑惑
  • 跟踪 $pn。好像每次都一样。我怀疑你的 preg_replace 会给你预期的结果。现在它替换了满足以下条件的字符串/字符:# matches the character # literally - [^0-9] match a single character not present in the list below: 0-9 a single character in the range between 0 and 9 - #i matches the characters #i literally (case sensitive)
  • $i=0; if(isset($_GET['pn'])){ $i = (($_GET['pn']-1) * $itemsPerPage)+1;$i++
  • 我已经使用了这个,现在我的错误已修复

标签: php html wordpress


【解决方案1】:

而不是 echo $i;

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

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

【讨论】:

  • echo i++; 不在问题的代码中,ts 已经让他的代码工作了。
  • 对不起,而不是 echo $i;
猜你喜欢
  • 1970-01-01
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
相关资源
最近更新 更多