1

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

1
  • so $data is returning results of 2 to 3 columns, am I right? and you want to have an empty cell when $data is returning only 2 results instead of 3 ? Commented Mar 14, 2013 at 14:57

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

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

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

this will done the things

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.