2014-12-04 139 views
0

我使用Sphinx Search来搜索我的网站上的项目。我是使用foreach找出索引,搜索来自哪里? Foreach循环将迭代所有匹配搜索项的项目,并找出索引。foreach循环中嵌套while循环的结果集结构

数据库中的所有项都有user_id,它引用用户表中的用户。因此,我想输出的是显示少量项目(搜索)并将其放在该用户下。如果搜索“Book”,则输出像这样

Alex  //Book1,Book2,Book3 all of them has user_id of 2 which refers to Alex 

Book1 
Book2 
Book3 

Jordan  //Book11,Book12,Book18 all of them has user_id of 5 which refers to Joradn 

Book11 
Book12 
Book18 

and so on. 

以下是一个

foreach ($res["matches"] as $doc => $docinfo) { 
    switch($docinfo['attrs']['table_id']) { 
     case 1:       //if search come from index 1 

     $res_db = mysqli_query($connect3,'select id_2, image, user_id, title, detail from lunch_menu where id_2 = (('.$doc.'-300000))'); 
      if ($res_db === false) { 
        echo "Error in mysql query #" . mysqli_errno($connect) . ' - ' . mysqli_error($connect); 
      } else { 
      $row = mysqli_fetch_assoc($res_db); 
          $connect4=mysqli_connect('localhost', 'root', '','user'); 
        $sql2="SELECT * FROM `user`.`user_det` WHERE id='".$row['user_id']."' GROUP BY id ORDER 
        BY id DESC"; 
        $query2=mysqli_query($connect4,$sql2); 
        while($row2 = mysqli_fetch_assoc($query2)){ //line X 
        $userx=$row2['id']; 
        $image=$row2['img']; 
        $busi_title=$row2['busi_title']; 

        $page_owner.="<img src='../../account/$userx/$image' width='140' 
        height='140'><b>$busi_title</b><hr>"; 

        $res_db2 = mysqli_query($connect3,'select id_2, image, user_id, title, detail from 
        lunch_menu where id_2 in ((' . $doc . '-300000)) order by field(id_2,('.$doc.'-300000))'); 
        $alu=""; 
        while($row3 = mysqli_fetch_assoc($res_db2)){ 
          $alu.= 

          '<img src="../../'.$row3['user_id'].'/lunch/'.$row3['image'].'" width="100" height="100">' 
          . '<a href="../../'.$row3['user_id'].'/menu_item2.php">' . $row3['title'] . '<a>' 

          . '<br/> '.$row3['detail'].'<br><br><br><br>' ; 
         } 
        $all.="<div id='' style='border:2px solid black;'>".$page_owner.".".$alu."</div>"; 
        } 
    } 

但上面的代码是给我造成这样

Alex 
Book1 

Alex 
Book2 

Alex 
Book3 

Jordan 
Book11 

Jordan 
Book12 

Jordan 
Book18 

and so on. 
代码

我需要做些什么修改才能让我的代码像预期的那样工作。我尽了最大努力找到一个解决方案,但没有帮助。所以,只是发了一个SO帐户来发布问题。

请帮忙,如果可以的话。那真是太棒了。

在此先感谢

感谢您瑞安你reply.We几乎没有

1更多修复,那么我们done.Now什么,我得到的是

Problem 
Result is almost the way,I was looking for except for the first one.This is what I'm getting 

Alex 
Book1 

Jordan 
Book11 
Book12 
Book18 

Alex 
Book2 
Book3 

如果我可以在回复$all之前临时执行order by user_id,则可能会修复此错误。

因为$all打印user和它的corresponding item它在数据库

请告知,我使用的是第二种方式整理出来的样子,你showed.First一个不能正常工作。

我想通了第二部分的problem.To的实现这一理想的结果,我需要做的这

$cl->SetSortMode(SPH_SORT_EXTENDED, '@relevance DESC,user_id ASC'); 

这行代码输出的第一个最相关的搜索结果,并通过user_id升序分类。

Special Thanks to Ryan

+0

保存当前的用户ID,并且只打印出用户信息 - >'$ page_owner = “$ busi_title


”;'如果用户ID发生变化。即。 '$ current_user = 0;如果($ current_user!= $ userx){$ page_owner。= ...; $ current_user = $ userx;' – Sean 2014-12-04 20:25:27

+0

感谢您的答复。是,user_id更改,只是$ page_owner打印出来。我做了你说的,但仍然得到相同的结果 – Coder 2014-12-04 20:40:06

+0

**警告**:当使用'mysqli'您应该使用参数化查询和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)将用户数据添加到您的查询中。 **不要**使用字符串插值来实现此目的,因为您将创建严重的[SQL注入漏洞](http://bobby-tables.com/)。 – tadman 2014-12-04 21:15:36

回答

2

所以我有点困惑,其中重复的用户是从,因为我不知道你在查询什么,但我有想法可能是导致它而产生。

首先,如果内环是问题,那么它是一个简单的修复。只在第一个循环中显示'user_id'。

   $alu=""; 
       $firstLoop = true; 
       while($row3 = mysqli_fetch_assoc($res_db2)){ 
         if($firstLoop){ 
          $alu.= '<img src="../../'.$row3['user_id'].'/lunch/'.$row3['image'].'" width="100" height="100">' 
          $firstLoop = false; 
         } 
         $alu.= '<a href="../../'.$row3['user_id'].'/menu_item2.php">' . row3['title'] . '<a>' 

         . '<br/> '.$row3['detail'].'<br><br><br><br>' ; 
        } 

另外,它可能会发生在外部循环,因为肖恩建议。在这种情况下,我们可以跟踪前一个用户,以确定是否应该设置$ userx并使用$ page_owner。

 $prevUsr = ''; 
    foreach ($res["matches"] as $doc => $docinfo) { 
switch($docinfo['attrs']['table_id']) { 
    case 1:       //if search come from index 1 

    $res_db = mysqli_query($connect3,'select id_2, image, user_id, title, detail from lunch_menu where id_2 = (('.$doc.'-300000))'); 
     if ($res_db === false) { 
       echo "Error in mysql query #" . mysqli_errno($connect) . ' - ' . mysqli_error($connect); 
     } else { 
     $row = mysqli_fetch_assoc($res_db); 
         $connect4=mysqli_connect('localhost', 'root', '','user'); 
       $sql2="SELECT * FROM `user`.`user_det` WHERE id='".$row['user_id']."' GROUP BY id ORDER 
       BY id DESC"; 
       $query2=mysqli_query($connect4,$sql2); 
       while($row2 = mysqli_fetch_assoc($query2)){ 
       $userx=$row2['id']; 
       $image=$row2['img']; 
       $busi_title=$row2['busi_title']; 

       $page_owner.=""; 

       // only set the page owner if the user id is different 
       if($prevUsr != $userx){ 
        $page_owner.="<img src='../../account/$userx/$image' width='140' 
        height='140'><b>$busi_title</b><hr>"; 

        $prevUsr = $userx; 
       } 

       $res_db2 = mysqli_query($connect3,'select id_2, image, user_id, title, detail from 
       lunch_menu where id_2 in ((' . $doc . '-300000)) order by field(id_2,('.$doc.'-300000))'); 
       $alu=""; 
       while($row3 = mysqli_fetch_assoc($res_db2)){ 
         $alu.= 

         '<img src="../../'.$row3['user_id'].'/lunch/'.$row3['image'].'" width="100" height="100">' 
         . '<a href="../../'.$row3['user_id'].'/menu_item2.php">' . $row3['title'] . '<a>' 

         . '<br/> '.$row3['detail'].'<br><br><br><br>' ; 
        } 
       $all="<div id='' style='border:2px solid black;'>".$page_owner.".".$alu."</div>"; 
       } 
} 

我希望其中一个可以帮助你。

快乐编码。 瑞安

+0

感谢您的回复。我已经使用了第二种方法,因为第一种方法不起作用。请看看@更新的问题。您的帮助将非常感谢 – Coder 2014-12-10 19:37:15

+0

当我回显$ userx时,我得到9 7 7 9 9 9 9 。这里7和9是user_id。为什么我没有得到9 9 9 9 9 7 7,因为我按ID排序?如果我可以通过回显userx来实现这个结果,那么我的程序将100% – Coder 2014-12-11 06:06:00