如何使用 php 根据 mysql table 中的数据自动创建 div?
How to auto create divs depending on the data in mysql table using php?
我想创建一个电子商务网站,使用 PHP 和 mySQL 数据库。我在下面有这段代码,我想(如果可能的话使用 php),在连接到 sql 数据库(我这样做了)之后,根据DB table 并回显 table 中每个条目的数据。我找到了这个,但我不知道它是否是正确的示例:Automatically creating div's using data from a sql table
代码如下:
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
<div class="hs-wrapper hs-wrapper2">
<img src="images/<image>.jpg" alt=" " class="img-responsive" />
<div class="hs_bottom hs_bottom_sub1">
<ul>
<li>
<a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
</li>
</ul>
</div>
</div>
<h5><a href="#"><Itemname></a></h5>
<div class="simpleCart_shelfItem">
<p><i class="item_price"><Price></i></p>
<p><i class="item_price"><Description></i></p>
<form action="#" method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="item" value="Itemname" />
<input type="hidden" name="amount" value="Price"/>
<button type="submit" class="cart">Add to cart</button>
</form>
</div>
</div>
您上面提供的示例可以正常工作,您需要做的就是将要生成多次的div/div放在一个循环中。那么只要满足循环条件就会生成。
But though the example u provided works but you need to note that it
is using mysql_*
functions which are depreciated and no longer
supported by newer php versions .
因此,我建议您改为了解准备好的语句,并使用 PDO。
考虑以下几点:
<?php
$sql = $pdo->query("SELECT * FROM products")->fetchall(PDO::FETCH_ASSOC);
foreach($sql as $row):?>
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
<div class="hs-wrapper hs-wrapper2">
<img src="images/<?php echo $row['productImg'];?>" alt=" " class="img-responsive" />
<div class="hs_bottom hs_bottom_sub1">
<ul>
<li>
<a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
</li>
</ul>
</div>
</div>
<h5><a href="#"><?php echo $row['item_name'];?></a></h5>
<div class="simpleCart_shelfItem">
<p><i class="item_price"> Price : <?php echo $row['item_price'];?></i></p>
<p><i class="item_price">Description <?php echo $row['item_description'];?></i></p>
<form action="#" method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="item" value="<?php echo $row['item_name'];?>" />
<input type="hidden" name="amount" value="<?php echo $row['item_price'];?>"/>
<button type="submit" class="cart">Add to cart</button>
</form>
</div>
</div>
<?php
endforeach;
?>
或
<?php
$sql = $pdo->query("SELECT * FROM products");
while ($row = $sql->fetchall(PDO::FETCH_ASSOC)):?>
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
<div class="hs-wrapper hs-wrapper2">
<img src="images/<?php echo $row['productImg'];?>" alt=" " class="img-responsive" />
<div class="hs_bottom hs_bottom_sub1">
<ul>
<li>
<a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
</li>
</ul>
</div>
</div>
<h5><a href="#"><?php echo $row['item_name'];?></a></h5>
<div class="simpleCart_shelfItem">
<p><i class="item_price"> Price : <?php echo $row['item_price'];?></i></p>
<p><i class="item_price">Description <?php echo $row['item_description'];?></i></p>
<form action="#" method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="item" value="<?php echo $row['item_name'];?>" />
<input type="hidden" name="amount" value="<?php echo $row['item_price'];?>"/>
<button type="submit" class="cart">Add to cart</button>
</form>
</div>
</div>
<?php
endwhile;
?>
其中 $pdo
是您的数据库连接字符串。
这将获取数据并为每个产品生成 div。
如果你的应用程序没有使用准备好的语句,我建议你多使用它们,尤其是 pdo 准备好的语句,下面是你可以学习 pdo 准备好的语句的地方
我想创建一个电子商务网站,使用 PHP 和 mySQL 数据库。我在下面有这段代码,我想(如果可能的话使用 php),在连接到 sql 数据库(我这样做了)之后,根据DB table 并回显 table 中每个条目的数据。我找到了这个,但我不知道它是否是正确的示例:Automatically creating div's using data from a sql table
代码如下:
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
<div class="hs-wrapper hs-wrapper2">
<img src="images/<image>.jpg" alt=" " class="img-responsive" />
<div class="hs_bottom hs_bottom_sub1">
<ul>
<li>
<a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
</li>
</ul>
</div>
</div>
<h5><a href="#"><Itemname></a></h5>
<div class="simpleCart_shelfItem">
<p><i class="item_price"><Price></i></p>
<p><i class="item_price"><Description></i></p>
<form action="#" method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="item" value="Itemname" />
<input type="hidden" name="amount" value="Price"/>
<button type="submit" class="cart">Add to cart</button>
</form>
</div>
</div>
您上面提供的示例可以正常工作,您需要做的就是将要生成多次的div/div放在一个循环中。那么只要满足循环条件就会生成。
But though the example u provided works but you need to note that it is using
mysql_*
functions which are depreciated and no longer supported by newer php versions .
因此,我建议您改为了解准备好的语句,并使用 PDO。
考虑以下几点:
<?php
$sql = $pdo->query("SELECT * FROM products")->fetchall(PDO::FETCH_ASSOC);
foreach($sql as $row):?>
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
<div class="hs-wrapper hs-wrapper2">
<img src="images/<?php echo $row['productImg'];?>" alt=" " class="img-responsive" />
<div class="hs_bottom hs_bottom_sub1">
<ul>
<li>
<a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
</li>
</ul>
</div>
</div>
<h5><a href="#"><?php echo $row['item_name'];?></a></h5>
<div class="simpleCart_shelfItem">
<p><i class="item_price"> Price : <?php echo $row['item_price'];?></i></p>
<p><i class="item_price">Description <?php echo $row['item_description'];?></i></p>
<form action="#" method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="item" value="<?php echo $row['item_name'];?>" />
<input type="hidden" name="amount" value="<?php echo $row['item_price'];?>"/>
<button type="submit" class="cart">Add to cart</button>
</form>
</div>
</div>
<?php
endforeach;
?>
或
<?php
$sql = $pdo->query("SELECT * FROM products");
while ($row = $sql->fetchall(PDO::FETCH_ASSOC)):?>
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
<div class="hs-wrapper hs-wrapper2">
<img src="images/<?php echo $row['productImg'];?>" alt=" " class="img-responsive" />
<div class="hs_bottom hs_bottom_sub1">
<ul>
<li>
<a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
</li>
</ul>
</div>
</div>
<h5><a href="#"><?php echo $row['item_name'];?></a></h5>
<div class="simpleCart_shelfItem">
<p><i class="item_price"> Price : <?php echo $row['item_price'];?></i></p>
<p><i class="item_price">Description <?php echo $row['item_description'];?></i></p>
<form action="#" method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="item" value="<?php echo $row['item_name'];?>" />
<input type="hidden" name="amount" value="<?php echo $row['item_price'];?>"/>
<button type="submit" class="cart">Add to cart</button>
</form>
</div>
</div>
<?php
endwhile;
?>
其中 $pdo
是您的数据库连接字符串。
这将获取数据并为每个产品生成 div。
如果你的应用程序没有使用准备好的语句,我建议你多使用它们,尤其是 pdo 准备好的语句,下面是你可以学习 pdo 准备好的语句的地方