Keep while information in variable

Asked

Viewed 225 times

-1

I have a code where you have a while within another, to be able to list products within the shopping cart divided by stores. Logo, to see listed the products, as in image 1. imagem 1

As in the picture above, my problem is the following when clicking buy I would need to have the id from the store I’m finalizing the purchase, only as I did this display with while it always saves the last id processed in while... I will put the code, I would have how I make that when I click buy it open the respective number the store?

while ($linhaaa = mysqli_fetch_array($resultadooo)) {

                        //cria variavel com o numero da loja    
                        $codigo_loja = $linhaaa["cod_loja"];
                        //seleciona tudo de compra com join de produtos_clientes com cod_user - essa parte vai exibir os produtos
                        $sintaxesql = "SELECT compra.cod_compra, compra.cod_user, compra.item, compra.tipo_entrega, compra.cod_loja, produtos_clientes.cod_colar, 
                        produtos_clientes.nome, produtos_clientes.preco, produtos_clientes.id_cliente, produtos_clientes.modalidade, produtos_clientes.cod_loja
                        FROM compra
                        JOIN produtos_clientes
                        ON compra.item = produtos_clientes.cod_colar && produtos_clientes.cod_loja = compra.cod_loja WHERE compra.cod_user='$cod_user' && compra.cod_loja='$codigo_loja'";
                        $resultado = mysqli_query($link, $sintaxesql);
                        //define a variavel de soma dos produtos
                        $total_pedido_soma = 0;
                        //abre while pra mostrar os itens que ele possui no carrinho                           
                        echo '<table class = "table table-striped">
                        <thead>
                            <tr>
                                <th scope = "col">Ações</th>
                                <th scope = "col">Nome do Vendedor</th>
                                <th scope = "col">Item</th>
                                <th scope = "col">Nome</th>
                                <th scope = "col">Preço</th>
                            </tr>
                        </thead>';
                        while ($linha = mysqli_fetch_array($resultado)) {
                            //calcula o total do pedido
                            $total_pedido_soma =
                            $total_pedido_soma = $total_pedido_soma + $linha["preco"];
                            //trata a exibição para monetária somente a linha não o resultado final
                            $linha["preco"] = str_replace(".", ",", $linha["preco"]);
                            //verifica o tipo de entrega
                            $tipo_entrega = $linha["tipo_entrega"];
                            //query para buscar nome do vendedor
                            $id_cliente = $linha["id_cliente"];
                            $sintaxesqll = "SELECT nome FROM usuario WHERE cod_user='$id_cliente'";
                            $resultadoo = mysqli_query($link, $sintaxesqll);
                            $linhaa = mysqli_fetch_array($resultadoo);
                            //exibe conforme o tipo de entrega                                                               
                            echo'<tbody><td>';
                            echo '    <a href="/controller/edita_colar_carrinho.php?cod=' . $linha["item"] . '&cod_funcao=2"><img class="si-glyph-pencil" id="borda" src="svg/svg/icons8-delete.png"/></a>

                                      ';
                            echo'</td>
                                <td>' . $linhaa["nome"] . '</td>
                                <td>' . $linha["item"] . '</td>
                                <td>' . $linha["nome"] . '</td>
                                <td>R$: ' . $linha["preco"] . '</td>
                                </tr>
                                </tbody>
                                ';
                        }                        
                        //resultado final
                        echo '<td>Total R$:';
                        echo $total_pedido_soma;
                        echo '<center><button class="btn btn-success" rel="5" onclick="submete();">Comprar</button></center>';
                        echo '</td>';
                        echo '</table>';                          
                        echo '<form id="envia" method="POST" action="envia.php">';
                        echo '<input type="hidden" name="total" value="'; print $codigo_loja; echo '" />'; 
                        echo'</form>';
                    }

2 answers

0

Just concatenate the value of the variable with the string of the 'Hidden' button being used, once concatenated into the string, no matter if you change the value of the variable in another loop.

echo '<input type="hidden" name="total" value="'.$codigo_loja.'" />';

To concatenate the value of the variable in the string with PHP just use '.', to learn more about string concatenation: http://php.net/manual/en/language.operators.string.php

  • hasn’t solved the problem yet...

0

Dude from what I understand your problem is that you can’t use the variable outside the while and it’s obvious if it was created inside the while it’s only going to work there, what you need to do is create the variable outside the secondary while, if you want to use outside the two whiles then create the variable as null out of the two, the logic is as follows: variable=null when going through the while it will receive the value and will come out of it with the value, and you will be able to use it outside, it would be something like this

<?php
while (10 < 14){
    $variavel1 = null;
    while (10 < 14){
        $variavel1 = $x['id'];
    }
   echo $variavel1;
}

it is obvious that you have to adapt to your code, but the logic is this

Browser other questions tagged

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