Jump to content

breaking the while loop each X entries


fael097

Recommended Posts

Wrote it now so here,

 

You would use a counter, then modulus by 4 every loop - if the answer is 0 (no remainder) then insert a new tr row.

 

echo("<table>");

// For loops are much more dynamic/customizable etc - for([executes before first loop];[condition to loop];[executes after each loop])
for($i=0, $row = mysql_fetch_array($result);$i<mysql_num_rows($result);$i++, $row = mysql_fetch_array($result)){

// ** This is your column data.
$thisrow = "<td>test: ".$row['test']."</td>";

// If its the fourth row (also happens to execute when $i = 0, first row).
if($i % 4 == 0){
	// If its _not_ the very first row, end the <tr> tag.
	if($i != 0){
		echo("</tr>\n");
	}
	// Add a new <tr> tag
	echo("<tr>");
}

// Echo this column(<td>)
echo($thisrow);
}

echo("</table>");

 

Note- You can change the modulus to change the amount of columns you want per row.

 

-cb-

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.