2013-07-01 55 views
0

我在MySQL表中有35条状态名记录。我划分了数据转换成两列 这里是要求将一列数据划分为四列

<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> 

我的PHP代码现在按我的新的要求,我想这个分成4列可有人建议我 如何实现

+0

试试'modulo''%'操作符 – swapnesh

+0

可以在代码中详细说明吗? – Mac

回答

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++; 
} 
+0

谢谢你这么多。它真的帮了我很多 – Mac

+0

欢迎。请不要忘记接受答案 – Wishnu

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> 
+0

为什么最后3行是开放的.. ???你能否重新检查并更新代码... – Mac

+0

你是什么意思?最后3行将结束最后一个表格。只需尝试一下代码并告诉我它是否无效 – drarkayl