我有一張名爲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