简体   繁体   中英

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. I know count() is not available in loop. If know $columnNumber , I can handle this look like...

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:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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