0

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

我希望每个查询只能显示 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 的公共函数,以便从数据库调用相关页面:

 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");
}

谁能看到解决方案?谢谢!

4

1 回答 1

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;

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

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

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

于 2012-05-17T08:14:42.833 回答