doujian7132 2015-11-14 00:45
浏览 189
已采纳

PHP foreach循环问题与第一个元素

I'm trying to add first and last class to some LI elements and I'm having a problem with the first index. The code where I have the issue is here:

// this is always true, and all items get the `first` class
if ($i == 0) { $menu_item_class .= ' first'; } 

The entire code looks like this:

function social_icons( $menu_item_class='', $icon_class='', $title_class = ''){
   $menu_item_class .= ($title_class != '') ? ' no-title' : '';
   $social_network = array(
     0=>  array (
       "title"=> "ello",
       "url"=>"envato"
     ),
     1=> array (
       "title"=> "vk",
       "url"=> "envato"
     ),
     2=> array (
       "title"=> "twitter",
       "url"=> "envato"
     ),
     3=> array (
       "title"=> "lastfm",
       "url"=> "envato"
     )
   ); 
   $social_icon_wrapper = ' string';
   $type = ' string2';
   $count = 4;

   if ( $count > 0 ) { ?>
   <div id="social-network">
       <ul class="social-icons">
       <?php foreach ($social_network as $i=>$sn ){
           // var_dump($i); // this always returns fine
           if ( $i == 0 ) { $menu_item_class .= ' first'; } // for some reason this IF is always true
           if ( $i == ($count-1) ) { $menu_item_class .= ' last'; } ?>
           <?php if ( $sn['title'] == 'ello' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="ello" href="https://ello.co/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-ello"></i></span>ello</a></li><?php } ?>
           <?php if ( $sn['title'] == 'vk' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="vk" href="https://vk.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-vk"></i></span>vk</a></li><?php } ?>
           <?php if ( $sn['title'] == 'facebook' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="facebook" href="https://www.facebook.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-facebook"></i></span>facebook</a></li><?php } ?>
           <?php if ( $sn['title'] == 'pinterest' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="pinterest" href="https://pinterest.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-pinterest"></i></span>pinterest</a></li><?php } ?>
           <?php if ( $sn['title'] == 'twitter' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="twitter" href="https://www.twitter.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-twitter"></i></span>twitter</a></li><?php } ?>
           <?php if ( $sn['title'] == 'lastfm' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="lastfm" href="http://www.last.fm/user/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-lastfm"></i></span>lastfm</a></li><?php } ?>
       <?php } ?>      
       </ul>
   </div>

   <?php } 
 }
 social_icons();

I made a simple pen to illustrate the issue. Please check and let me know what's wrong.

  • 写回答

2条回答 默认 最新

  • dongnanbi4942 2015-11-14 01:44
    关注

    Just so you can understand, take a lool at this code.

    And more explanation: You have a variable $class_name that has, for example, the value abc, once it enters the foreach and the $i IS equals to 0, $class_name is going to be abc first. But from now on, you'll keep accessing $class_name, which is, guess what, abc first.

    This Code is changed to keep the correct value (note that this might not be the best way, but what you need to understand is that once you add first to the variable, next time you access it, it's going to be there).

    Take a look at the // ADDED THIS LINE lines:

    function social_icons( $menu_item_class='', $icon_class='', $title_class = ''){
        $menu_item_class .= ($title_class != '') ? ' no-title' : '';
        $original_class = $menu_item_class; // ADDED THIS LINE
        $social_network = array(
          0=>  array (
            "title"=> "ello",
            "url"=>"envato"
          ),
          1=> array (
            "title"=> "vk",
            "url"=> "envato"
          ),
          2=> array (
            "title"=> "twitter",
            "url"=> "envato"
          ),
          3=> array (
            "title"=> "lastfm",
            "url"=> "envato"
          )
        ); 
        $social_icon_wrapper = ' string';
        $type = ' string2';
        $count = 4;
    
        if ( $count > 0 ) { ?>
        <div id="social-network">
            <ul class="social-icons">
            <?php foreach ($social_network as $i=>$sn ){
                // var_dump($i); // this always returns fine
                $menu_item_class = $original_class; // ADDED THIS LINE
                if ( $i == 0 ) { $menu_item_class .= ' first'; } // for some reason this IF is always true
                if ( $i == ($count-1) ) { $menu_item_class .= ' last'; }
                ?>
                <?php if ( $sn['title'] == 'ello' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="ello" href="https://ello.co/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-ello"></i></span>ello</a></li><?php } ?>
                <?php if ( $sn['title'] == 'vk' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="vk" href="https://vk.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-vk"></i></span>vk</a></li><?php } ?>
                <?php if ( $sn['title'] == 'facebook' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="facebook" href="https://www.facebook.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-facebook"></i></span>facebook</a></li><?php } ?>
                <?php if ( $sn['title'] == 'pinterest' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="pinterest" href="https://pinterest.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-pinterest"></i></span>pinterest</a></li><?php } ?>
                <?php if ( $sn['title'] == 'twitter' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="twitter" href="https://www.twitter.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-twitter"></i></span>twitter</a></li><?php } ?>
                <?php if ( $sn['title'] == 'lastfm' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="lastfm" href="http://www.last.fm/user/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-lastfm"></i></span>lastfm</a></li><?php } ?>
            <?php } ?>      
            </ul>
        </div>
    
        <?php } 
    }
    social_icons();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 logisim中设计一个位于十字路口的交通信号灯控制系统
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码