0
I’m doing an information query in the database like Google Instant, where you type and already appears the results below.
I managed to do this, but I want these results to come together. I’m doing the query in the table of Products and Services and I want to bring first the results of products and then services separated by a subtitle.
Notice above that he is bringing the products and services all together.
Now comes the code:
JS
// Resultados da Pesquisa
$('#q').on('keyup', function(){
var str = $(this).val();
var res = $("#resultados");
var row = $("#resultados > ul");
$.ajax({
url: urlBase + '/consulta',
type: "POST",
cache: false,
async: false,
data: { str: str },
success: function(result){
res.css('display', 'block');
row.html('');
if(str != ''){
$.each(result, function(index, value){
if(value == 'Produtos'){
row.append('<li class="subtitle">Produtos</li>');
}
else if(value == 'Serviços'){
row.append('<li class="subtitle">Serviços</li>');
}
else
row.append('<li>'+value+'</li>');
});
}
else{
str = '';
res.css('display', 'none');
}
}
});
});
PHP
# Consulta no Site
public function postConsulta(){
$str = Input::get('str');
$arrProd = array(0 => 'Produtos');
$arrServ = array(0 => 'Serviços');
$produtos = DB::table('produtos')
->join('categorias', 'categorias.id', '=', 'produtos.id_categoria')
->select('categorias.slug as slug_categoria', 'produtos.slug as slug', 'produtos.id', 'produto')
->where('produto', 'like', "%$str%")
->take(5)
->get();
$servicos = Servico::where('servico', 'like', "%$str%")->get();
foreach ($produtos as $value) {
$arrProd['p'.$value->id] = "<a href='".URL::to('produtos/'.$value->slug_categoria.'/'.$value->slug)."'>".$value->produto."</a>";
}
foreach ($servicos as $value) {
$arrServ['s'.$value['id']] = "<a href='".URL::to('servicos/'.$value->slug)."'>".$value['servico']."</a>";
}
return array_merge($arrProd, $arrServ);
}
I want you to bring it like this:
• Produtos
JFL SHC 3.0 PA
JFL RRC 500 (5 CANAIS)
JFL RRC 400 (4 CANAIS)
JFL RRC 300 (3 CANAIS)
JFL RRC 200 (2 CANAIS)
• Serviços
Segurança Patrimonial
Monitoramento 24 Horas
Portaria e Recepção
Segurança Armada e Desarmada
I can’t think of a way to do it, I’m feeling a dump!
It didn’t work. It doesn’t even show the results. I think the problem is at the time of
merge
in PHP, I thought that using this function it would join, but it would follow the code.– Diego Souza