2014-11-04 64 views
0

用戶可能輸入讓我們說'Red Car',它會在數據庫和php/html中找到圖像,並在搜索結果中顯示圖像?顯示從數據庫到結果頁面的圖像

我對「搜索」

<?php 
    $query = $_GET['query']; 


    $min_length = 3; 


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

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

     $query = mysql_real_escape_string($query); 
     // makes sure nobody uses SQL injection 

     $raw_results = mysql_query("SELECT * FROM articles 
      WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error()); 

     // * means that it selects all fields, you can also write: `id`, `title`, `text` 
     // articles is the name of our table 

     // '%$query%' is what I'm looking for, % means anything, for example if $query is Hello 
     // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query' 
     // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query' 

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

    while($results = mysql_fetch_array($raw_results)){ 
    // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 



    echo "<div class='successmate'><h2></h2></a> 

    </div><br><br><br>"; 

echo "<div class='search69'><a href='../pages/{$results['page_name']}'><h2>{$results['title']}</h2></a><p>{$results['text']}</‌​p>"; 



    } 


} 
    else{ // if there is no matching rows do following 
    echo ("<br><br><div class='search1'><h2>No results</h2></br></br>"); 
} 

} 

else{ // if query length is less than minimum 
echo ("<br><br><div class='search1'><h2>Minnimum Length Is</h2><h2>".$min_length); 
} 

?> 

,我希望這些圖像的代碼爲關鍵字找到:

http://puu.sh/cCHv1/9f58d770f3.jpg

這些都在數據庫中的圖像的名稱:

http://puu.sh/cCHwa/a82d2cc7fe.png

謝謝!

回答

0

路上,我理解這個問題,我會做這樣的

編輯您的數據庫表,所以你也得到了「IMAGE_NAME」,圖像後,你含有「ser_pic1.jpg」

SELECT * FROM {} your_database WHERE IMAGE_NAME = 「紅車」

,並張貼像

link here

1

沒有測試我會說你有你的搜索功能下來,顯示結果將是一件簡單的事情簡單地把下面的代碼,你想要顯示圖像結果(這隻會用於顯示圖像,但把它與其他結果的工作是一個簡單的返工)

<?php 
while($data = $result->fetch_assoc()){ 
    echo '<img src="'.$data['image_path'].'" alt="" title="" />'; 
} 
?> 

然後你就可以簡單地包裹在什麼都容器中的圖片,如果你願意的話,另一種方式來做到這一點可能是結果存儲到'陣列並用foreach循環顯示它

<?php 
while($data = $result->fetch_assoc()){ 
    $imageArray[] = $data['image_path']; 
} 

/*Code below you can place where ever you want, no need to place it directly after the 
above query execute*/ 

foreach($imageArray as $imgPATH){ 
    echo '<img src="'.$imgPATH.'" alt="" title="" />'; 
} 
?>