MySQL分离为DIV元素

时间:2014-04-17 16:49:17

标签: php html mysql

我非常乐意知道如何根据"季节"分离我的查询结果。仅使用PHP。产品应根据它们的季节放在不同的div中。我也想以最有效的方式做到这一点。我已经考虑了他一段时间,并且在解决方案和在互联网上找到其他类似的结果都失败了。请忽略jquery,它是我可能使用或不使用的东西的剩余部分。

    <?php
    session_start();
    include_once("config.php");
    ?>

    <!DOCTYPE html>
    <html>
    <head>
    <title>Shop</title>
    <link rel="stylesheet" type="text/css" href="style/main.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0       
    /jquery.min.js"></script>
    <script type="text/javascript" src="script/jquery.simplyscroll.min.js"></script>
    <script type="text/javascript">
    (function($) {
$(function() {
    $("#scroller").simplyScroll();
});
    })(jQuery);
    </script>
    </head>
    <body>

    <div id="container">

    <div id="header">
    <div id="menu">
    <ul>
    <li><a href="index.php">Home</a></li>
    <li><a href="about.php">About</a></li>
    <li><a href="shop.php">Shop</a></li>
    <li><a href="contact.php">Contact</a></li>
    </ul>
    </div>
    </div>

    <div id="content">

    <?php
    //current URL of the Page. cart_update.php redirects back to this URL
    $current_url = base64_encode("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

    //$results = $db->query('SELECT id, name, thumb, description, price, season FROM products 
    BY season ASC');

foreach($db->query('SELECT id, name, thumb, description, price FROM products BY season 
    ASC') as $results){

if ($results) { 
    //output results from database
    $last_season = 1;
    echo '<div class="products">';
    while($obj = $results->fetch_object())
    {
    if ($last_season != $obj->season){
    echo '</div><div class="products">';
    }
    echo '<div class="product">'; 
    echo '<form method="post" action="cart_update.php">';
    echo '<div class="product-thumb"><img src="image/'.$obj->product_img_name.'"></div>';
    echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
    echo '<div class="product-desc">'.$obj->product_desc.'</div>';
    echo '<div class="product-info">Price '.$currency.$obj->price.' <button 
    class="add_to_cart">Add To Cart</button></div>';
    echo '</div>';
    echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
    echo '<input type="hidden" name="type" value="add" />';
    echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
    echo '</form>';
    echo '</div>';

    $last_season = $obj->season;
    }
    echo '</div>';
}
}
    ?>

    <div id="shopping-cart">
    <h2>Your Shopping Cart</h2>
    <?php
    if(isset($_SESSION["products"]))
    {
    $total = 0;
    echo '<ol>';
    foreach ($_SESSION["products"] as $cart_itm)
    {
    echo '<li class="cart-itm">';
    echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&
    return_url='.$current_url.'">&times;</a></span>';
    echo '<h3>'.$cart_itm["name"].'</h3>';
    echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
    echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
    echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
    echo '</li>';
    $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
    $total = ($total + $subtotal);
    }
    echo '</ol>';
    echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a 
    href="../page/view_cart.php">Check-out!</a></span>';
    echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&
    return_url='.$current_url.'">Empty Cart</a></span>';
    }else{
    echo 'Your Cart is empty';
    }
    ?>
    </div>

    </div>

    <div id="footer">Footer goes here.</div>
    </div>

    </body>
    </html>

我在第44行收到错误,这是&#34; foreach&#34;声明。我又一次不知所措。到目前为止,谢谢你的帮助。如果我能够在工作的帮助下工作,我当然会非常兴奋并负债累累。

1 个答案:

答案 0 :(得分:0)

这是一个循环,每当季节在新行中变化时,它将创建一个新的div。您需要更改查询以按季节排序结果,例如

ORDER BY season ASC

这是循环:

if ($results) { 
    //output results from database

$last_season = 1; //initial value

echo '<div class="season">';  //opens first season div

 while($obj = $results->fetch_object()){

    if ($last_season != $obj->season){
        echo '</div><div class="season">';
    }

        echo '<div class="product">'; 
        echo '<form method="post" action="cart_update.php">';
        echo '<div class="product-thumb"><img src="image/'.$obj->product_img_name.'"></div>';
        echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
        echo '<div class="product-desc">'.$obj->product_desc.'</div>';
        echo '<div class="product-info">Price '.$currency.$obj->price.' <button class="add_to_cart">Add To Cart</button></div>';
        echo '</div>';
        echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
        echo '<input type="hidden" name="type" value="add" />';
        echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
        echo '</form>';
        echo '</div>';

        $last_season = $obj->season;
    }
    echo '</div>'; //closes final season div

}

关于foreach中的第44行错误,我相信你想要的不是foreach而只是:

$results = $db->query('SELECT id, name, thumb, description, price FROM products BY season 
ASC'); //remember to remove the closing bracket of the foreach