0

我有一个用于展示我的产品的 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>
4

1 回答 1

0

以下代码应该可以满足您的需求(如果我理解正确的话)...只需将 的内容替换为$cell您上面的代码即可echo

$counter = 1;

echo "<tr>";
$started = 0;
while($row = mysqli_fetch_array($yourArray)){

    $cell = "Put the code you were echoing above in here";

    if($counter%3 == 0 && $started == 1){
        echo $cell, "</tr><tr>";
    }
    else{
        echo $cell;
    }
    $started = 1;
    $counter++;
}
echo "</tr>";
于 2013-07-13T20:15:17.340 回答