在 foreach 循环中只显示一次标题

Display a title only once in a foreach loop

我试过了,但我觉得我遗漏了什么。 这段代码有什么问题?我想不通。有人能帮忙吗? 我只想显示(annual/monthly 会员)类别标题一次,但这显示在每个选项中,请查看我的屏幕截图。我知道这主要是因为 for each 循环,但是当我向上移动并尝试时,它会将我重定向到主页。可能是我没写好。感谢任何帮助。

这在每个选项中都显示类别标题,这不是我想要的。

<?php
/**
 * @package        Joomla
 * @subpackage     Membership Pro
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2012 - 2016 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
// no direct access
defined('_JEXEC') or die;
?>
<ul class="osm-upgrade-options">
    <?php
    $upgradeOptionCount = 0;
    $display_title = true;
    foreach ($this->upgradeRules as $rule)
    {
        $checked = '';
        if ($upgradeOptionCount == 0)
        {
            $checked = ' checked="checked" ';
        }

        $upgradeOptionCount++;
        $upgradeToPlan = $this->plans[$rule->to_plan_id];
        $symbol = $upgradeToPlan->currency_symbol ? $upgradeToPlan->currency_symbol : $upgradeToPlan->currency;

        $taxRate = 0;
        if ($this->config->show_price_including_tax)
        {
            $taxRate = OSMembershipHelper::calculateMaxTaxRate($rule->to_plan_id);
        }
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('a.title')
        ->from('#__osmembership_categories AS a')
        ->innerJoin('#__osmembership_plans AS b ON a.id = b.category_id')
        ->where('b.id = ' . $rule->to_plan_id);
        $db->setQuery($query);
        $categoryTitle = $db->loadResult();

        if ($categoryTitle == "Annual Membership Plan" && $display_title) {
            echo 'Annual Membership'; 
            $display_title=false;           
        }
        
        else {
            echo 'Monthly Membership';
        }

        ?> 
        <li class="osm-upgrade-option">
            <input type="radio" class="validate[required]" id="upgrade_option_id_<?php echo $upgradeOptionCount; ?>" name="upgrade_option_id" value="<?php echo $rule->id; ?>"<?php echo $checked; ?> />
            <label for="upgrade_option_id_<?php echo $upgradeOptionCount; ?>"><?php JText::printf('OSM_UPGRADE_OPTION_TEXT', $this->plans[$rule->from_plan_id]->title, $upgradeToPlan->title, OSMembershipHelper::formatCurrency($rule->price * (1 + $taxRate / 100), $this->config, $symbol)); ?></label>
        </li>
        <?php
    }


    ?>
</ul>

你可以使用标志变量来做到这一点(如果你的数据在 foreach 循环中)

    <?php
/**
 * @package        Joomla
 * @subpackage     Membership Pro
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2012 - 2016 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
// no direct access
defined('_JEXEC') or die;
?>
<ul class="osm-upgrade-options">
    <?php
    $upgradeOptionCount = 0;
    $display_annual = true;
    $display_month = true;

    foreach ($this->upgradeRules as $rule)
    {
        $checked = '';
        if ($upgradeOptionCount == 0)
        {
            $checked = ' checked="checked" ';
        }

        $upgradeOptionCount++;
        $upgradeToPlan = $this->plans[$rule->to_plan_id];
        $symbol = $upgradeToPlan->currency_symbol ? $upgradeToPlan->currency_symbol : $upgradeToPlan->currency;

        $taxRate = 0;
        if ($this->config->show_price_including_tax)
        {
            $taxRate = OSMembershipHelper::calculateMaxTaxRate($rule->to_plan_id);
        }
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('a.title')
        ->from('#__osmembership_categories AS a')
        ->innerJoin('#__osmembership_plans AS b ON a.id = b.category_id')
        ->where('b.id = ' . $rule->to_plan_id);
        $db->setQuery($query);
        $categoryTitle = $db->loadResult();

        if ($categoryTitle == "Annual Membership Plan") {
            if($display_annual)
            {
                echo 'Annual Membership';     
                $display_annual=false;
            }
        }

        else{
            if($display_month)
            {
                echo 'Monthly Membership';
                $display_month=false;
            }
        }

        ?> 
        <li class="osm-upgrade-option">
            <input type="radio" class="validate[required]" id="upgrade_option_id_<?php echo $upgradeOptionCount; ?>" name="upgrade_option_id" value="<?php echo $rule->id; ?>"<?php echo $checked; ?> />
            <label for="upgrade_option_id_<?php echo $upgradeOptionCount; ?>"><?php JText::printf('OSM_UPGRADE_OPTION_TEXT', $this->plans[$rule->from_plan_id]->title, $upgradeToPlan->title, OSMembershipHelper::formatCurrency($rule->price * (1 + $taxRate / 100), $this->config, $symbol)); ?></label>
        </li>
        <?php
    }


    ?>
</ul>