简体   繁体   English

计数循环内的html表列

[英]count html table columns inside loop

I am having a basic problem, but now it gives pain me lot. 我有一个基本问题,但现在这让我非常痛苦。 I just want a table which have three column in each row. 我只想要一个每行有三列的表。 I want to add a extra empty column in a row when it has two columns. 当它有两列时,我想在一行中添加一个额外的空列。 code here... 代码在这里...

$j=0;
while ($data = mysql_fetch_assoc($q))
{
    // when 3 columns fill, it create new row 
    if (($j%3) == 0)
    {
        echo "ADD A ROW";     
    }
    $j++;
}

But now I need to know how many columns ( $j value) in this loop to add a extra empty column in a row when it has two columns. 但是现在我需要知道在此循环中有多少列( $j值),以便在有两列时在一行中添加额外的空列。 I know count() is not available in loop. 我知道count()在循环中不可用。 If know $columnNumber , I can handle this look like... 如果知道$columnNumber ,我可以处理一下...

if ($columnNumber == 2)
{
    echo "ADD A COLUMN";      
}

How I do 我怎样做

As j will be the total number of columns after your while loop has completed, you can calculate how many extra columns you need with: 由于j是while循环完成后的总列数,因此您可以使用以下方法计算所需的额外列数:

$remainder = (j % 3);
$columnsLeft = ($remainder == 0 ? 0 : 3 - $remainder);
$j = 1;
 while($data=mysql_fetch_assoc($q))
 {

  if($j == 3)
  {
    echo "ADD A ROW";   
    $j = 0;
  }
  $j++;
 }

this will done the things 这会做的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM