dongzhan1570 2017-06-17 03:09
浏览 45
已采纳

使用PHP for循环以Z字形顺序放置一系列图像

I'm a beginner in PHP and I'm seeking your guidance to know the possibilities of placing a sequence of images in a zigzag order using PHP loop.

The number of images received may vary each time, so the img src code is placed in a foreach loop, I currently have them display one below the other but would like to know if there is an opportunity to place them in a zigzag manner as image attached

Output I'm trying for

$j=0; 
 foreach ($data['employee'] as $teammember) {
echo $data['team']['members'][$j]['TeamRank'] . "." . $data['team']['members'][$j]['name'] ;
echo "<img src='http://yyyyyyy/employeeimage'" . $j . "'.png' />";
$j = $j + 1;
}

I'm seeking your advice in placing the name and image in zigzag form.

enter image description here

  • 写回答

1条回答 默认 最新

  • douzhe3516 2017-06-17 03:16
    关注

    This is really just CSS. Put your loop or the data from the loop inside of an element that wraps it, and on that element, apply display: flex; flex-direction: column; align-items: flex-start, and use the :nth-child() selector to change every other flex child to align-self: flex-end;

    In your loop, change this so that the text and image are wrapped in an element.

    <div class="flex">
    <?php
      $j=0; 
      foreach ($data['employee'] as $teammember) {
        echo '<div><p>' . $data['team']['members'][$j]['TeamRank'] . "." . $data['team']['members'][$j]['name'] . '</p>';
        echo "<img src='http://yyyyyyy/employeeimage'" . $j . "'.png' /></div>";
        $j = $j + 1;
      }
    ?>
    </div>
    

    And use this CSS.

    .flex {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      max-width: 50vw;
      margin: auto;
    }
    
    .flex > div:nth-child(odd) {
      align-self: flex-end;
    }
    <div class="flex">
      <!-- your php code would go here, and just output a list of images -->
      <div>
        <p>text</p>
        <img src="http://kenwheeler.github.io/slick/img/lazyfonz2.png">
      </div>      
      <div>
        <p>text</p>
        <img src="http://kenwheeler.github.io/slick/img/lazyfonz2.png">
      </div>
      <div>
        <p>text</p>
        <img src="http://kenwheeler.github.io/slick/img/lazyfonz2.png">
      </div>
      <div>
        <p>text</p>
        <img src="http://kenwheeler.github.io/slick/img/lazyfonz2.png">
      </div>
      <div>
        <p>text</p>
        <img src="http://kenwheeler.github.io/slick/img/lazyfonz2.png">
      </div>
      <div>
        <p>text</p>
        <img src="http://kenwheeler.github.io/slick/img/lazyfonz2.png">
      </div>
    </div>

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题