add values to an array in php and play the result in a dynamic table

Asked

Viewed 35 times

0

Good morning Devs, I have a problem that I can not see the solution... Let me explain I have a system here at the company that checks the value of my product with the competitor’s product, a boy does the search via bar Cod going in the competitor’s physical store with an app to scan the bar Cod and send this data to our database, with this it is possible to check with the bar Cod if this product exists in my store, so seeing the margin of the competitor and etc. the system this ok, example on the 11th the employee was in Guanabara and recorded several items for the survey of the 11th in the competitor Gwuanabara, he recorded by cell phone the products chocolate Estle, biscuit trakinas and rice brazil 500g, and then he went to do a search in Carrefour, then he went to the Uncle John market and so on, the system was developed until then to verify which researches were made on the X day, ex:day 11 had Guanabara, Carrefour and uncle John, by clicking on the Google query it generates an equal dynamic table below the system:

similar Cod product bar product value my company concurrent value
not 00000000001 chocolate Estle 4,50 4.00
yes 00000000002 biscuit trakinas similar 5.00
not 00000000003 brazil rice 500g 40,00 6,00

the system in php generates me a dynamic table with the products researched and registered on the specific day, until then everything well working with the code below:

VIEW:

                    <thead>
                        <tr>
                            <th>Similar</th>
                            <th>Código de barras</th>
                            <th>produto</th>
                            <th>valor minha empresa</th>
                            <th>valor concorrente</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($newProdutos as $produto)
                        <tr>
                            <td>{{ $produto->similarProduto == 0 ? "Não" : "Sim"}}</td>
                            <td>{{ $produto->codbarProduto}}</td>
                            <td>{{ $produto->descricacap }}</td>
                            <td>{{ $produto->valorminhaempresa}}</td>
                            <td>{{ $produto->valorconcorrente}}</td>
                        </tr>
                        @endforeach
                    </tbody>

BACKEND:

public Function show($codPesquisa){

    $query = [
        ['pes.codPesquisa', $codPesquisa],
    ];        
    
    //AQUI EU PEGO TODOS OS PRODUTOS PESQUISADOS NO CORRENTE PELO DIA SELECIONADO
    $produtos = DB::connection('pesquisas')
        ->table('produtos')
        ->where($query)
        ->get();

    $newProdutos = [];

    foreach($produtos as $produto) {
        $newProduto = new stdClass();

        $minhalojaProduto = DB::selectOne("
             //// AQUI EU FACO O SELECT PARA VER SE OS PRODUTOS
         DA PESQUISA EXISTEM NA MINHA LOJA, OS QUE NAO EXISTIREM 
         SAO MARCADOS COMO SIMILARES,
        ");

        $newProduto->concorrente = $produto->descConcorrente;
        $newProduto->descProduto = $produto->descProduto;
        $newProduto->codbarProduto = $produto->codbarProduto;
        $newProduto->precoConcorrente = $produto->valorProduto;
        
        if (intval($produto->similarProduto) === 0) {
            $newProduto->valorminhaempresa = floatval($$minhalojaProduto->PRECO);
            //outras funcoes existem para ver lucro margem etc
        }
        
        array_push($newProdutos, $newProduto);
    }

    return view('compras::pages.pesquisa.show-produtos', compact('newProdutos'));
}

my doubt is the following the owner he now wants the search to be no more than 1 p 1, he wants to take all the searches of the day and play on the same screen example:

day Cod product bar product value my company guanab value carref value etc.
11 00000000001 chocolate Estle 4,50 4.00 8,00 ...
11 00000000002 biscuit trakinas similar 5.00 7,00 ...
11 00000000003 brazil rice 500g 40,00 6,00 --- ...

It’s going to look something like this, does anyone have any idea how to do that? I can put all the searches inside a single array, and even make the comparison more or less , I was trying to see if it flowed but I got stuck... my Cod below:

public Function showTodas (Request $request, $codPesquisa){

    //aqui eu pego o cod de uma pesquisa realizada do dia selecionado
    $pesquisa = Pesquisa::find($codPesquisa);

    //aqui eu pego a data dessa pesquisa
    $dataPesquisa = $pesquisa->dataPesquisa;

    //aqui eu busco todas as pesquisas realizadas no dia
    $pesquisas = Pesquisa::where('dataPesquisa',$dataPesquisa)->get();
    $produtos = [];        
    //AQUI EU PEGOS TODAS AS PESQUISAS REALIZADAS NO DIA E JOGO EM $produtos
    for($i=0; $i<$pesquisas->count(); $i++) {
        $query = [
            ['pes.codPesquisa', $codPesquisa + $i],
        ];

       $produtos[$i] = DB::connection('pesquisas')
        ->table('produtos')
        ->where($query)
        ->get();
    }

 //AQUI EU CRIO UM ARRAY COM O PRODUTOS DE TODAS AS PESQUISAS DO DIA
   $todos = [];
    foreach($produtos as $produto){
        foreach($produto as $prod){
            array_push($todos, $prod);
        
        }            
    }

I stopped here, like if in this array there is the same bar code I would like to join them in the same position of the array, how do I do it with PHP, Laravel ? I researched some native php array functions but could not see anything that would help me .. I tried to put as much detail as possible of this problem that I am having, if there is something without understanding I can explain, since already Gradeco qq helps

No answers

Browser other questions tagged

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