dongping4901
2012-10-20 21:15
浏览 23
已采纳

基于另一个递增变量的增量

I have this odd situation and I can't think of a solution for it.

I have a variable $cat_count = 1; and I use it inside a loop and then do $cat_count++ somewhere below where I've used it.

Then I have another alphabetical counter which works the following way:

I have $alpha_string = 'abcdefghijklmnopqrstuvwxyz'; and $alpha_counter = 0;. I use this the following way - $alpha = $alpha_string{$alpha_counter}. I want my alphabetical counter to start counting from a, whenever $cat_count gets incremented by one.

So for example we would have this:

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

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

What I get momentarily is this:

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

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

Ideas?

Thank you.

图片转代码服务由CSDN问答提供 功能建议

我有这种奇怪的情况,我想不出它的解决方案。 \ n

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

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

我有 $ alpha_string ='abcdefghijklmnopqrstuvwxyz'; 和< 代码> $ alpha_counter = 0; 。 我使用以下方法 - $ alpha = $ alpha_string {$ alpha_counter} 。 我希望我的字母计数器从 a 开始计数,只要 $ cat_count 增加1。

所以例如我们会有 这个:

  $ 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 
   
 
 

想法? \ n

谢谢。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douhuangzhi0707 2012-10-20 21:27
    已采纳

    following my answer in comments..

    $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++;
    }
    
    点赞 评论

相关推荐 更多相似问题