2010-06-06 34 views
1

這是我的分頁腳本:外鍵和分頁

<?php 
/*********************************** 
* PhpMyCoder Paginator   * 
* Created By PhpMyCoder   * 
* 2010 PhpMyCoder     * 
* ------------------------------- * 
* You may use this code as long * 
* as this notice stays intact and * 
* the proper credit is given to * 
* the author.      * 
***********************************/ 
?> 
<head> 
    <title> Pagination Test - Created By PhpMyCoder</title> 
     <style type="text/css"> 
    #nav { font: normal 13px/14px Arial, Helvetica, sans-serif; margin: 2px 0; } 
    #nav a { background: #EEE; border: 1px solid #DDD; color: #000080; padding: 1px 7px; text-decoration: none; } 
    #nav strong { background: #000080; border: 1px solid #DDD; color: #FFF; font-weight: normal; padding: 1px 7px; } 
    #nav span { background: #FFF; border: 1px solid #DDD; color: #999; padding: 1px 7px; } 
    </style> 
    </head> 
    <?php 
    //Require the file that contains the required classes 
    include("pmcPagination.php"); 

    //PhpMyCoder Paginator 
    $paginator = new pmcPagination(20, "page"); 

    //Connect to the database 
    mysql_connect("localhost","root","PASSWORD"); 
    //Select DB 
    mysql_select_db("tvguide"); 

    //Select only results for today and future 
    $result = mysql_query("SELECT programme, channel, airdate, expiration, episode, setreminder FROM epdata1 where airdate >= now() order by expiration GROUP BY airdate"); 
    //You can also add reuslts to paginate here 
    mysql_data_seek($queryresult,0) ; 
      while($row = mysql_fetch_array($result)) 
    { 
    $paginator->add(new paginationData($row['programme'], 
       $row['channel'], 
       $row['airdate'], 
       $row['expiration'], 
       $row['episode'], 
       $row['setreminder'])); 
    } 
    ?> 
     <?php 
    //Show the paginated results 
    $paginator->paginate(); 
    ?><? include("pca-footer1.php"); ?> 
     <?php 
    //Show the navigation 
    $paginator->navigation();  
    ?> 

不過,我在這兩個表,它們是epdata1(其中對我的表演豪斯醫生的airtimes是),並housemdep加上setreminder表。

如何使用與此相關的外鍵?我不確定這是否適用於我的劇本,但願意嘗試。

我想什麼做的是從表housemdep 選擇某些事件(的電視劇集),如果任何被選中它們顯示爲這樣:

House M.D. showing on Channel 1 June 6th - 8:00pm "Wilson" Set Reminder 
House M.D. showing on Channel 1 June 7th - 1:30am "Wilson" Set Reminder 
House M.D. showing on Channel 1 June 7th - 12:55pm "House's Head" Set Reminder 

或類似這樣的,如果我沒有選擇該行的一集:

House M.D. showing on Channel 1 June 7th - 8:00pm "House's Head" Set Reminder 
House M.D. showing on Channel 1 June 8th - 9:00pm Set Reminder 
House M.D. showing on Channel 1 June 9th - 2:30pm Set Reminder 
House M.D. showing on Channel 1 June 7th - 8:00pm "Que Sera Sera" Set Reminder 

外鍵和鏈接表的關係對我來說是新的,如果任何人都可以幫助我會欣賞這一點。

另外,我試圖讓像這樣我的程序(這是代碼應如何呈現,如果你在瀏覽器上查看源文件點擊):

<tr><td><b><a href="housemd.php">House M.D.</a></b></td><td>showing on <a href="channel/1"><i>Channel 1</i></a></td><td>June 9th - 7:00pm</td><td><b>"<a href="episodes/714790">House's Head</a>"</b></td><td><img src="reminder.gif" height="16" width="22"> <a href="reminder.php" alt="Set a reminder" target="_top">Set Reminder</a></td></tr> 

豪斯醫生顯示在頻道1 6月10日 - 上午12時25 「房子的頭」設置提醒 豪斯醫生顯示在頻道1 6月10日 - 下午12:55  設置提醒 豪斯醫生顯示在頻道1 6月10日 - 下午9:00 「威爾遜」設置提醒 豪斯醫生顯示在頻道1 6月11日 - 凌晨1:30 「威爾遜」設置提醒 樓MD顯示在頻道1 6月11日 - 下午8:00 「生活的夢想」設置提醒 豪斯醫生顯示在頻道1 6月12日 - 下午7:00  集合R eminder

我已經嘗試了一些Google在這個腳本的另一個版本(這是運行Apache和PHP 5.28/MySQL的本地主機服務器上的原始克隆)的外鍵上建議的內容,但我不確定如何執行這個。

謝謝。

+0

你有什麼麻煩?數據庫查詢是否需要通過外鍵將表連接起來?如果沒有,那麼將表格連接起來,看看你的分頁功能。最好的學習方法是嘗試。修改來修復。 此外,這不是分頁查詢的最佳方式。如果您的查詢具有10k條結果,它將緩存所有這些結果,然後您從10k的緩衝列表中只使用20條(或每頁值)。通常最好使用mysql關鍵字LIMIT來讓查詢只返回所需的行數。 – 2010-06-07 06:20:21

+0

我無法使用通過外鍵將表連接在一起的數據庫查詢,這是它的要點。 至於我的分頁腳本,每個腳本顯示20行,這是一個免費的腳本,我正在使用;) – whitstone86 2010-06-07 08:56:21

回答

0

如果我明白你在說什麼,你希望SQL查詢連接兩個表,並且根據數據庫的響應,顯示行的方式會有所不同,具體取決於在一個表中還是另一個表中找到行。

你說對了,你正在尋找的是聯接,這聽起來像你需要了解聯接的類型。 INNER JOIN適用於您期望某個表中的行始終與另一個表中的行相對應(並且如果它們沒有相應的行則永不返回行)的情況。左連接適用於查詢某個表中的數據的情況,但您可能有或沒有相應的行可用於另一個表中。

例如:一個人將永遠有父母(INNER JOIN)。

SELECT 
    person.id, 
    relative.id 
FROM 
    person 
INNER JOIN 
    relative 
ON 
    person.biological_mother_id = relative.id 

在這個例子中,行會回來,在那裏既person.id和relative.id將有價值觀,否則,數據庫將不返回行,因爲這些行不匹配的INNER JOIN標準。

但是,一個人不會總是有寵物(左連接)。

SELECT 
    person.id, 
    pet.id 
FROM 
    person 
LEFT JOIN 
    pets 
ON 
    person.id = pets.owner_id 

在這種情況下,可以返回行,其中person.id具有值,但pet.id沒有值(NULL值,在某些情況下)。這是因爲LEFT JOIN不會像INNER JOIN那樣對返回的行進行約束,而是表示「左」或「主」表可以返回行值,但對於「右」或「副」 「表格返回值。

正是通過這種方式,我們使用關係數據庫來提出關於表之間關係的問題。如果您執行上述寵物查詢,並且pet.id返回null,則您知道該人沒有寵物。