将一列数据分成四列

时间:2013-07-01 06:58:27

标签: php

我在mysql表中有35条statename记录。我已将数据分为两列 这是我的需求的PHP代码

<td>
<table class="statetable">
<?
//******Fetching State Name Dynamically*****//
$fetchstate = mysql_query("SELECT LocationName FROM servicedesklocationmaster group by      LocationName ASC");
$half1   = floor(mysql_num_rows($fetchstate)/2);
echo $half1;
$count  = 0;
// First Half State
while($count <= $half1 && $row = mysql_fetch_array($fetchstate))
{
$statename = $row['LocationName'];
$count++;
?>
<tr>
<td>                    
<font size="1.5">
<input type="checkbox" name="Location[]" id="Location"  checked value="<?php echo     $statename;?>" onClick="CheckEachAsset()"><?php echo $statename;?><br>
</font>
</td>
<?
}
//echo $count;
?>
</tr>
</table>
</td>
<td>
<table class="statetable">
<?
// Second Half State
while($row = mysql_fetch_array($fetchstate))
{
$statename = $row['LocationName'];
?>
<tr>
<td>
<font size="1.5">
<input type="checkbox" name="Location[]" id="Location"  
checked value="<?php  echo  $statename;?>" onClick="CheckEachAsset()"><?php echo            $statename;?><br>
</font>
</td>
<?
}
?>
</tr>
</table>
</td>

现在根据我的新要求,我想把它分成4列,任何人都可以建议我 如何实现它

2 个答案:

答案 0 :(得分:1)

$quater = floor(mysql_num_rows($fetchstate)/4);
$count = 0;
while($row = mysql_fetch_array($fetchstate)) {
    if($count < $quater) {
     // echo table content1
    }
    if($count >= $quater &&  $count < $quater*2) {
      // echo table content2
    }
    if($count >= $quater*2 &&  $count < $quater*3) {
      // echo table content3
    }
    if($count >= $quater*3 &&  $count < $quater*4) {
     // echo table content4
    }
    $count++;
}

答案 1 :(得分:0)

 <td>
    <table class="statetable">
    <?

    $fetchstate = mysql_query("SELECT LocationName FROM servicedesklocationmaster group by          LocationName ASC");
    $quart= floor(mysql_num_rows($fetchstate)/4);
    echo $quart;
    $count  = 0;
    $times=0;

    while($times<=4 && $row = mysql_fetch_array($fetchstate))
    {
    $statename = $row['LocationName'];
    $count++;
    $times++;
    ?>
    <tr>
    <td>                    
    <font size="1.5">
    <input type="checkbox" name="Location[]" id="Location"  checked value="<?php echo     $statename;?>" onClick="CheckEachAsset()"><?php echo $statename;?><br>
    </font>
    </td>
    <?
    if($count==$quart && $times!=4){
    $count=0;
    ?>

    </tr>
    </table>
    </td>
    <td>
    <table class="statetable">
<?

}
    }
    //echo $count;
    ?>

    </tr>
    </table>
    </td>