簡體   English   中英

foreach PHP 出了點問題

[英]foreach PHP something wrong

我在 PHP 中有這段代碼

<?php foreach($this->group('filter') as $i => $fields): ?>
        <?php $newField = $this->field('name#'.$i)->value(); ?>
        <?php $finalvar = explode(",", $newField); ?>
            <a data-filter=<?php foreach($finalvar as $fletter) : ?>".filter_<?php echo standardize($fletter); ?>">
            <?php endforeach; ?>    
                <?php if($this->field('icon#'.$i)->value()): ?><i class="<?php echo $this->field('icon#'.$i)->value(); ?>"></i><?php endif; ?>
                <span class="name"><?php if($this->field('label_items#'.$i)->value()): ?><?php echo $this->field('label_items#'.$i)->value(); ?><?php else: ?><?php echo $this->field('name#'.$i)->value(); ?><?php endif; ?>
                </span>
        </a>
        <?php endforeach; ?>

PHP 代碼有效,但我的 HTML 代碼有問題。 生成的 HTML 代碼是這樣的:

<a data-filter=".filter_country">
            ".filter_capital;
                
    <span class="name">Australia</span>
</a>

我的目標是實現這個:

<a data-filter=".filter_country,.filter_capital"
    <span class="name">Australia</span>
</a>

我知道它在我面前,但我看不到它。 有人對語法有想法嗎?

檢查你的語法:

<?php foreach ($this->group('filter') as $i => $fields) : ?>
    <?php $newField = $this->field('name#' . $i)->value(); ?>
    <?php $finalvar = explode(",", $newField); ?>
    <a data-filter="
        <?php
        $i = 1;
        foreach ($finalvar as $fletter) { ?>
            .filter_<?php echo standardize($fletter); ?>
            <?php if ($i < count($fletter)) { ?>, <?php } ?>
        <?php $i++; } ?>
    ">
    <?php if ($this->field('icon#' . $i)->value()) : ?><i class="<?php echo $this->field('icon#' . $i)->value(); ?>"></i><?php endif; ?>
    <span class="name"><?php if ($this->field('label_items#' . $i)->value()) : ?><?php echo $this->field('label_items#' . $i)->value(); ?><?php else : ?><?php echo $this->field('name#' . $i)->value(); ?><?php endif; ?>
    </span>
    </a>
<?php endforeach; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM