2011-10-22 24 views
0

我收到了我的數據庫中的總線號碼列表。此處示例:在循環中捕捉新的百位號碼

route_id,route_short_name,route_long_name,route_type,route_color,route_text_color 
    1,1,Victoria/Churchill,3,A32638,FFFFFF 
    2,2,Tiffin/St-Georges,3,A32638,FFFFFF 
    99,99,Saint-Bruno,3,A32638,FFFFFF 
    100,100,Ile-Des-Soeurs,3,A32638,FFFFFF 
    106,106,Secteur B/Victoria,3,A32638,FFFFFF 
    199,199,Seigneurial/Grand Boulevard,3,A32638,FFFFFF 
    818,T18,Taxi - Aéroport,3,A32638,FFFFFF 
    893,T93,Taxi - De Mortagne - Du Boisé - Ch. De Touraine,3,A32638,FFFFFF 

我們將使用route_id(第一列)。

這是我的問題。我怎樣才能抓住一個新的百位?從上面的預期結果將是

1 to 99 
    100 to 199 
    818 to 893 

我幾乎總是找到了答案,我的問題,但這個時候..我真的不知道。我這樣做,但這不是很好:

# Creating the array 
$Routes = array(); 
$Quick = array(); 

# Array launching 
$F_D = -1; 
$i=0; 
while($Assoc_Routes = mysql_fetch_assoc($Query_Routes)){ 
    # Array 
    $Routes[] = $Assoc_Routes; 

    $Digit_Length = strlen($Assoc_Routes['route_short_name']); 
    switch($Digit_Length){ 
     case 1 : $Digit = '00'.$Assoc_Routes['route_id'][0]; break; 
     case 2 : $Digit = '0'.$Assoc_Routes['route_id'][0]; break; 
     default: $Digit = $Assoc_Routes['route_id'][0]; break; 
    } 

    if($Digit[0] != $F_D){ 
     # Count 
     $i++; 

        # Avoid the first one 
     if($i > 1){ 
      $Quick[$i-1]['g'].= ' à '.($Assoc_Routes['route_id']-1); 
     } 
     $Quick[$i] = array('g' => 'Groupement '.$Assoc_Routes['route_id']); 
    } 
    $F_D = $Digit[0]; 
} 

感謝您的任何幫助。

回答

1

使跟蹤你是哪個組中一個新的變量:

$currentGroup = 0; 
$groupSize = 100; 

然後你ROUTE_ID比作currentGroup,看它是否是其GROUPSIZE內:以何種形式

if ($Assoc_Routes['route_id'] >= $currentGroup+$groupSize && $groupSize != 0) 
{ 
    //update the value for your currentGroup 
    $currentGroup = floor($Assoc_Routes['route_id']/$groupSize)*$groupSize; 
} 
+0

您應該投射爲int,truncate或地板。圓會在50年代錯誤行事 – Mikhail

+0

@Mikhail你是 - 我將它鋪平了。順便說一句,很好的答案,非常優雅。 – jsleuth

+0

http://pastebin.com/0Zz3qZpL ..代碼更新與評論。工作得很好。非常感謝大家。 –

0

不知道你需要的輸出是,但這裏是我採取的方法:

while ($r = mysql_fetch_object... 
    $index = floor($r->route_id/100); 

    $groups[$index][] = $r;