gpt4 book ai didi

php - MySQL 分隔为 DIV 元素

转载 作者:行者123 更新时间:2023-11-30 00:22:17 25 4
gpt4 key购买 nike

我非常想知道如何仅使用 PHP 根据“季节”来分离查询结果。产品应根据其适用季节放置在单独的分区中。我也想以最有效的方式做到这一点。我已经思考他的问题有一段时间了,但在解决方案和在互联网上找到其他人的类似结果时都失败了。请忽略 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 行收到错误,该行是“foreach”语句的开头。我又茫然了。非常感谢您迄今为止的帮助。如果我能在您的帮助下完成这项工作,我将非常兴奋,并感谢您。

最佳答案

这是一个循环,每次季节变化时都会创建一个新的 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

关于php - MySQL 分隔为 DIV 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23138968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com