dongxiong1941 2016-05-30 01:32
浏览 41

php jquery json即时搜索

I'm trying to create an instant search to pull the data from my database. I want it to be able to search 2 fields from 2 different tables. So far, I have it working that if you choose one specific field of either table, it will work just fine, currently; it is pulling the data and displaying it, but I want the search to be specific.

For example, let's say I have 2 tables, one is for artist, and the other one is for their records; when you type the name of the artist, it will show all the records form that artist, but if you type the date of the release alongside of the name, it will give you that specific record and it won't show others. That's how I want it to work.

Here's what I've got so far:

include 'conn.php';

$query = $_POST['query'];

$like = "'%$query%'";
$sql = mysqli_query($db, "SELECT * FROM users  JOIN tshift ON users.id=tshift.uid  WHERE name LIKE {$like}
    LIMIT 6");

$arr = array();
while ($r = mysqli_fetch_assoc($sql))
{
    $results= array();

    foreach($r as $key => $val){
        if(is_string($key))
            $results[$key] = $val;
    }
    array_push($arr, $results);
}
echo json_encode(array("artist" => $arr,"album" => $arr,"release" =>$arr));
  • 写回答

1条回答 默认 最新

  • duanhuan1147 2016-05-30 04:32
    关注

    for those that wonder or will wonder one day how to do something like this, this is how i got it to work i don't know if is the best way but it will do for now.

        include 'conn.php';
    
    $query = $_POST['query'];
    
        $newQuery = explode("+", $query);
        $name = "'%{$newQuery[0]}%'";
    
        if(!empty($newQuery[1])){
            $date = "'%{$newQuery[1]}%'";
        }
    
        if(empty($date))
        {
            $select = "SELECT * FROM users  JOIN tshift ON users.id=tshift.uid  WHERE name LIKE {$name}";
    
        }
        else
        {
            $select = "SELECT * FROM users  JOIN tshift ON users.id=tshift.uid  WHERE name LIKE {$name} AND udate LIKE {$date} ";
    
        }
    
    
    
       $sql = mysqli_query($db,$select);
    $arr = array();
    while ($r = mysqli_fetch_assoc($sql))
    {
        $results= array();
    
        foreach($r as $key => $val){
            if(is_string($key))
                $results[$key] = $val;
        }
        array_push($arr, $results);
    }
    echo json_encode(array("artist" => $arr,"album" => $arr,"release" =>$arr));
    
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊