Jump to content

columns of results


1internet

Recommended Posts

You only run one query. Then you use PHP to format the output using PHP code. You don't state how the columns should be constructed

 

1 2 3 4
5 6 7 8

 

or

 

 

1 3 5 7
2 4 6 8

 

Plus, how will you display them? DIVs, in a table, or what?

Link to comment
Share on other sites

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.