用户是否可以输入让我们说“红色汽车”的信息。它会在数据库和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 >
$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
这些是DB中图像的名称:
http://puu.sh/cCHwa/a82d2cc7fe.png
谢谢!
答案 0 :(得分: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="" />';
}
?>
答案 1 :(得分:0)
我理解这个问题的方式,我会这样做
编辑数据库表,以便在包含“ser_pic1.jpg”的图像之后得到“image_name”
SELECT * FROM {your_database} WHERE image_name =“Red car”
并发布如