2013-07-01 88 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