Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在循环中捕捉新的百位数_Php_Mysql_Loops_Modulo - Fatal编程技术网

Php 在循环中捕捉新的百位数

Php 在循环中捕捉新的百位数,php,mysql,loops,modulo,Php,Mysql,Loops,Modulo,我的数据库里有一张公共汽车号码表。此处示例: 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,Il

我的数据库里有一张公共汽车号码表。此处示例:

    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];
}

感谢您的帮助。

创建一个新变量,跟踪您所在的组:

$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;
}

不确定您希望输出的形式,但以下是我将采取的方法:

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

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

您应该将其转换为int、truncate或floor。回合将在50'时出错s@Mikhail是的,我把它弄坏了。顺便说一句,回答很好,很优雅。。用注释更新代码。工作得很好。谢谢大家。