简体   繁体   中英

foreach PHP something wrong

I have this code in 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 code works but I have a problem on the HTML code. The generated HTML code is this:

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

My goal is to implement this one:

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

I know it's something in front of me but I cannot see it. Does anyone have an idea concerning the syntax?

Check your syntax:

<?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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM