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.

  • 写回答

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++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题