2015-08-14 17 views
0

我用一個簡單的表單和一些php上傳了一些圖像到MySQL數據庫。 該窗體有以下選擇類別輸入。如何使用php按類別上傳圖片?

<select name="image_ctgy"> 
<option value="animals">animals</option> 
<option value="vegetables">vegetables</option> 
<option value="minerals">minerals</option> 
</select> 

每個圖像都有MySQL表'image_ctgy'中的這些類別之一。 現在我想將類別動物中的圖像回顯到我的網頁。

我使用下面的代碼讓我的網​​頁上的所有圖片:

<?php 
/*** Check the $_GET variable ***/ 
    try { 
      /*** connect to the database ***/ 
      $dbh = new PDO("mysql:host=;dbname=", '', ''); 

      /*** set the PDO error mode to exception ***/ 
      $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

      /*** The sql statement ***/ 
      $sql = "SELECT image_id, thumb_height, thumb_width, image_ctgy, image_type, image_name, title FROM testblob"; 

      /*** prepare the sql ***/ 
      $stmt = $dbh->prepare($sql); 

      /*** exceute the query ***/ 
      $stmt->execute(); 

      /*** set the fetch mode to associative array ***/ 
      $stmt->setFetchMode(PDO::FETCH_ASSOC); 

      /*** set the header for the image ***/ 
      foreach($stmt->fetchAll() as $array) 
       { 
      echo '<div class="thumb" style="width: '.$array['thumb_width'].'px; height: '.$array['thumb_height'].'px;"> 
      <p class="title">' . $array["title"] . '</p> 
      <p><a href="showfile.php?image_id='.$array['image_id'].'"> 
      <img src="showthumbs.php?image_id='.$array['image_id'].'" alt="'.$array['image_name'].' /"> 
      </a></p> 
      </div>'; 

     } 
    } 
    catch(PDOException $e) 
     { 
     echo $e->getMessage(); 
     } 
    catch(Exception $e) 
     { 
     echo $e->getMessage(); 
     } 
?> 

我想我需要在foreach循環中的if語句來檢查,如果圖像有catagry動物。誰能幫我這個?謝謝

+2

您可以用查詢更改WHERE子句:WHERE類=「動物」 –

+0

,我沒有想到的是自己... 。是的,感謝您的幫助! – ShadowMan

回答

0

Raveenanigam發佈了一個awnser我的問題。

您可以用查詢更改WHERE子句:WHERE類=「動物」