2015-04-05 120 views
0

我有一個問題,我正在尋找最簡單的解決方案!所以我有這個數據庫項目,我將使用PHP。我們認爲我們需要用這種我們不太熟悉的語言來解決這個問題。無論如何,假設我使用postgresql創建了一個下拉列表(例如美食類型列表)(下面的代碼示例)。我的下拉菜單隻能找到,但是我可以添加一個功能,以便他們可以點擊他們想要的任何菜餚,然後重定向到帶有相應餐廳的新頁面(我將再次從數據庫中獲取)。下面的代碼是不是真的很重要,但生病後,這樣你可以有一個想法。遺憾的是太長。指定鏈接到下拉菜單

<form action="index.php" method="post"> 
<select name="****" id="****"> 

<option>Select book</option> 
     <?php 
$conn_string = "host=########################################"; 
      $dbconn = pg_connect($conn_string) or die ('Connection failed'); 
      $query = "SELECT name FROM db.typeofcuisine"; 
      $result = pg_query($dbconn, $query); 

           while ($row = pg_fetch_assoc($result)) 
           { 
             echo "<option value=$row[name]>$row[name]</option>"; 
           } 
           pg_close($dbconn); 

    ?> 

而且其實我是想這種形式被分配給一個鏈接(不只是一個選擇的選項)。我的意思是,我將有一個鏈接,當點擊時,然後我顯示錶格

回答

0

你可以嘗試這種方式來設置鏈接到你的下拉菜單,請參閱select關於更改事件,其中值是您的數據庫選擇restaurant_link相應restaurant名稱

<form action="index.php" method="post"> 
<select name="****" id="****" onchange="location = this.options[this.selectedIndex].value;"> 

<option>Select book</option> 
     <?php 
      $conn_string = "host=########################################"; 
      $dbconn = pg_connect($conn_string) or die ('Connection failed'); 
      $query = "SELECT name,restaurant_link FROM db.typeofcuisine"; 
      $result = pg_query($dbconn, $query); 

      while ($row = pg_fetch_assoc($result)) 
      { 
      echo "<option value=$row['restaurant_link']>$row['name']</option>"; 
      } 
      pg_close($dbconn); 

    ?> 
</select> 
</form> 
+0

好吧,虐待嘗試這樣做 – 2015-04-05 16:50:48