我该如何对这些数据进行分页?

时间:2012-05-17 07:36:46

标签: php mysql pagination

你会如何分页? 数据由下面的“getInstance”查询调用,然后在动态表中列出了一些名为“pages”的记录。

我希望每个查询只能显示10个“页面”,然后将它们分页

     <table width="100%" border="0" cellpadding="0" cellspacing="0" class="std">

        <?php   


        $counter = 1;
        $userID = PageDB::getInstance()->get_user_id_by_name($_SESSION['user']);
        $result = PageDB::getInstance()->get_pages_by_campaign_id($campaignID);
        $i=0;
            while ($row = mysqli_fetch_array($result)):
             $style = "";
                if($i%2==0)
{
    $style = ' style="background-color: #EFEFEF"';
}
echo "<tr".$style.">";
            echo "<td>&nbsp;</td>";
            echo "<td></td>";


            //The loop is left open
            ?>
            <td>
            <form style="display:none;"></form>
            <div id="Pagination" class="pagination"> </div>
    <div id="Searchresult"> </div>
                <form name="editPage" action="editPage.php" method="GET">
                    <input type="hidden" name="pageID" value="<?php echo $pageID = $row['pid']; ?>" />
                    <input type="submit" name="editPage"  value="<?php echo "Page "  . $counter; ?>" style="background:none;border:none; cursor:pointer"/>
                </form>

            </td>
            <td>
                <form name="deletePage" action="deletePage.php" method="POST">
                    <input type="hidden" name="pageID" value="<?php echo $pageID = $row['pid']?>"/>
                    <input type="submit" name="deletePage" value="Delete" onclick = "javascript: return confirm('Delete Page <?php echo $counter ?> ?');"/>
                </form>
            </td>              
            <?php
            $pageID = $row['pid'];  
            $counter++;
            $i++;

            echo "</tr>\n";
        endwhile;
        mysqli_free_result($result);
        ?>
    </table>

查询引用这两个调用用户id的公共函数,以便从DB调用corellating页面:

 public function get_user_id_by_name($name) {
    $name = $this->real_escape_string($name);
    $user = $this->query("SELECT id FROM users WHERE name = '" . $name . "'");

    if ($user->num_rows > 0){
        $row = $user->fetch_row();
        return $row[0];
    } else
        return null;
}

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

有人能看到解决方案吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您可以在MySQL中使用LIMIT查询。

SELECT pid , campaignid 
FROM pages , campaigns 
WHERE campaignid = campaigns.id
LIMIT first_record_number, how_many_records

PEAR Pager可以帮助您获取当前页码等。

 $pager_options = array(
    'mode'       => 'Sliding',
    'perPage'    => 10,
    'delta'      => 2,
    'totalItems' => $total_count,
 );

 $pager = Pager::factory($pager_options);

 list($from, $to) = $pager->getOffsetByPageId();
 //TODO: get and display data from database
 //first_record_number = $from-1
 //how_many_records = 10

 //show the links
 echo $pager->links;

我的建议是使用虚拟化库与数据库进行通信,例如。 AdoDB。有方法:

$rs = $conn->SelectLimit("SELECT pid , campaignid 
FROM pages , campaigns 
WHERE campaignid = campaigns.id", how_many_records,first_record_number);
$arr1 = $rs->GetArray();

它还可以对输入数据进行过滤,将所有结果作为数组和许多其他有用的东西进行处理。