Valen Tin
Valen Tin

Reputation: 169

Search with ajax , "like" in array

Please help with search script

$("#search").on("keyup",function(){
    var search=$("#search").val();
    search=search.replace(/  +/g, ' ');
    if(search.length>=1){
        var result=search.split("");
        $.ajax({  
            type: 'POST',  
            url: "{{URL('/searchajax')}}", 
            data: 
                { 
                    search:result
                },
            success: function(data) {
                $("#searchresult").css("display","block");
                if(data.length>0){
                    for(var i=0;i<data.length;i++){
                        $.each(data[i], function( index, value ) {
                            $("#itemssearch").append("<a href='{{URL('/product')}}/"+value.id+"'>\
                                                            <li>"+value.originalname+value.name+"</li>\n\
                                                        </a>\n\
                                                        ");

                            });
                    }
                }else{
                      ...
                }
            }
        });
    }
});

public function searchajax(Request $request){
    $search=$request->search;
        $return=DB::table("products")
                ->where('originalname', 'like', '%'.$search.'%')
                ->orWhere('name', 'like', '%'.$search.'%')
                ->orderby("products.table_id")
                ->take(5)
                ->get();

    return response()->json($return);
}
  1. if have search word "iphone 7s" how to search first "iphone" then "7s" and get all result in one variable
  2. if have search word "iphone 7s" how to search first "iphone" then "7s" and get all result in one variable

Upvotes: 1

Views: 1678

Answers (3)

govind
govind

Reputation: 1

example with CodeIgniter controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->model('common_model');
        $var1='engineering';

        $where=" id !='0'";
        $a['reco']=$this->common_model->get_entry_by_data('collages',false,$where);

        $this->load->view('welcome_message',$a);


    }


    public function ajaxcall()
    {   
    $searchval=$this->input->post('lalla');
    $this->load->model('common_model');

        $where="`city` like '%".$searchval."%' or `deparment` like '%".$searchval."%'";
        $a=$this->common_model->get_entry_by_data('collages',false,$where);


        $result='';
        $result="<table>
        <tr><td>name</td><td>city</td><td>deparment</td></tr>";
        if(!empty($a))
        { 
         foreach($a as $recos){
        $result.="<tr><td>". $recos['name']."</td><td>". $recos['city']."</td><td>". $recos['deparment']."</td></tr>"; 
        }
        }else{
            $result.="<tr><td>no result</td><td>no result</td><td>no result</td></tr>";

        }
        $result.="</table>";

        echo $result;



}
}

view example

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <style type="text/css">

    ::selection { background-color: #E13300; color: white; }
    ::-moz-selection { background-color: #E13300; color: white; }

    body {
        background-color: #fff;
        margin: 40px;
        font: 13px/20px normal Helvetica, Arial, sans-serif;
        color: #4F5155;
    }

    a {
        color: #003399;
        background-color: transparent;
        font-weight: normal;
    }

    h1 {
        color: #444;
        background-color: transparent;
        border-bottom: 1px solid #D0D0D0;
        font-size: 19px;
        font-weight: normal;
        margin: 0 0 14px 0;
        padding: 14px 15px 10px 15px;
    }

    code {
        font-family: Consolas, Monaco, Courier New, Courier, monospace;
        font-size: 12px;
        background-color: #f9f9f9;
        border: 1px solid #D0D0D0;
        color: #002166;
        display: block;
        margin: 14px 0 14px 0;
        padding: 12px 10px 12px 10px;
    }

    #body {
        margin: 0 15px 0 15px;
    }

    p.footer {
        text-align: right;
        font-size: 11px;
        border-top: 1px solid #D0D0D0;
        line-height: 32px;
        padding: 0 10px 0 10px;
        margin: 20px 0 0 0;
    }

    #container {
        margin: 10px;
        border: 1px solid #D0D0D0;
        box-shadow: 0 0 8px #D0D0D0;
    }
    </style>
</head>
<body>

<div id="container">
    <h1>Welcome to CodeIgniter!</h1>

    <div id="body">

        <input type="text" name="searchbox" value="" id="searchbox"/>
        <button name="btn" id="btn">search</button>
        <div id="replace">
        <table>
        <tr><td>name</td><td>city</td><td>deparment</td></tr>
        <?php foreach($reco as $recos){?>
        <tr><td><?php echo $recos['name'];?></td><td><?php echo $recos['city'];?></td><td><?php echo $recos['deparment'];?></td></tr>
        <?php }?>
        </table>
        </div>
</div>

</body>
</html>
<script>
$("#btn").click(function() {
    var searchval=$("#searchbox").val();

 $.ajax({
        url: "index.php/Welcome/ajaxcall", 
        type: "post",
        data: {'lalla':searchval} ,
        success: function (response) {
                   $("#replace").html(response);

        },



    });
    });
</script>

Upvotes: 0

Genina Anne Gabuten
Genina Anne Gabuten

Reputation: 278

Use this Autocomplete, this code I used in laravel 5.4

$('#search').on('input', function(){
    var search = $('#search').val(); //search input var

    if(search.length == 0){
        //do something if value of search box is empty
    } else {
        $('#search').autocomplete({
            source: function(request, response){
                $.ajax({
                    url: 'search', //route
                    data: { search:search },
                    dataType: 'json',
                    type: 'post',
                    success: function(data){
                        //here is your data
                    },
                    error: function(){
                        alert('Could not load the data');
                    }
                });
            }
        });
    }
});

Upvotes: -1

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

If I understood you correctly, you want to apply orWhere() pair for each word with foreach:

$words = explode(' ', $search);

$result = Product::query();

foreach ($words as $word) {
    $result = $result->orWhere('originalname', 'like', '%'.$word.'%')
                     ->orWhere('name', 'like', '%'.$word.'%');
}

$result = $result->orderby("products.table_id")->take(5)->get();

Upvotes: 3

Related Questions