我想将数据库中的数据拆分为三列表

时间:2013-02-07 15:21:31

标签: php mysql

我想将数据库中的数据拆分为三列表格,但我不知道怎么办我试过但是工作不正常请事先帮助我

 <?php
 $count=1;
$query1=mysql_query(" SELECT *,category.id ids FROM category INNER JOIN products ON  category.`cid`=products.`cid` WHERE category.id='$id' ") or die ('Product Query Problem');
    while($row1=mysql_fetch_array($query1))
    {
    $count++;
?>

我想将数据从数据库分成三列表格,但我不知道如何做到这一点我已经尝试过但是它不能正常工作请事先帮助我

 <div class="main_content">

<div class="featured-items clearfix">
<div class="items clearfix">
<table border="0">
<tr>
<td><div class="item-block-1">
<div class="image-wrapper">
<div class="image">
<div class="overlay">
<div class="position">
<div>
<p><?php echo $row1['description']; ?></p>
<a href="pandora-homepage.html#" class="quickshop">Quick shop</a>
</div>
</div>
</div>
<a href=""><img src="products/images/photos/photo-2.jpg" style="margin: -27.5px 0 0 0;" alt="" /></a>
</div>
</div>
<h2><a href=""><?php echo $row1['product']; ?></a></h2>
<p class="price"><?php echo $row1['price']; ?></p>
</div>
<?php
if($count%3==1)
{

    ?></td></tr>
  </table>
<?php   }}?>
</div>
</div>
</div>

2 个答案:

答案 0 :(得分:1)

您将找到一个从php here创建html表的非常明确的示例。我复制了一些相关的行:

if(mysql_num_rows($result2)) {
    echo '<table cellpadding="0" cellspacing="0" class="db-table">';
    echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default<th>Extra</th></tr>';
    while($row2 = mysql_fetch_row($result2)) {
       echo '<tr>';
       foreach($row2 as $key=>$value) {
           echo '<td>',$value,'</td>';
       }
       echo '</tr>';
    }
    echo '</table><br />';
}

它回显了表中的所有列 - 如果你想做一些不同的事情,你可以改变内部foreach循环。我希望这有帮助!

答案 1 :(得分:0)

试试这个

      <?php
  $count=1;
 ?>
 <div class="main_content">

<div class="featured-items clearfix">
<div class="items clearfix">
<table border="0">

<?
$query1=mysql_query(" SELECT *,category.id ids FROM category INNER JOIN products ON   category.`cid`=products.`cid` WHERE category.id='$id' ") or die ('Product Query Problem');
 while($row1=mysql_fetch_array($query1))
 {
 $count++;
 ?>

  <tr><td><div class="item-block-1">
  <div class="image-wrapper">
  <div class="image">
  <div class="overlay">
  <div class="position">
  <div>
   <p><?php echo $row1['description']; ?></p>
   <a href="pandora-homepage.html#" class="quickshop">Quick shop</a>
  </div>
  </div>
  </div>
 <a href=""><img src="products/images/photos/photo-2.jpg" style="margin: -27.5px 0 0 0;" alt="" /></a>
  </div>
  </div>
  <h2><a href=""><?php echo $row1['product']; ?></a></h2>
  <p class="price"><?php echo $row1['price']; ?></p>
  </div>
  <?php
   if($count%3==1)
    {

    ?></td></tr>
   <?php   }}?>
  </table>
  </div>
  </div>
  </div>