2014-02-11 42 views
0

我需要你們的幫助。使用php從mysql中的3個表創建下載腳本

問題是我必須顯示從其他表連接的表的下載鏈接,有三(3)個表。

**First Table:** 

file_id | file_title | file_name | file_dir 
------------------------------------------- 
     |   |   | 


**Second Table:** 

file_id | books_id 
------------------- 


**Third Table:** 

books_id | books_title | books_author | books_publisher 
----------------------------------------------------------- 

我只是想創建一個按鈕,可以從第一個表中下載文件,文件存儲在一個文件夾中。我有點困惑,爲什麼在我之前建立這個腳本的開發人員(現在這個人退出了,我不能聯繫他)爲了上傳的文件添加到三個表中。如果我改變了上傳領域,我必須改變一切。

任何線索或鏈接可以幫助我解決我的困惑。

謝謝你們在這裏的幫助。

對不起,我的英文。 :)

+0

您可以加入3張表並獲取所有信息,然後使用循環使用file_dir/file_name生成下載鏈接 –

+0

祝您的作業順利:) – DanFromGermany

回答

1

我相信你正在尋找的查詢是:

SELECT t1.file_title, t1.file_name, t1.file_dir, 
     t3.books_title, t3.books_author, t3.books_publisher 
FROM first_table t1, second_table t2, third_table t3 
WHERE t1.file_id=t2.file_id AND 
     t2.books_id=t3.books_id 

這裏假設你的表的名稱是first_tablesecond_tablethird_table。隨意修改。

要使用這個結果在PHP中,你可以做這樣的事情:

$sql = "SELECT t1.file_title, t1.file_name, t1.file_dir, " . 
     "  t3.books_title, t3.books_author, t3.books_publisher " . 
     "FROM first_table t1, second_table t2, third_table t3 " . 
     "WHERE t1.file_id=t2.file_id AND " . 
     "  t2.books_id=t3.books_id"; 

$query_result = mysqli_query($sql); 

$data = array(); 
while ($row = mysqli_fetch_assoc($query_result)) { 
    $row_data = array(); 
    foreach ($row as $key => $value) { 
    $row_data[$key] = $value; 
    } 
    array_push($data, $row_data); 
} 

foreach($data as $item) { 
    $path_to_file = $item['file_dir'] . '/' . $item['file_name']; 
    print "<a href='$path_to_file'>" . 
      $item['books_title'] . 
      ' (Author: ' . $item['books_author'] . ', ' . 
      ' Publisher: ' . $item['books_publisher'] . ')</a>'; 
    print '<br>'; 
} 

當然,HTML的輸出完全是爲了演示目的 - 我不知道你需要什麼樣的格式。關鍵件明白是:

  • $path_to_file共同基礎上,$item['file_dir']$item['file_name']
  • 讓你的鏈接(或您的按鈕,或任何你選擇使用)指向$path_to_file
0

SELECT FirstTable.file_name, FirstTable.file_dir, ThirdTable.books_title, ThirdTable.books_author, ThirdTable.books_publisher INNER JOIN SecondTable ON FirstTable.file_id = SecondTable.file_id INNER JOIN ThirdTable ON SecondTable.books_id = ThirdTable.books_id

INNER JOIN不一定要使用的連接類型,但是這將是抓住從對應於第三(SecondTable)2個表中的數據的總體思路哪個環節他們。

$link = $row['file_dir'] . $row['file_name'];