循环捕获新的百数

发布于 2024-12-11 07:15:20 字数 1561 浏览 0 评论 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];
}

感谢您的帮助。

I got a list of bus number in my database. Sample here :

    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

We will be working with route_id (first column).

Here is my question. How can I catch a new hundred ? The expected results from above will be

    1 to 99
    100 to 199
    818 to 893

I am almost always finding the answer to my question but this time.. I really don't. I did this but this ain't working well :

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

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初吻给了烟 2024-12-18 07:15:20

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

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

Make a new variable that tracks which group you are in:

$currentGroup = 0;
$groupSize = 100;

Then compare your route_id to the currentGroup to see if it's within its groupSize:

if ($Assoc_Routes['route_id'] >= $currentGroup+$groupSize && $groupSize != 0) 
{
    //update the value for your currentGroup
    $currentGroup = floor($Assoc_Routes['route_id']/$groupSize)*$groupSize;
}
乖乖哒 2024-12-18 07:15:20

不确定您希望输出的形式是什么,但这是我采取的方法:

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

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

Not sure in what form you want the output to be, but here's the approach I'd take:

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

  $groups[$index][] = $r;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文