doufei2355 2017-01-15 00:44
浏览 8

在db中返回大多数关键字出现在搜索中的行 - codeigniter,php

I'm attempting to create a fairly simple search function that queries the database for user profiles.

In order to do this, the function is going to take the string entered in the search bar and use the explode() function to seperate individual words into an array. These words are represented in the array $keywords as $keys.

I then iterate over the $keywords array, and for each $key I then iterate whether it matches any of the sql queries in the array $fields as $field. If it matches a field, then I want to get the index of the row and add it to an array $indexes.

I take the $indexes array and then find the row with the most matches. In other words, it will return the row which has matched the most via duplicate row indexes in the $indexes array. I then want to return this as my functions result.

Issue: I'm not sure how to retrieve the index of the current row being queried for keywords. This is probably a really simple fix, but I can't find a specific question that answers this.

Thanks.

The code I am mainly concerned is commented below.

foreach($keywords as $key){
        foreach($fields as $field){
            if($key = $field){  
                array_push($indexes, index($query->row())); /*add row at index to 
$indexes array ??? */
            }
        }
}
  • 写回答

1条回答 默认 最新

  • duanshan3331 2017-01-15 04:17
    关注

    Try like this...

    <?php
    $keywords = array('key1','key2','key3','key4');//assumption
    $fields = array('field1','field2','key1','key3');//assumption
    $indexes = array();
    foreach($keywords as $key=>$value){
        foreach($fields as $field_key=>$field_value){
            if($value == $field_value){ 
                array_push($indexes,$field_key);
            }
        }
    }
    print_r($indexes);
    ?>
    

    Output: Array ( [0] => 2 [1] => 3 ) :Matched row's indexes

    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致