duanlie7447 2014-02-19 21:53
浏览 17
已采纳

迭代php中的对象无法正常工作

I have been struggling to get the iteration working of a objects array in php. I have a array structure like this:

Array
(

[0] => stdClass Object

(
        [tid] => 7
        [vid] => 2
        [name] => Bakkerijen
        [description] => 
        [format] => full_html
        [weight] => 0
        [depth] => 0
        [parents] => Array
            (
                [0] => 0
            )

    )

[1] => stdClass Object
    (
        [tid] => 10
        [vid] => 2
        [name] => Horeca
        [description] => 
        [format] => full_html
        [weight] => 1
        [depth] => 0
        [parents] => Array
            (
                [0] => 0
            )

    )

[2] => stdClass Object
    (
        [tid] => 8
        [vid] => 2
        [name] => Food Shops
        [description] => 
        [format] => full_html
        [weight] => 2
        [depth] => 0
        [parents] => Array
            (
                [0] => 0
            )

    )   

)

I want to add a selected class to li if a value matches to tid within this array.

Here is the code and HTML I did for that:

 <?php $current_tid = 7; ?>
 <?php foreach ($terms as $term): ?>
              <?php unset($term->parents); ?> 
                <?php if($term->tid == $current_tid) { ?>
                <li><a href="/portfolio#filter=.tid-<?php print $term->tid; ?>" class="selected"><?php print $term->name;?></li>
                <?php }else if($term->tid != $current_tid){ ?>
                <li><a href="/portfolio#filter=.tid-<?php print $term->tid; ?>"><?php print $term->name; ?></a></li>

                <?php } ?>
              <?php endforeach; ?> 

This is the final output I am getting:

<ul class="cathome-list-inner">                            
<li>
    <a class="selected" href="/portfolio#filter=.tid-7">Bakkerijen</a>
</li>
<a class="selected" href="/portfolio#filter=.tid-7"></a>
<li>
    <a class="selected" href="/portfolio#filter=.tid-7"></a>
    <a href="/portfolio#filter=.tid-10">Horeca</a>
</li>                                          
<li>
    <a href="/portfolio#filter=.tid-8">Food Shops</a></li>
<li>
    <a href="/portfolio#filter=.tid-9">Non-food Shops</a></li>
<li>
    <a href="/portfolio#filter=.tid-11">Privé interieurprojecten</a>
</li>                                                   

The issue is why I am getting extra anchor tag added with the selected class..

  • 写回答

1条回答 默认 最新

  • dongren4147 2014-02-19 21:59
    关注

    On this line:

    <li><a href="/portfolio#filter=.tid-<?php print $term->tid; ?>" class="selected"><?php print $term->name;?></li>
    

    You are missing your closing </a> the browser is probably trying to automatically correct your mistake.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP