-1

我有一个名为的表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("\'", "&amp;");
  $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();
}

PHPandSELECT语句返回两个结果:

  1. 跳过...
  2. 热狗很棒...

期望的结果(请帮助)

更喜欢的是,对于每次出现的术语,即使在同一行中,我的声明也echo有一个结果,如下所示:$query

  1. 跳过...(来自Row 1
  2. ...在懒惰的狗身上。(来自Row 1
  3. 热狗很棒...(来自Row 3

问题

我如何编辑我的PHP和/或我的SELECT陈述,以便每次出现该术语时我都可以echo得到一个结果?$query

4

2 回答 2

1

Here is your answer.

<?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!";
}
?>

working demo here: http://3v4l.org/BTeCU

于 2013-06-08T08:05:28.280 回答
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();
于 2013-06-07T14:23:06.003 回答