2013-06-05 90 views
-1

我有一张名为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("\'", "&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(); 
} 

PHPSELECT语句返回两个结果:

  1. 快速跃过...
  2. 是伟大的...

期望的结果(请帮助)

我会喜欢什么 ,是为了我的发言echo一个结果每次出现$query任期,即使在sa我行,如下所示:

  1. 快速跃过...(从Row 1
  2. ...在偷懒。 (从Row 1
  3. 是伟大的...(从Row 3

问题

如何编辑我PHP和/或我的SELECT语句,这样,我可以echo一个结果为出现$query期限?

+1

通过'PHP'解析来自数据库的'content',看看有多少** **狗中存在的那句话。 –

+0

@YogeshSuthar - 你能解释一下你的意思吗?我不确定我明白。 – adamdehaven

+0

请注意,在用户输入查询数据库时,您应该使用准备好的语句。另外,你正在混合OOP和程序风格的mysqli。这只是在你的例子中,或从字面上你的代码? – newfurniturey

回答

1

这里是你的答WER。

<?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

+0

如果我误解了原谅,请原谅我这个,但我怎么会初始化我的结果,使用我现有的'mysqli'查询,以便结果是'$ mysql_result [0] ['address'] =“www.example.com/page1”;'? – adamdehaven

+0

@AdamD看到更新的答案(没有测试过,但应该没问题):) – Sharky

+0

它部分与我的代码一起工作,但是如果用户有复合词,如“dog park”,则不会返回结果。我正在玩它... – adamdehaven

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(); 
+0

这只是显示相同的结果,一遍又一遍地重复数字='$ count'。我需要移动到每个词的下一个出现。 – adamdehaven

+0

您可以在[strpos](http://php.net/manual/en/function.strpos.php)中使用第3个参数来搜索字符串中的下一个'dog'。 – furas

+0

现在查看示例 - 我添加了'$ offset'。在代码中使用'strpo()'中的'$ offset'作为第3个参数。 – furas