【问题标题】:Increment based on another incrementing variable基于另一个递增变量的递增
【发布时间】:2012-10-20 21:15:31
【问题描述】:

我遇到了这种奇怪的情况,我想不出解决办法。

我有一个变量$cat_count = 1;,我在一个循环中使用它,然后在我使用它的下面某处执行$cat_count++

然后我有另一个按以下方式工作的字母计数器:

我有$alpha_string = 'abcdefghijklmnopqrstuvwxyz';$alpha_counter = 0;。我通过以下方式使用它 - $alpha = $alpha_string{$alpha_counter}。我希望我的字母计数器从 a 开始计数,只要 $cat_count 加一。

所以例如我们会有这样的:

$cat_count = 1
$alpha = a
$alpha = b

$cat_count = 2
$alpha = a
$alpha = b

我暂时得到的是:

$cat_count = 1
$alpha = a
$alpha = b

$cat_count = 2
$alpha = c
$alpha = d

想法?

谢谢。

【问题讨论】:

  • 调用... $counter = 0;每当 $cat_count 增加时
  • @Hawili 我不明白,请告诉我你的意思。
  • $c = 'a'; $c++; $c === 'b'; 也可以,如果你想要的话

标签: php loops while-loop auto-increment


【解决方案1】:

按照我在 cmets 中的回答..

$counter        = 0;
$cat_count      = 1;
$alpha_count    = 'abcdefghijklmnopqrstuvwxyz';
$rule_id        = null;
$public_cats    = array();

while ($row = $db->sql_fetchrow($result))
{
    if ($rule_id != $row['rule_id']) 
    {       
        $group_ids  = array_map('intval', explode(' ', $row['groups']));
        $is_grouped = false;

        // Check if user can see a specific category if he is not an admin or moderator
        if (!$auth->acl_get('a_') && !$auth->acl_get('m_'))
        {
            $is_grouped = (group_memberships($group_ids, $user->data['user_id'], true)) ? true : false;
        }
        else
        {
            $is_grouped = true; 
        }

        // Fill $public_cats with boolean values
        if ($is_grouped !== false)
        {
            $public_cats[] = $is_grouped;
        }

        $rule_id = $row['rule_id'];

        $template->assign_block_vars('rules', array(
            'RULE_CATEGORY' => $row['rule_title'],
            'ROW_COUNT'     => $cat_count,
            'CAN_SEE_CAT'   => $is_grouped
        ));

        $cat_count++;
        $counter = 0;
    }

    $uid = $bitfield = $options = '';
    generate_text_for_storage($row['rule_desc'], $uid, $bitfield, $options, $row['bbcode'], $row['links'], $row['smilies']);

    $template->assign_block_vars('rules.rule', array(
        'RULE_DESC'     => generate_text_for_display($row['rule_desc'], $uid, $bitfield, $options),
        'ALPHA_COUNT'   => $alpha_count{$counter}
    ));

    $counter++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多