2017-10-14 32 views
0

首先,如果這個問題似乎是重複的,或者您覺得我還沒有完成我的盡職調查,請致歉。我以前通常在互聯網上衝浪,但我覺得好像我需要問。在搜索功能旁邊使用一頁網址

所以,基本上,我正在建立一個單頁的動態網站,列出當地城市的一些景點。這一部分我沒事 - 我使用MySQL創建了一個數據庫,我有1個數據庫,4個不同的表,每個表都有不同的ID名稱。

我的問題是,我的搜索僅限於用戶輸入的內容,即吸引力的名稱,我努力實際將其鏈接到任何動態URL(除了第一個表格循環查詢)

我的代碼如下解釋了爲什麼它會這樣做,我希望上帝在外面的某個人可以引導我回答問題,無論是外部鏈接等。我會非常感激並永遠感激。

<?php 
error_reporting(0); 
?> 



<?php 



      mysqli_connect("localhost", "root", "", "everydayleeds") or die(mysqli_error()); 
      mysqli_select_db($con, "everydayleeds") or die(mysqli_error()); /* everydayleeds is the name of database we've created */ 


      $query = $_GET['query']; // 'query' is the variable name given to the $_GET action, which will take whatever is typed into the search box, indicating this via the URL paramater' 

      $min_length = 3; // You can set minimum length of the query if you want 


      if(strlen($query) >= $min_length){ // If query length is more or equal minimum length then 

       $query = htmlspecialchars($query); // This changes characters used in HTML to their equivalents, for example: < to &gt; 

       $query = mysqli_real_escape_string($con, $query); // Makes sure nobody uses SQL injection 


       $raw_results = mysqli_query ($con, "SELECT foodndrink.Name AS fdName, nightlife.Name AS nightName, culture.Name AS cultName, placestostay.Name AS pName, foodndrink.fdID AS foodID, nightlife.nightID AS nID, cultID AS cID, placesID AS pID FROM foodndrink, nightlife, culture, placestostay 


        WHERE (foodndrink.Name LIKE '%".$query."%') OR (nightlife.Name LIKE '%".$query."%') OR (culture.Name LIKE '%".$query."%') OR (placestostay.Name LIKE '%".$query."%') ") OR die (mysqli_error($con)); 


       if(mysqli_num_rows($raw_results) > 0){ // If one or more rows are returned do following 

        while($results = mysqli_fetch_assoc($raw_results)){ // $results = mysql_fetch_assoc($raw_results) puts data from database into array, while it's valid it does the loop    
         echo "Yes, we have a".$_GET['query']."<a href=attractions.php?category&fdID=".$results['foodID']; 

break; 

        } 

       } 

       else{ // if there is no matching rows do following 
        echo "<p>Nope, Leeds probably doesn't have a </p>".$_GET['query']; 
       } 

      } 
      else{ // if query length is less than minimum 
       echo "Minimum length is ".$min_length; 
      } 




?> 

所以,鏈接,用戶可一旦他們發現的結果僅僅是鏈接回因事實,我已經制定了一套?category&fdID=".$results['foodID'];食品飲料ň頁面點擊。

我試過循環,JOINs,我似乎在圈子裏四處走動。我相信我的問題只是事實,我有4個不同的表和所有4個不同的ID。

任何意見,幫助將不勝感激。

謝謝。

+0

是的,你是無法看到的代碼?代碼預覽似乎沒有捕獲我所有的代碼,但我使用了HTML預覽代碼。 – Bilaal

回答

0

我不確定,但我認爲如果你使用「Union all」方法,你可以添加靜態文本到每個表的結果。有一個樣品in here

之後,你可以讓這樣的事情

echo "<a href=attractions.php?category&".$results['tableName']."&ID=".$results['foodID']; 
+0

是的,今天我遇到了UNION,我想我的答案可能在於此。我會看看,謝謝! – Bilaal