我试图遍历sql中的每个项目(有4个),但是;它只返回一次迭代。应该注意的是,所有这些信息都在我的PHP文件中以垂直顺序
javascript
func indexHandler(w http.ResponseWriter, r *http.Request, title string) {
p := &Page{Title: title}
err := templates.ExecuteTemplate(w, "index.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
HTML
<script>
function showDiv() {
if($("#hiddenDiv" ).css('display') == 'none') {
$("#hiddenDiv" ).show();
}else {
$("#hiddenDiv" ).hide();
}
}
</script>
PHP
<div class="panel-heading">
<span>Checkout</span>
<button onclick="showDiv()" class="btn btn-warning pull-right">
<span class="glyphicon glyphicon-chevron-down"></span>
</button>
</div>
答案 0 :(得分:0)
当你点击showDiv按钮时,它将只显示一个id为hiddenDiv的div,其余的将被隐藏。所以将hiddenDiv添加为类并在脚本中进行相应的更改
<div class="col-md-3 hiddenDiv" style="display:none;">
<script>
function showDiv() {
if($(".hiddenDiv" ).css('display') == 'none') {
$(".hiddenDiv" ).show();
}else {
$(".hiddenDiv" ).hide();
}
}
</script>
答案 1 :(得分:0)
由于返回了多行,您需要更改&#34; hiddenDiv&#34;从id到类或在循环外创建隐藏的div
<div id="hiddenDiv" style="display:none;">
<?php
$query = "SELECT * FROM products ORDER BY id ASC";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<div class="col-md-3" >
<form method="post" action="shop.php?action=add&id=<?php echo $row['id']; ?>">
<div style="border: 1px solid #eaeaec; margin: -1px 19px 3px -1px; box-shadow: 0 1px 2px rgba(0,0,0,0.05); padding:10px;" align="center">
<?php echo "<img src=\"imeg/". $row["image"] . "\" " . "class=\"img-responsive\">"; ?>
<h5 class="text-info"><?php echo $row["p_name"]; ?></h5>
<h5 class="text-danger">$ <?php echo $row["price"]; ?></h5>
<input type="text" name="quantity" class="form-control" value="1">
<input type="hidden" name="hidden_name" value="<?php echo $row["p_name"]; ?>">
<input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>">
<input type="submit" name="add" style="margin-top:5px;" class="btn btn-default" value="Add to Cart">
</div>
</form>
</div>
<?php
}
}
?>
</div>
<script>
function showDiv() {
$("#hiddenDiv" ).slideToggle();
}
</script>