I have a table called pages
that stores the id, url address, title, and content of webpages on my site. Here's an example of the table:
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. |
I would like to SELECT
all occurrences of a query term, and echo them in PHP
. For example, if I want to display search results for the word "dog" my SELECT
statement would look like this:
SELECT * FROM pages WHERE (`content` LIKE '%dog%')
and my full PHP
statement with the search term = $query
looks like this:
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();
}
This PHP
and SELECT
statement returns two results:
- The quick dog jumps over the...
- Hot dogs are great...
DESIRED RESULT (please help)
What I would prefer, is for my statement to echo
one result for every occurrence of the $query
term, even within the same row, as shown here:
- The quick dog jumps over the... (from
Row 1
) - ...over the lazy dog. (from
Row 1
) - Hot dogs are great... (from
Row 3
)
QUESTION
How can I edit my PHP
and/or my SELECT
statement so that I may echo
one result for every occurrence of the $query
term?
content
from database byPHP
and see how many dog present in that sentence. – Yogesh Suthar Jun 5 '13 at 12:53