我的mysql数据库中有各种产品的数据。防爆
ID Name price detail fact
1 test 20.00 text example
2 testing 30.00 apple orange
3 tester 5.43 plum grape
而且我有类似于下面的html代码,然后重复说大约30次不同的时间,因为mysql中有30种不同的产品数据
<!-- This is product 1 code -->
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="sample image 1" alt="" />
<h2>"price 1"</h2>
<p>"detail 1"</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
<div class="product-overlay">
<div class="overlay-content">
<h2>"price 1"</h2>
<h2>"ID"</h2>
<p>"detail 1"</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
</div>
</div>
<div class="choose">
<ul class="nav nav-pills nav-justified">
<li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
<li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
</ul>
</div>
</div>
<!-- This is product 2 code -->
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="sample image 2" alt="" />
<h2>"price 2"</h2>
<p>"Detail 2"</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
<div class="product-overlay">
<div class="overlay-content">
<h2>"price 2"</h2>
<p>"ID2"</p>
<p>"Detail 2"</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
</div>
</div>
<div class="choose">
<ul class="nav nav-pills nav-justified">
<li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
<li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
</ul>
</div>
</div>
我要完成的工作例如,在产品1代码中,输入“ price 1”将是数据库第一行中price的变量,“ detail 1”将是数据库中第一行的明细。依此类推,第四。然后,当我们获得产品2的代码时,请跳至第二行数据,并将匹配的数据插入其正确字段中。并重复此过程,直到数据库中的所有产品都与代码匹配为止。还要注意,并非像“事实”那样使用了mysql数据行中的所有字段。
我不确定如何做到这一点。
最佳答案
您需要通过查询MySQL数据库来获取数据。完成此操作后,您可以逐行遍历结果集并输出。
<?php
// Some code here to obtain MySQL data result set, like...
$result_set = mysqli_query( 'SELECT * FROM products_table_name WHERE 1 ORDER BY name ASC' );
$x = 1;
foreach( $result_set as $row ){
?>
<!-- This is product <?php echo($x); ?> code -->
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="sample image 1" alt="" />
<h2><?php echo $row['price']; ?></h2>
<p><?php echo $row['detail']; ?></p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
<div class="product-overlay">
<div class="overlay-content">
<h2><?php echo $row['price']; ?></h2>
<h2><?php echo $row['ID']; ?></h2>
<p><?php echo $row['detail']; ?></p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
</div>
</div>
<div class="choose">
<ul class="nav nav-pills nav-justified">
<li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
<li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
</ul>
</div>
</div>
<?php
$x++;
}