Jump to content

columns of results


1internet

Recommended Posts

e.g.

$q = mysql_query(SELECT * FROM `table`);
<div>
while($r = mysql_fetch_assoc($q)) {
     echo $r['name'];
}
</div>

But how do I split that into 4 divs:

 

<div id="1"></div><div id="2"></div><div id="3"></div><div id="4"></div>, withouth having to do 4 sepate queries?

Link to comment
https://forums.phpfreaks.com/topic/279947-columns-of-results/#findComment-1439861
Share on other sites

Not very elegant, but should give you the idea:

$q = mysql_query(SELECT * FROM `table`);
$total = mysql_num_rows($q);
$groups = ceil($total / 4);
$i = 0;
while($r = mysql_fetch_assoc($q)) {
     if($i % $groups == 0) {
		if($i != 0) {
			echo '</div>'.PHP_EOL;
		}
		$id = $i / $groups + 1;
		echo '<div id="'.$id.'">'.PHP_EOL;
	}
	echo $r['name'].PHP_EOL;
	$i++;
}
Link to comment
https://forums.phpfreaks.com/topic/279947-columns-of-results/#findComment-1439931
Share on other sites

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.