Tuesday, April 11, 2017

how to php code of Search module convert into laravel controller

i am new in laravel and working on Search module. i made a code for it in custom PHP, Ajax, MySql but i want to convert this code in laravel. How could we make a controller for it. Can anyone please help me out for this?

<script type="text/javascript">
$(function(){
$(".search").keyup(function() 
{ 
var searchid = $(this).val();
var dataString = 'search='+ searchid;
if(searchid!=' ')
{
    $.ajax({
    type: "POST",
    url: "fetch.php",
    data: dataString,
    cache: false,
    success: function(html)
    {
    $("#result").html(html).show();
    }
    });
}return false;    
});
 
jQuery("#result").click(function(e){ 
    var $clicked = $(e.target);
    var $name = $clicked.find('.name').html();
    var decoded = $("<div/>").html($name).text();
    $('#searchid').val(decoded);
});
jQuery(document).click( function(e) { 
    var $clicked = $(e.target);
    if (! $clicked.hasClass("search")){
    jQuery("#result").fadeOut(); 
    }
});
$('#searchid').click(function(){
    jQuery("#result").fadeIn();
});
});
</script>
<div class="content">
        <input type="text" class="search" id="searchid" placeholder="Search for people" /> 
        <div id="result"></div>
</div>

PHP

$q = mysqli_real_escape_string($connection,$_POST['search']);
$strSQL_Result = mysqli_query($connection,"select firstName,lastName from users where firstName like '%$q%' or lastName like '%$q%' order by id LIMIT 5");

while($row=mysqli_fetch_array($strSQL_Result))
{
    $username   = $row['firstName'];
    $email      = $row['lastName'];
    $b_username = '<strong>'.$q.'</strong>';
    $b_email    = '<strong>'.$q.'</strong>';
    $final_username = str_ireplace($q, $b_username, $username);
    $final_email = str_ireplace($q, $b_email, $email);
    ?>
        <div class="show" align="left">
            <img src="dp.jpg" style="width:50px; height:50px; float:left; margin-right:6px;" /><span class="name"><?php echo $final_username; ?></span>&nbsp;<?php echo $final_email; ?><br/>
        </div>
    <?php
}



via Jamal Ahmad

Advertisement