我有一个名为pages
的表,用于存储我网站上的网页的ID,网址,标题和内容。这是表格的一个例子:
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
和SELECT
语句返回两个结果:
期望的结果(请帮助)
我 偏好 的内容,是我echo
$query
Row 1
Row 1
{{1>} {{1>} {{1>}的结果} term,即使在同一行内,如下所示:
Row 3
)PHP
)SELECT
)问题
如何修改我的echo
和/或$query
声明,以便{strong} 每个 {{1}}一个结果发生{{1}}术语?
答案 0 :(得分:1)
这是你的答案。
<?php
$query="dog";
if ($result = $mysqli->query("SELECT * FROM pages WHERE (`content` LIKE '%".$query."%')"))
{
$inc = 0;
$mysql_result = array();
// if one or more rows are returned do following
while($a_result = $result->fetch_array(MYSQLI_ASSOC))
{
$mysql_result[$inc]=$a_result;
$inc++;
}
$chars_to_show_before = 10; // ...1234567890dog
$chars_to_show_after = 10; // dog1234567890...
foreach($mysql_result as $results)
{
$content = str_replace($query,"my_un1qu3_r3pl4c3m3nt_".$query,$results['content']);
$occurences = explode("my_un1qu3_r3pl4c3m3nt_",$content);
$position_in_original_content = 0;
foreach($occurences as $an_occurence)
{
if (strpos($an_occurence,$query) !== false)
{
$content=$an_occurence;
echo '<li class="media">
<a class="pull-left" href="'.$results['address'].'">
<img class="media-object thumbnail" src="'.$results['image'].'" style="height:100px !important">
</a>
<div class="media-body">
<h4 class="media-heading"><a href="'.$results['address'].'">'.$results['title'].'</a></h4>
<p>...'.str_replace($query,'<strong>'.$query.'</strong>', substr($results['content'],max(0,($position_in_original_content-$chars_to_show_before)),strlen(strlen($query))+$chars_to_show_after+$chars_to_show_before)).'...</p>
</div>
</li>';
}
$position_in_original_content = $position_in_original_content + strlen($an_occurence);
}
}
}
else
{
echo "Sorry, no results found!";
}
?>
在这里工作演示:http://3v4l.org/BTeCU
答案 1 :(得分:0)
正如Yogesh Suthar所说:使用PHP。
这里快速而肮脏的例子
$count = substr_count($content, 'dog');
$offset = 0;
for( $i = 0 ; $i < $count ; $i++ ) {
echo $content; // put you code here with strpos($content,$query,$offset)
$offset = strpos($content, 'dog', $offset) + strlen('dog');
}
修改:
我添加$offset
修改强>
while($results = $result->fetch_array(MYSQLI_ASSOC)){
$content = $results['content'];
$full_content = $content;
$contentItems = $results['contentSearch'];
// Count number of times term in content
$count = substr_count($contentItems, $full_content);
$offset = 0;
for( $i = 0 ; $i < $count ; $i++ ) {
$content = substr($full_content, strpos($full_content, $firstTerm, $offset )-25,160);
// rest of your code
/* ------------------
---end echo statement
--------------------*/
$offset = strpos($full_content, 'dog', $offset) + strlen('dog');
}
}
$result->close();