2012-04-03 123 views
0

我試圖創建一個電子商務網站主要銷售使用PHP和MySQL的PC組件。使用圖像目錄顯示圖像

我使用的是其中有一個被稱爲「部件」表中存儲PC組件與像「名稱」,「類型」等字段的數據庫我已經爲我被告知了一個名爲「圖像」字段最好是將圖像存儲在文件系統中而不是BLOB。該字段包含該組件的圖像的名稱(例如,英特爾的i7 2700K處理器提交的圖像具有inteli72700k/JPG)。該圖像將存儲在'../images/products/'中。

現在我試圖讓我的產品列表(即用戶點擊'處理器',我現在試圖顯示我們有產品的列表,類似於overclockers.co.uk)。我創建了一個表格,其中表格標題是產品的名稱,每個產品有一行。

<table id="products"> 
      <?php 
      //Connects to the DB, localhost is yourpc, root is the username and there is no password 
      $con = mysql_connect('localhost','root',''); 
      //This is the database we want to access in phpMyAdmin 
      mysql_select_db('pctweeks'); 

      $query = "SELECT * FROM component WHERE type = 'CPU'"; 

      $query_run = mysql_query($query) or die(mysql_error($con)); 

      if (mysql_num_rows($query_run) >= 1){ 

      while($row = mysql_fetch_assoc($query_run)) { 
       echo "<th>{$row['name']}</th>" . 
       "<tr><td>{$row['image']}</td></tr>"; 
      } 
      } else 
      { 
       echo 'No Products'; 
      } 
     ?>    
     </table> 

問題是{$ row ['image']}部分。我知道我必須使用圖片代碼,但我不知道如何$行[「形象」]前面插入圖片的目錄。

回答

1

廣東話您使用類似的代碼在這裏:

echo "<th>{$row['name']}</th>" . 
    "<tr><td> <img src='../images/products/{$row['image']}'/> </td></tr>"; 
+0

這個工作,我以爲我是接近,但我沒有使用路徑上的單引號(「../images ..」)。非常感謝! – Mike 2012-04-03 12:13:36

0

你可以試試這個:

define('IMG_PATH', 'PATH_OF_YOUR_IMAGE_DIRECTORY_HERE'); 

while($row = mysql_fetch_assoc($query_run)) { 
    echo "<th>{$row['name']}</th>" . 
    "<tr><td><img src='".IMG_PATH."'{$row['image']}</td></tr>"; 
} 

替換 「PATH_OF_YOUR_IMAGE_DIRECTORY_HERE」 與您的實際圖像文件夾路徑。

希望這有助於..