分页总是缺少1排

时间:2015-04-21 21:16:48

标签: php mysql pagination

我有一个分页代码,它从DB中获取数据并在Table中显示它们,但总是缺少数据库中添加的最后一行,我不知道为什么......

这是我的代码;

if(isset($_GET['page'])){
                    $page=$_GET['page'];
                    }else $page=1;

            $obj=new action_a_DB();
            $totall= $obj->getNmbLgForPages() ; // number of all articles.
            $pagination=new pagination($totall,10 /*number of content per page*/,$page,5 /*number of button to show*/);
            $start = $pagination->Start ; 
            $end = $pagination->End ;
            // get the info from the db 
            $total_page =   ceil($totall/10);
            $result = $obj->getAllArticle($start,$end) ;
                                }

pagination.php

...
public function Show_Pagination($link,$get='page',$div_class_name='pagination')
    { 
    if($this->Pages==1)return; 

    echo'<div class="'.$div_class_name.'">'; 
    if($this->Page_number>1)echo '<a  href="'.$link.'&'.$get.'='.($this->Page_number -1 ).'">Pre</a> ';

    else echo '<a >Pre</a> ';

    //print button

    $this->Buttons=(int)$this->Buttons;

    $start_counter  =   $this->Page_number-floor($this->Buttons/2);          

    $end_conter     =   $this->Page_number+floor($this->Buttons/2);  

    if($start_counter<1) $end_conter=$end_conter+abs($start_counter);       

    if($end_conter>$this->Pages) $start_counter=$start_counter-($end_conter-$this->Pages);

    if(($this->Page_number-floor($this->Buttons/2))<1)$end_conter ++; 

    for ($i=$start_counter;$i<=$end_conter;$i++)
            {

            if($i>$this->Pages || $i<1)continue;        //no print less than 1 value or grater than totall page

            if($i==$this->Page_number)echo ' <a  class="cur">'.$i.'</a> ';       

            else echo ' <a  href="'.$link.'&'.$get.'='.$i.'">'.$i.'</a> ';       
            } 
    if($this->Page_number<$this->Pages)echo '<a  href="'.$link.'&'.$get.'='. ($this->Page_number +1 ) .'">Suiv</a> ';

    else echo '<a >Suiv</a> ';       

    echo'</div>';

    }
...

我在action_a_DB.php中的功能

function getAllArticle($Start, $End){

    $sql = "SELECT * FROM articles order by id desc limit $Start , $End";
    $req = mysql_query($sql) or die('Req Invalide: ' . mysql_error());
        //return mysql_fetch_array($req);
        return $req;
  }

  function getNmbLgForPages(){
    $query=mysql_query("SELECT count(id) from articles") or die('Erreur :'.mysql_error()); //
    $totall=mysql_result($query,0);
    return $totall;
  }

我的表

                    </tr>
                    <?php if(mysql_fetch_row($result)!=0){ 
                    while($data = mysql_fetch_array($result)) {?>
                    <tbody>
                    <tr>

                        <td class="first style1"><?php echo $data['titre']; ?> </td>
                        <td><?php echo $data['descrition']; ?></td>
                        <td><img src="img/edit-icon.gif" width="16" height="16" alt="" /></td>
                        <td><img src="img/hr.gif" width="16" height="16" alt="" /></td>
                        <td><img src="img/save-icon.gif" width="16" height="16" alt="save" /></td>

                    </tr></tbody> <?php } 

                                        ?>

            <tfoot><tr id="nav"><td colspan="5"><div> <?php
            $pagination->Show_Pagination("index.php?param1=value1",'page','pagination');

             ?></div></td>
            </tr>
            <tr id="total"><td colspan="5"><?php    echo 'Page : '.$page.'   sur     '.$total_page.'  Nombre totales d\'articles: '.$totall.'' ; ?></td>
            </tr>
                <?php } else{ ?>
            <tr><td align="center" colspan="5">Rien a afficher</td>
            </tr></tfoot>
                <?php } ?>

                </table>

我该如何解决这个问题?

非常感谢,

1 个答案:

答案 0 :(得分:1)

我的错误就在这里

 <?php if(mysql_fetch_row($result)!=0){ 

我必须使用 mysql_num_rows 而不是 mysql_fetch_row

所以我用

<?php if(mysql_num_rows($result)!=0){ 

为什么,当我的查询执行时,它返回一个带有指向第一条记录的指针的结果集。然后每个fetch返回一行并将指针移动到下一行,如果结果集中没有下一行则返回false,然后处理所有结果。在我的情况下,我的资源的第一行使用,如果是这样,它在显示中失败。

现在工作正常,感谢 Bovino 的解释和所有人