Group JS and PHP Array

Asked

Viewed 209 times

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.

What I got so far: inserir a descrição da imagem aqui

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.

1 answer

0


I did it. But I don’t know if that can be considered a RTA or POG.

As you can see in the montage of array in PHP, I put in the key a p when it is product, concatenating with the id of the same. And when it is service I put the s concatenating with the id of the same.

But in the subtitle I did not put the p and neither s. So I thought, if I put the p and the s also in the subtitle I can do the sorting by key.

That’s what I did:

$arrProd = array('p0' => '<li class="subtitle">Produtos</li>');
$arrServ = array('s0' => '<li class="subtitle">Serviços</li>');

And then sorting using the function ksort who orders the array by keys:

ksort($arrProd);
ksort($arrServ);

return array_merge($arrProd, $arrServ);
  • What does RTA mean ?

  • @Gabrielrodrigues Alternative Technical Resource, aka gambiarra.

  • kkkkk is a euphemistic word for gambiarra, because POG is already emphatic in that :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.