2012-05-17 34 views
0

你會如何分頁? 數據由下面的「getInstance」查詢調用,然後在動態表中列出一些名爲「pages」whis的記錄。我如何分頁這些數據?

我想能夠顯示每個查詢只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> 

的查詢REFFERENCE該呼叫中的用戶ID,以調用從corellating頁面上,這2個公共職能DB:

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

任何人都可以看到解決方案嗎?謝謝!

+2

此處的關鍵字是您需要添加的查詢中的「LIMIT」。谷歌上的教程太多了。嘗試一個。如果您遇到困難,請告訴我們您卡在哪裏。什麼不工作。 etc ... – itachi

回答

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; 

我的建議是使用virualization庫數據庫,例如通信。 AdoDB。有方法:

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

它也可以做輸入數據的過濾,歌廳所有結果作爲陣列和許多其他有用的東西。

+0

不幸的是,我無法將梨或其擴展程序加載到相關服務器上:-S – MrMage

+0

此擴展程序不使用任何其他PEAR類,因此您可以將其下載爲manualy並解壓縮到您的應用程序的源代碼。如果需要更改'require'中的路徑,並替換/刪除'Common.php'中的'PEAR :: raiseError' –

+0

謝謝我將會給它一個鏡頭! – MrMage