doutuo6689 2015-06-16 14:51
浏览 8

仅当数据填入数据库时​​才使数据显示在页面上

I created a custom checkout/shopping cart system. I'm running into an issue though. Some of my products will have different options like colors or sizes. As of now, I only have one database table created called products with all of my information in it like product name, id, price, item details, description, etc. I display all of the products on one page and then if you click on the products link I grab that ID and show that products information.

Should I add a colors and size option to my products db or create new db tables?

Then how can I get these options to display ONLY if that product has different colors or sizes?

I'm adding products to my cart like this

<div class="addcartbox">
    <div class="productitemwrapper">
        Price: <span class="addtocartboxtotal"><?php echo "$" . $products[$product_id]['price']; ?></span>
        <div class="productdetailquantity"><?php echo"<form action='./itemsadded.php?view_product=$product_id' method='POST'>"?>
            <select class="productquantity" name='quantity'>
                <option value='1'>1</option>
                <option value='2'>2</option>
                <option value='3'>3</option>
                <option value='4'>4</option>
                <option value='5'>5</option>
                <option value='6'>6</option>
                <option value='7'>7</option>
                <option value='8'>8</option>
                <option value='9'>9</option>
                <option value='10'>10</option>
            </select>
        </div><br><br>

        //I want to add the color and size options here
        <div class="productdetailaddbutton">
        <?php 
            echo "<input type='hidden' name='product_id' value='$product_id' />
            <input type='submit' class='addtocart' name='add_to_cart' value='Add to cart' />";  
        ?>
        <br>
        <?php
            // Add Product to Cart
            if(isset($_POST['add_to_cart'])) {
                $product_id = $_POST['product_id'];

                // If item is already in cart, tell user
                if(isset($_SESSION['shopping_cart'][$product_id])) {
                    echo "Item already in cart!<br />";
                }
                // Otherwise add to cart
                else {
                    $_SESSION['shopping_cart'][$product_id]['product_id'] = $_POST['product_id'];
                    $_SESSION['shopping_cart'][$product_id]['quantity'] = $_POST['quantity'];
                    echo $message = "Added to cart!";
                }
            }
        ?>
        </form>
        </div>
    </div>
</div>

I want to add the color and size options in the location I commented in my code. I want to echo out the info from the db in option values. So this would mean I would have to have each color or size be a different row in the database table correct?

I'm not going to have many products, so this doesn't need scaled to a huge degree. I just more or less need this to work in the system I have set up, so I can continue displaying my products with a loop I created and only showing these fields if the product has different options with it.

UPDATE:

I based this on how I was displaying all of my products through a foreach loop. I'm not sure if I am even close...

<?php
//connection to db
$con = mysqli_connect("localhost", "root", "", "bfb"); 

//Check for errors  
if (mysqli_connect_errno()) {
    printf ("Connect failed: %s
", mysqli_connect_error());
    exit();
}

//Fetch products
    $products = array();
    $colors = mysqli_query($con,"SELECT * FROM colors");
    while($row = mysqli_fetch_array($colors)) {
        $products[$row['product_id']] = $row;
    }

// Loop to display all products
    foreach($colors as $id => $color) { ?>
<select name='colors'>
                <option value='1'><?php echo $color; ?></option>
</select>
<?php } ?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 linux驱动,linux应用,多线程
    • ¥20 我要一个分身加定位两个功能的安卓app
    • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
    • ¥15 IAR程序莫名变量多重定义
    • ¥15 (标签-UDP|关键词-client)
    • ¥15 关于库卡officelite无法与虚拟机通讯的问题
    • ¥15 目标检测项目无法读取视频
    • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
    • ¥100 求采集电商背景音乐的方法
    • ¥15 数学建模竞赛求指导帮助