我有一个用于展示我的产品的 html 表格,并且我有以下代码:
<?php
$result = mysqli_query($con,"SELECT * FROM products");
while($row = mysqli_fetch_array($result))
{
echo '<td>
<!--START OF PRODUCT-->
<div>
<h3>'.$row['productname'] . '</h3><img src="'.$row['thumb']. '" width="100%">
<div>
<div class="panel">
<h5>From £' . $row['price']. '</h5>
<div class="row">
<div class="large-12 columns">
<p>'. $row['shortdesc']. '</p>
<div class="row">
<div class="large-12 columns">
</div>
<form name="sign" action="http://www.shapeways.com/cart/addMultiple" method="GET" class="custom">
<input type="hidden" name="model_id[]" value="'. $row['id']. '">
<label for="customDropdown1">Material/Colour</label>
<select name="material_id[]" id="customDropdown1" class="medium">'.$row['materials']. '</select>
<div class="row">
<div class="large-6 columns">
<a href="#'. $row['id']. '" class="button secondary">Information</a>
</div>
<div class="row">
<div class="large-6 columns">
<input type="submit" class="button" name="submit" id="add_to_cart" alt="add to cart" value="Buy Now">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
';
}
mysqli_close($con);
?>
我希望它显示数据库中的 3 行,然后当它选择下 3 行以使其位于 html 表的下一行时。并重复此操作。
我希望这很清楚,一些帮助会很好。
第一个答案的代码:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
$result = mysqli_query($con,"SELECT * FROM products");
echo "<tr>";
$start = 0;
while($row = mysqli_fetch_array($result)){
$cell = '<td>
<!--START OF PRODUCT-->
<div>
<h3>'.$row['productname'] . '</h3><img src="'.$row['thumb']. '" width="100%">
<div>
<div class="panel">
<h5>From £' . $row['price']. '</h5>
<div class="row">
<div class="large-12 columns">
<p>'. $row['shortdesc']. '</p>
<div class="row">
<div class="large-12 columns">
</div>
<form name="sign" action="http://www.shapeways.com/cart/addMultiple" method="GET" class="custom">
<input type="hidden" name="model_id[]" value="'. $row['id']. '">
<label for="customDropdown1">Material/Colour</label>
<select name="material_id[]" id="customDropdown1" class="medium">'.$row['materials']. '</select>
<div class="row">
<div class="large-6 columns">
<a href="#'. $row['id']. '" class="button secondary">Information</a>
</div>
<div class="row">
<div class="large-6 columns">
<input type="submit" class="button" name="submit" id="add_to_cart" alt="add to cart" value="Buy Now">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</td>';
if($counter%3 == 0 && $start == 1){
echo "</tr><tr>", $cell;
}
else{
echo $cell;
}
$started = 1;
$counter++;
}
echo "</tr>";
mysqli_close($con);
?>
</table>