我有一张名为pages
的表格,它在我的网站上存储了网页的ID,网址,标题和内容。下面是表的例子:MYSQLi Select - PHP回显行内的每个查询字词出现
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
呼应他们。例如,如果我想的单词“狗”我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
和SELECT
语句返回两个结果:
- 快速狗跃过...
- 热狗是伟大的...
期望的结果(请帮助)
我会喜欢什么 ,是为了我的发言echo
一个结果每次出现的$query
任期,即使在sa我行,如下所示:
- 快速狗跃过...(从
Row 1
) - ...在偷懒狗。 (从
Row 1
) - 热狗是伟大的...(从
Row 3
)
问题
如何编辑我PHP
和/或我的SELECT
语句,这样,我可以echo
一个结果为每出现$query
期限?
通过'PHP'解析来自数据库的'content',看看有多少** **狗中存在的那句话。 –
@YogeshSuthar - 你能解释一下你的意思吗?我不确定我明白。 – adamdehaven
请注意,在用户输入查询数据库时,您应该使用准备好的语句。另外,你正在混合OOP和程序风格的mysqli。这只是在你的例子中,或从字面上你的代码? – newfurniturey