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

时间:2017-01-22 08:27:16

标签: php joomla3.0

我试过这个,但想想我错过了什么。 这段代码有什么问题?我无法弄明白。有人能帮忙吗? 我只想显示(年度/月度会员)类别标题一次,但每个选项都会显示,请参阅我的截图。我知道这主要是因为每个循环,但是当我向上移动并尝试时,它会将我重定向到主页。可能是我没有写好。任何帮助表示赞赏。

每个选项中的显示类别标题,这不是我想要的。 enter image description here



<?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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

你可以使用flag变量(如果你的数据在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>