2011-10-22 22 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;