Cleaner way of using modulus for columns

后端 未结 4 1912
一个人的身影
一个人的身影 2021-01-26 10:31

I currently have a list (

    ) of people that I have divided up into two columns. But after finishing the code for it I keept wondering if there is a more effective or cle
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-26 11:08

    You can just loop twice, once for each column. Start at 0 in the first column and at 1 in the second. Increment by two.

    Edit: To make it even nicer, put the columns themselves in a loop:

    $cols = 3;
    
    echo '';
    
    // column loop
    for ($c = 1; $c <= $cols; $c++) {
      echo '';
    }
    
    echo '
      '; // item loop for ($i = 0; $i < count($areaArray); $i += $c) { echo '
    • ...
    • '; } echo '
    ';

提交回复
热议问题