doucou19961205 2015-01-13 14:17
浏览 104

Foreach循环不破坏HTML结构 - PHP

I have simple HTML / CSS code which should display the 5 most-used browsers used on the website. HTML:

<div class="content">
    <div class="browserStat big">
        <img src="{$basePath}/img/browser-chrome-big.png" alt="Chrome">
        <span>36%</span>
    </div>
    <div class="browserStat big">
        <img src="{$basePath}/img/browser-firefox-big.png" alt="Firefox">
        <span>34%</span>
    </div>
    <div class="browserStat">
        <img src="{$basePath}/img/browser-ie.png" alt="Internet Explorer">
        <span>34%</span>
    </div>
    <div class="browserStat">
        <img src="{$basePath}/img/browser-safari.png" alt="Safari">
        <span>34%</span>
    </div>
    <div class="browserStat">
        <img src="{$basePath}/img/browser-opera.png" alt="Opera">
        <span>34%</span>
    </div>                                                  
</div>

I also have all the percentages and browsers in my array from the database (browsers are saved as numbers, so for example 1 = IE , 2= Chrome , etc.)

I can easily echo it out like:

foreach($borowser as $elem){
  echo '<div>
            <div class="country">'.$elem["borowser"].'</div>
            <div class="number">'.$elem["number"].'</div>
        </div>';
}

So basically it looks I have everything which is needed, but I have problem with writing a correct cycle that doesn't break up this HTML / CSS, because it has to always have just 2 browsers with big class and 3 with no big class.

Can somebody help me to write cycle which will not break this HTML / CSS ?

  • 写回答

2条回答 默认 最新

  • dslijian2015 2015-01-13 14:21
    关注

    Try this script:

    $i = 0;
    foreach($borowser as $elem){
      $is_big = ($i < 2) ? 'big' : '';
      echo '<div class="browserStat ' . $is_big . '">
            <div class="country">'.$elem["borowser"].'</div>
            <div class="number">'.$elem["number"].'</div>
        </div>';
      $i++;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程