色谱柱使用模数的更清洁方法 [英] Cleaner way of using modulus for columns

查看:93
本文介绍了色谱柱使用模数的更清洁方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个名单(< ul>),我将他们分为两列.但是在完成代码之后,我一直想知道是否有更有效或更干净的方法来完成相同的事情.

I currently have a list (<ul>) 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 clean way to do the same thing.

echo "<table class='area_list'><tr>";
// Loop users within areas, divided up in 2 columns
$count = count($areaArray);
for($i=0 ; $i<$count ; $i++) {
  $uid = $areaArray[$i];
  // get the modulus value + ceil for uneven numbers
  $rowCalc = ($i+1) % ceil($count/2);

  if ($rowCalc == 1) echo "<td><ul>";

  // OUTPUT the actual list item
  echo "<li>{$users[$uid]->profile_lastname}</li>";

  if ($rowCalc == 0 && $i!=0) echo "</ul></td>";
}
echo "</tr></table>";

推荐答案

不知道这是否是@llia的意思,但是对于这样的for循环该怎么办:

Dont know whether this is what @llia meant, but what about have the for loops like this:

//declare how many columns are needed
$cols=2;

//iterate over each row of entries (down the column)
for ($i=0;i<$count;i+=cols){ 

     echo "<td><ul>";

     //entry loop (across the row)
     for($j=0;j<$cols;j++){ 

           //whose line is it anyway?
           $uid = $areaArray[$i+$j];

           echo "<li>{$users[$uid]->profile_lastname}</li>";
     }
     //end entry loop

     echo "</ul></td>";
}
//end row loop

那样,您可以设置任意多的列.

that way, you can set however many columns you like.

如果我在等待水壶煮沸以获取急需的咖啡因时错过任何东西,请原谅我!

Forgive me if I missed something as I'm waiting for the kettle to boil to get my much needed caffine!

这篇关于色谱柱使用模数的更清洁方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆