PHP - 卡住了每行不能有不同数量的购物车

时间:2016-10-08 10:31:22

标签: php html mysqli foreach session-variables

我希望能够制作一个可以在表格中的每一行上接受不同值的购物车但是它看起来像我在字段上写了一定数字它也会影响其他字段,是否有办法制作它接受不同的价值观?

cart.php

   <!DOCTYPE>

<?php
session_start();
include ("functions/functions.php");

?>

<html>
<head>

    <title>Deriada's Trading</title>
</head>

<link rel="stylesheet" href="styles/style.css" media="all" />

<body>
<!--Main Container starts here!
        ↓↓↓↓↓
-->
<div class="mainwrapper">

<!--Header starts here-->
    <div class="header_wrapper">

    <h1><span>Deriada's</span>Trading</h1>

    <a href="cart.php"><img class="imagedropshadow" id="imgcart" src="images/shopcart.png"></a>

    </div>
<!--Header ends here-->

<!--Menubar starts here-->
    <div class="menubar">

    <ul id="menu">

    <li><a class="home" href="index.php" >Home</a></li>
    <li><a href="AboutUs.php" >About Us</a></li>
    <li class="current_list_item"><a href="all_products.php">All Products</a></li>
    <li><a href="customer_register.php">Register</a></li>
    <li><a href="customer_login.php">Login</a></li>

    </ul>

    <div id="form">
    <form method="get" action="results.php" enctype="multipart/form-data">
    <input type="text" class="searchfrm" name="user_query" placeholder="Search for a product"/>
    <input  type="submit" class="searchbtn" name="search" value="Search" />

    </form>
    </div>

    </div>
<!--Menubar ends here-->

<!--Content starts here-->
    <div class="content_wrapper">

    <div id="sidebar">

    <div id="sidebar_title">Categories</div>

    <ul id="cats">

    <?php

    getCats();

    ?>

    </ul>



    </div>


    <?php cart(); ?>
    <div class="shopping_cart">

    <span style="float:right; font-size:18px; padding:5px; line-height:40px;">

                Welcome Guest! <b style="color:yellow">Shopping Cart -</b> Total Items: <?php total_items(); ?> Total Price: <?php total_price(); ?>



                </span>

    </div>

    <div id="content_area">


    <div id="products_box">

    <form action="" method="post" enctype="multipart/form-data">

        <table align="center" width="750">



            <tr align="center">
                <th>Remove</th>
                <th>Product(s)</th>
                <th>Quantity</th>
                <th>Total Price</th>
            </tr>

    <?php

    $total = 0;

    global $con;

    $ip = getIp();

    $sel_price = "select * from cart where ip_add='$ip'";

    $run_price = mysqli_query($con, $sel_price);

    while($p_price=mysqli_fetch_array($run_price)){

    $pro_id = $p_price['p_id'];

    $pro_price = "select * from products where prod_id = '$pro_id'";

    $run_pro_price = mysqli_query($con,$pro_price);

    while ($pp_price = mysqli_fetch_array($run_pro_price)){

        $product_price = array($pp_price['prod_price']);
        $product_title = $pp_price['prod_title'];
        $product_image = $pp_price['prod_image'];
        $single_price = $pp_price['prod_price'];


        $values = array_sum($product_price);

        $total += $values;



    ?>

            <tr align="center">
                <td width="150"><input type="checkbox" name="remove[]" value="<?php echo $pro_id;?>"/></td>
                <td width="150"><?php echo $product_title; ?><br>
                <img src="admin_area/product_images/<?php echo $prod_image; ?>" width="60px" height="60px">
                </td>
                <td width="150"><input type="text" size="4" name="qty"></td>

                <?php

                updatequantity();

                ?>

                <td width="150"><?php echo "₱" . $single_price ?></td>
            </tr>

            <?php }} ?>

            <tr align="right">
                <td colspan="5" style="
padding-right: 50;"><b>Sub Total:</b> <?php echo "₱" . $total?></td>
            </tr>

            <tr align="center">
                <td colspan="2" ><input type="submit" name="update_cart" value="Update Cart"/></td>
                <td><input type="submit" name="continue" value="Continue Shopping"/></td>
                <td><button><a href="checkout.php" style="text-decoration:none; color:black;">Checkout</a></button></td>
            </tr>




    </table>
    </form>

    <?php

    $total = 0;

    global $con; 

    $ip = getIp();

    if(isset($_POST['update_cart'])){

        foreach($_POST['remove'] as $remove_id){

        $delete_product = "delete from cart where p_id='$remove_id' AND ip_add='$ip'";

        $run_delete = mysqli_query($con, $delete_product); 

        if($run_delete){

        echo "<script>window.open('cart.php','_self')</script>";

        }

        }

    }
    if(isset($_POST['continue'])){

    echo "<script>window.open('index.php','_self')</script>";

    }

}
echo @$up_cart = updatecart();


    ?>

    </div>


</div>


    <div id="footer">&copy; 2016 Developed by <a href="#">JM Gresola</a>&nbsp;&nbsp;|&nbsp;&nbsp;Design by <a href="#" target="_blank" >Ron Majito</a> and <a href="#" target="_blank" >Marvin De Mesa</a></div>


</div>
<!--        ↑↑↑↑↑
    Main Container ends here!-->



</body> 
</html>

的functions.php

<?php

define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'ecommerce');

$con = mysqli_connect(DBHOST,DBUSER,DBPASS,DBNAME);

if(mysqli_connect_errno()){
echo "The connection was not established" . mysqli_connect_error();    
}

function getIp() {
$ip = $_SERVER['REMOTE_ADDR'];

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}

return $ip;
}

//creating the shopping cart
function cart(){

if(isset($_GET['add_cart'])){

    global $con;

    $ip = getIp();

    $pro_id = $_GET['add_cart'];

    $check_pro = "select * from cart where ip_add='$ip' AND p_id='$pro_id'";

    $run_check = mysqli_query($con, $check_pro);

    if(mysqli_num_rows($run_check)>0){
        echo "<script>alert('Product has already been added!')</script>";
    }
    else {

        $insert_pro = "insert into cart (p_id,ip_add) values ('$pro_id','$ip')";

        $run_pro = mysqli_query($con, $insert_pro);

        echo "<script>window.open('all_products.php','_self')</script>";

    }

}   

}
//getting the total added items

function total_items(){

if(isset($_GET['add_cart'])){

    global $con;

    $ip = getIp();

    $get_items = "select * from cart where ip_add='$ip'";

    $run_items = mysqli_query($con, $get_items);

    $count_items = mysqli_num_rows($run_items);

}

    else{

        global $con;

    $ip = getIp();

    $get_items = "select * from cart where ip_add='$ip'";

    $run_items = mysqli_query($con, $get_items);

    $count_items = mysqli_num_rows($run_items);

    }


    echo $count_items;
}

//getting the total price of the items in the cart

function total_price(){

$total = 0;

global $con;

$ip = getIp();

$sel_price = "select * from cart where ip_add='$ip'";

$run_price = mysqli_query($con, $sel_price);

while($p_price=mysqli_fetch_array($run_price)){

    $pro_id = $p_price['p_id'];

    $pro_price = "select * from products where prod_id = '$pro_id'";

    $run_pro_price = mysqli_query($con,$pro_price);

    while ($pp_price = mysqli_fetch_array($run_pro_price)){

        $product_price = array($pp_price['prod_price']);

        $values = array_sum($product_price);

        $total += $values;

    }

}

echo "₱" . $total;

}

//getting the categories

function getCats(){

global $con;

$get_cats = "select * FROM categories";

$run_cats = mysqli_query($con,$get_cats);

while($row_cats=mysqli_fetch_array($run_cats)){

    $cat_id = $row_cats['cat_id'];
    $cat_title = $row_cats['cat_title'];

echo "<li><a href='all_products.php?cat=$cat_id'>$cat_title</a></li>";

}

}



function getPro(){

if(!isset($_GET['cat'])){

global $con;

$get_pro = "select * from products";

$run_pro = mysqli_query($con, $get_pro);

while($row_pro = mysqli_fetch_array($run_pro)){

    $pro_id = $row_pro['prod_id'];
    $pro_title = $row_pro['prod_title'];
    $pro_cat = $row_pro['prod_cat'];
    $pro_qty = $row_pro['prod_qty'];
    $pro_price = $row_pro['prod_price'];
    $pro_image = $row_pro['prod_image'];

    echo "

    <div id='single_product'>

        <h4>$pro_title</h4>

                <img src='admin_area/product_images/$pro_image' width='180' height='180' />

                <p><b>Price: ₱ $pro_price </b></p>

                <a href='all_products.php?add_cart=$pro_id'><button  align='center'>Add to Cart</button></a>

    </div>

    ";

}

}

}

function getCatPro(){

if(isset($_GET['cat'])){

    $cat_id = $_GET['cat'];

global $con;

$get_cat_pro = "select * from products where prod_cat='$cat_id'";

$run_cat_pro = mysqli_query($con, $get_cat_pro);

$count_cats = mysqli_num_rows($run_cat_pro);

    if($count_cats==0){

    echo "<h2 style='padding:20px;'>There is no product in this category!</h2>";

    }

    while($row_cat_pro = mysqli_fetch_array($run_cat_pro)){

    $pro_id = $row_cat_pro['prod_id'];
    $pro_title = $row_cat_pro['prod_title'];
    $pro_cat = $row_cat_pro['prod_cat'];
    $pro_qty = $row_cat_pro['prod_qty'];
    $pro_price = $row_cat_pro['prod_price'];
    $pro_image = $row_cat_pro['prod_image'];

    echo "

    <div id='single_product'>

        <h4>$pro_title</h4>

                <img src='admin_area/product_images/$pro_image' width='180' height='180' />

                <p><b> ₱ $pro_price </b></p>

                <a href='all_products.php?add_cart=$pro_id'><button align='center'>Add to Cart</button></a>

    </div>



    ";



}

}
}


function updatequantity(){

global $con

if(isset($_POST['update_cart'])){

                        $qty = $_POST['qty'];

                        $update_qty = "update cart set qty='$qty'";

                        $run_qty = mysqli_query($con, $update_qty); 

                        $_SESSION['qty']=$qty;

                        $total = $total*$qty;
                    }
}

?>    

数据库结构

表:&#34;产品&#34;

'prod_id', INT(100), Primary Key
'prod_title' text
'prod_cat' text
'prod_qty' INT(100)
'prod_price' DECIMAL(65,2)
'prod_image' text

表:&#34;类别&#34;

'cat_id' INT(100),Primary Key
'cat_title' text

表:&#34;购物车&#34;

'p_id' INT(100), Primary Key
'iP_add' VARCHAR(255)
'p_qty' INT(100)

为什么我问?

它用于一个项目。我只是想知道是否可以制作一个允许每行的数量具有不同值的购物车。

修改

(1)我使用了一个文本框,以便用户输入他要订购的商品数量,是否有任何替代方法或方法可以使其更好?

0 个答案:

没有答案