aborted
aborted

Reputation: 4541

Increment based on another incrementing variable

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.

Upvotes: 0

Views: 108

Answers (1)

Hawili
Hawili

Reputation: 1659

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

Upvotes: 2

Related Questions