Tuesday, July 28, 2009

How do I display more than one column from a php while loop calling one field?

I want the information I retrieved with the while loop to be displayed in 3 columns and start a new row after the 3 columns.

How do I display more than one column from a php while loop calling one field?
In this example a table is used:





// here you can set the number of columns


$numOfCols = 3;





// this is a counter


$numOfEntries = 0;





echo "%26lt;table%26gt;";


while( ... ) {


// first check if a new row is starting


if ($numOfEntries % $numOfCols == 0){


// a new row is starting


echo "%26lt;tr%26gt;";


}





echo "%26lt;td%26gt;";


// print out your data


echo "%26lt;/td%26gt;";





// at last check if this is the end of the row


if ($numOfEntries % $numOfCols == $numOfCols -1){


// close the row


echo "%26lt;/tr%26gt;";


}


$numOfEntries ++;


}





// check if the last row is not closed


// $numOfEntris was inceremented(!)





if ($numOfEntris % $numOfCols != 0){


// it would be nice to print some empty cell here


// close the last row


echo "%26lt;/tr%26gt;";


}





echo "%26lt;/table%26gt;";


No comments:

Post a Comment