user1978081
user1978081

Reputation:

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

Upvotes: 1

Views: 501

Answers (2)

Pete
Pete

Reputation: 58432

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);

Upvotes: 2

Sibiraj PR
Sibiraj PR

Reputation: 1481

$j = 1;
 while($data=mysql_fetch_assoc($q))
 {

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

this will done the things

Upvotes: 0

Related Questions