Grouping Array JS and PHP

0

I'm doing an information query on the Google Instant style database, where you type and the results below appear.

I have been able to do this, but I want these results to be grouped together. I'm doing the query in the Products and Services table and I want to first bring the product results and then the service results separated by a subhead.

What I've achieved so far:

Realizeabovethatitisbringingtheproductsandservicesalltogether.

Nowcomesthecode:

JS

//ResultadosdaPesquisa$('#q').on('keyup',function(){varstr=$(this).val();varres=$("#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 not think of a way to do it, I'm feeling rubbish !

    
asked by anonymous 28.01.2016 / 14:36

1 answer

0

I did it. But I do not know if this can be considered a RTA or POG .

As you can see in the assembly of array in PHP, I put in the key a p when it is product, concatenando con id of it. And when it is service I put s concatenando con id of it.

But in the subtitle I did not put p or s . So I thought: if I put p and s also in the subtitle I can do the sort 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 the ordering using the ksort function that orders the array for keys:

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

return array_merge($arrProd, $arrServ);
    
28.01.2016 / 14:52