0

我在 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 列,任何人都可以建议我如何实现它

4

2 回答 2

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++;
}
于 2013-07-01T07:25:03.510 回答
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>
于 2013-07-01T07:37:14.853 回答