Jump to content
Fórum Script Brasil
  • 0

total carrinho


Clayton
 Share

Question

Bom dia estou com problemas com o carrinho ,se eu colocar $preço =$linha['preço']; exibe o valor corretamente,mas se eu colocar o number_format abaixo, ele zera o resultado:

  $preço = number_format((float) $linha['preço'] * $qtd, 2, ',', '.');

E o total dos produtos não  está sendo feito, me disseram que tem que usar javascript , mas eu não conheço javascript, alguém pode ajudar?

 

<?php
include(dirname(__FILE__) . './funcao/conecta.php');
session_start();

if (!isset($_SESSION['shop'])) {
    $_SESSION['shop'] = array();
}

if (isset($_GET['acao'])) { // adicionar produto
    $id = isset($_GET['id']) ? intval($_GET['id']) : null;
    if ($_GET['acao'] == 'add') {
        if (!isset($_SESSION['shop'][$id])) {
            $_SESSION['shop'][$id] = 1;
        }
    } elseif ($_GET['acao'] == 'del') {  //REMOVER shop
        if (isset($_SESSION['shop'][$id])) {
            unset($_SESSION['shop'][$id]);
        }
    }

//ALTERAR QUANTIDADE
    //Se existir $_POST['prod'] então começa..
    if (isset($_POST['prod']))
        if ($_GET['acao'] == 'atualizar') {
            if (is_array($_POST['prod'])) {
                foreach ($_POST['prod'] as $id => $qtd) {
                    $id = intval($id);
                    $qtd = intval($qtd);
                    if (!empty($qtd) || $qtd <> 0) {
                        $_SESSION['shop'][$id] = $qtd;
                    } else {
                        unset($_SESSION['shop'][$id]);
                    }
                }
            }
        }
}
?>
<!doctype html>
<html>
    <head>
        <meta charset='utf8'>
        <title>Seja Bem Vindo!</title>
        <link rel="stylesheet" href="css/default.css">
    </head>
    <body>
        <div><h2>Suas Compras Efetuadas:</h2>
            <br>
            <table border="1" width="100%">
                <thead>
                    <tr>
                        <th width="10%">FOTO</th>
                        <th width="10%">NOME</th>
                        <th width="10%">DESCRIÇÃO</th>
                        <th width="10%">TAMANHO</th>
                        <th width="10%">COR</th>
                        <th width="10%">PREÇO</th>
                        <th width="10%">QTD.</th>
                        <th width="10%">SUBTOTAL</th>
                        <th width="10%">REMOVER</th>
                    </tr>
                </thead>
                <tr><td colspan="9">
                        <form action="?acao=atualizar" method="post">
                            <table><tfoot>
                                    <tr>
                                        <td><input type="submit" value="Atualizar"></td>
                                    </tr><tr>
                                        <td><a href="./index.php">Continuar Comprando</a></td>
                                    </tr><tr>
                                        <td><a href="./admin/finalizar.php">Finalizar Pedido</a></td>
                                </tfoot></table>
                        </form>
                    </td></tr>
                <tbody>
                    <?php
                    $total = 0;
                    if (count($_SESSION['shop']) == 0) {
                        echo '<tr><td colspan="9"><div class="#">O cesto de compras esta vazio!</td></tr>';
                    } else {
                        $conn = new PDO("mysql:host=localhost;dbname=loja", "root", "");
                        foreach ($_SESSION['shop'] as $id => $qtd) {
                            $cart = $conn->prepare("SELECT * FROM produtos WHERE  id=$id");
                            $cart->setFetchMode(PDO::FETCH_ASSOC);
                            $cart->execute();
                            while ($linha = $cart->fetch()) {
                                $foto = $linha['foto'];
                                $nome = $linha['nome'];
                                $descricao = $linha['descricao'];
                                $tamanho = $linha['tamanho'];
                                $cor = $linha['cor'];
                                



                              $preço = number_format((float) $linha['preço'] * $qtd, 2, ',', '.');
                              $sub = number_format((float) $linha['preço'] * $qtd, 2, ',', '.');
                                $sub =  $linha['preço'] * $qtd;
                                $total += $preço;
                                $total = number_format($total, 2, ',', '.');
                                echo '<tr>
                                             <td><img src = "' . $foto . ' " width = "100px"</td>
                                             <td>' . $nome . '</td>
                                             <td>' . $descricao . '</td>
                                             <td>' . $tamanho . '</td>
                                             <td>' . $cor . '</td>

                                             <td>R$ ' . $preço . '</td>
                                             <td><input type="text" size="3" name="prod[' . $id . ']" value="' . $qtd . '" /></td>
                                             <td>R$ ' . $sub . '</td>
                                             <td colspan = "9"><a href="?acao=del&id=' . $id . '"><img src="./imagens/remover.png" width="70" height="70" ></td>
                                             </tr>';
                            }
                        }
                    }
                    ?>
            </table>
        </div>
    </body>
</html>
 

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Forum Statistics

    • Total Topics
      149.6k
    • Total Posts
      646.2k
×
×
  • Create New...