从MySQL数据库分页记录

时间:2012-05-23 08:39:01

标签: php javascript mysql

我有一个脚本可以计算它在页面上看到的记录数量:

   <script>
document.write(document.getElementById('moon').rows.length-1);
document.write ('&nbsp;');
document.write (' of ');
document.write ('&nbsp;');
document.write(document.getElementById('moon').rows.length-1);
document.write (' pages');

 </script>

我如何使用它一次分页10条记录? 也许使用诸如display:none之类的样式,然后在“页面”计数上链接到这些隐藏的div。

由于

修改

我曾尝试使用PHP Mysql Query对num_rows执行此操作,但即使在获取数据时也总是抛出错误!

第二次编辑

好的,我正在提取信息并显示:

<table class="std" id="moon" border="0" cellpadding="0" cellspacing="0">
        <tr>
        <?php

        $pcounter = 1;
        $userID = LedDB::getInstance()->get_user_id_by_name($_SESSION['user']);
        $presult = LedDB::getInstance()->get_page_by_campaign_id($campaignID);
        $i=0;
        while ($row = mysqli_fetch_array($presult)):
             $style = "";
                if($i%2==0)
{
    $style = 'style="background-color: #EFEFEF"';
}
echo "<tr ".$style.">";     
            echo "<td class='camp' style='padding-left:10px;'><b><a href='editPage.php?pageID=" .htmlentities($row['pid']) ."&campaignID=" .htmlentities($row['campaignid']) ."' class='camp'> Page" . $pcounter . "</a></b></td>";
            echo "<td style='padding-left:10px;'></td>";
            echo "<td style='padding-left:10px;'></td>";
            echo "<td></td>";
            echo "<td></td>";
            $pageID = $row['pid'];
            //The loop is left open
            ?>
            <td>
             <div class="buttons">
                <form name="editPage" action="editPage.php" method="GET"> 
                    <input type="hidden" name="campaignID" value="<?php echo $campaignID ?>"/>
                    <input type="hidden" name="pageID" value="<?php echo $pageID ?>"/>
                    <button type="submit" name="editPage" value="Edit" class="blue" >
                   <img src="images/edit.png" width="20" height="20"></button>

                </form>
                 </div>
            </td>
            <td>
                <form name="deletePage" action="deletePage.php" method="POST">
                    <input type="hidden" name="pageID" value="<?php echo $pageID; ?>"/>
                    <div class="buttons">
                    <button type="submit" name="deletePage" value="Delete" class="negative">
                    <img src="images/delete_x.png" width="20" height="20"></button>
                    </div>
                </form>
            </td>
            <?php
            echo "</tr>\n";
        $pcounter++;
        $i++;
        endwhile;
        mysqli_free_result($presult);
        ?>
    </table>

$presult = LedDB::getInstance()->get_page_by_campaign_id($campaignID);是这样的:

public function get_page_by_campaign_id($campaignID) {
     $campaignID = $this->real_escape_string($campaignID);
    return $this->query("SELECT pid,campaignid,message_text,message FROM pages WHERE campaignid =" . $campaignID );
}

每当我尝试将它包装在一个分页类中时,它会返回一个0值,或者只是用sql_num_rows()错误轰炸我的整个脚本......

这就是我认为javascript分页在这种情况下是最佳解决方案的原因。

1 个答案:

答案 0 :(得分:1)

网上有几个例子可以帮助您入门,这就是我要开始的地方:

http://www.phpclasses.org/search.html?words=pagination&x=0&y=0&go_search=1 http://www.phpsnaps.com/snaps/view/php-pagination-class/ http://www.phpbuilder.com/board/showthread.php?t=10283679 http://phpsense.com/2007/php-pagination-script/