我有一个名为的表pages
,用于存储我网站上网页的 id、url 地址、标题和内容。以下是该表的示例:
ID | address | title | content |
------------------------------------------------------------------------------------
1 | www.example.com/page1 | Page 1 | The quick dog jumps over the lazy dog. |
2 | www.example.com/page2 | Page 2 | The best thing about morning is breakfast. |
3 | www.example.com/page3 | Page 3 | Hotdogs are great ballpark food. |
我想SELECT
所有出现的查询词,并在PHP
. 例如,如果我想显示单词“ dog ”的搜索结果,我的SELECT
语句将如下所示:
SELECT * FROM pages WHERE (`content` LIKE '%dog%')
我的完整PHP
声明与搜索词 =$query
看起来像这样:
if ($result = $mysqli->query("SELECT * FROM pages WHERE (`content` LIKE '%".$query."%')")) {
// if one or more rows are returned do following
while($results = $result->fetch_array(MYSQLI_ASSOC)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
$content = $results['content'];
$contentItems = $results['contentSearch'];
// Count number of times term in content
$count = substr_count($contentItems, $originalQuery);
$content = substr($content,strpos($content,$firstTerm)-25,160);
$content = (strlen($content) > 55 ? '...'.$content.'...' : substr($results['description'],0,160).'...');
foreach($querySplit as $term){
$content = str_ireplace($term, '<strong>'.$term.'</strong>', $content);
}
// highlight search terms
$chars = array("\'", "&");
$charReplace = array("'", "&");
$content = str_replace($chars,$charReplace,$content);
// set $image if not empty
$image = (!empty($results['image']) ? 'http://www.example.com/'.$results['image'] : '');
/* ------------------
------ echo statement
--------------------*/
echo '
<li class="media">';
// if image is not empty
if(!empty($image)):
echo '<a class="pull-left span2 hidden-phone" href="http://www.example.com/'.$results['address'].'"> <img class="media-object thumbnail" src="'.$image.'" alt="'.$results['description'].'"/> </a>';
endif;
echo '
<div class="media-body">
<h4 class="media-heading"><a href="http://www.example.com/'.$results['address'].'">'.htmlspecialchars_decode($results['title']).'</a></h4>
<p class="result-address"><small><a href="http://www.example.com/'.$results['address'].'">http://www.example.com/'.$results['address'].'</a></small></p>
';
/*if(!empty($image)):
echo '<a class="visible-phone" href="'.$results['address'].'"> <img class="thumbnail phone-search-thumb" src="'.$image.'" alt="'.$results['description'].'"/> </a>';
endif;*/
echo '
<p class="result-content">'.$content.'</p>
</div>
</li>
';
/* ------------------
---end echo statement
--------------------*/
} $result->close();
}
此PHP
andSELECT
语句返回两个结果:
- 快狗跳过...
- 热狗很棒...
期望的结果(请帮助)
我更喜欢的是,对于每次出现的术语,即使在同一行中,我的声明也echo
有一个结果,如下所示:$query
- 快狗跳过...(来自
Row 1
) - ...在懒惰的狗身上。(来自
Row 1
) - 热狗很棒...(来自
Row 3
)
问题
我如何编辑我的PHP
和/或我的SELECT
陈述,以便每次出现该术语时我都可以echo
得到一个结果?$query