Jump to content

Recommended Posts

Hello

 

i'm trying to split my query results into 3 or 4 columns within a table so that the results look ordered and not random. The data returned from the query is similar for each row in the mysql table and what I would like to do is basically count the number of returned records, divide it by 3 or 4 and then echo the results making sure each entry is in it's own <td> in a grid like format. I don't know the best place to start so I really would appreciate some help.

 

Here is what I have so far. It works fine except that obviously all the results are printed inline and are not in a grid format...

 

<?				

$issues_query="SELECT * FROM issues WHERE issue_on_sale BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 500 DAY)";
$issues_result=mysql_query($issues_query);
if(!$issues_result) {
	print("Query Error: ".mysql_error());
}

while($row=mysql_fetch_array($issues_result))
{

$issue_number = $row['issue_number'];
$issue_month = $row['issue_month'];
$issue_year = $row['issue_year'];

echo 'Issue <strong>' . $issue_number . '</strong> ' . $issue_month . ' ' . $issue_year . '<input name="issue_number" type="checkbox" value="' . $issue_number . '" />'; 


}	?>

 

Regards

 

James

Link to comment
https://forums.phpfreaks.com/topic/110731-split-results-into-columns/
Share on other sites

Rarebit,

 

thanks for your help. I've read through your tutorial and have got my table working by combining the database rows into the array, but i'm a bit stumped how to create 3 or more columns. Which is the part that determines how many columns there would be?

 

Here is what I have now...

 

 while($row=mysql_fetch_array($issues_result)) {

	$issues[]= 'Issue' . ' <strong>' .$row['issue_number'] . '</strong> ' . $row['issue_month'] . ' ' . $row['issue_year'] . '<input name="issue_number" type="checkbox" value="' . $issue_number . '" />';

} 


function table_02($data, $details="")
{
$sret = "<table ".$details.">\n";

$all = count($data)-1;

for($i=0; $i <= $all; $i++)
{
	if(($i % 2)==0)
	{
		$sret .= "<tr><td>".$data[$i]."</td>";
	}
	else
	{
		$sret .= "<td>".$data[$i]."</td></tr>\n";
	}
}
//	Catch if an odd cell
if(($all % 2) == 0)
{
	$sret .= "<td><br></td></tr>\n";
}

$sret .= "</table>\n";
return $sret;
}

$data = $issues;
print table_02($data, "border='1' width='100%'");

 

Regards

 

James

A quick look and I think you just need to change the mod (%) to anther number, e.g. change '% 2' to '% 3' or '% 4', etc...

 

I've tried that and it doesn't work. I have tried changing:

 

		if(($i % 2)==0)
	{
		$sret .= "<tr><td>".$data[$i]."</td>";
	}

 

to this

 

		if(($i % 2)==0)
	{
		$sret .= "<tr><td>".$data[$i]."</td><td>".$data[$i]."</td>";
	}

 

but all that happens is the first two columns are duplicates, not different.

 

Can I use a variable as placeholder or container that gets filled by the query results until there are no more in the array?

 

James

Sorry, only half reading... Have a look version 4, it's multi columns...

function table_04($data, $cols, $details="")
{
$sret = "<table ".$details.">\n";

$all = count($data);
$offset = ceil($all/$cols);

for($i=0; $i < $offset; $i++)
{
	$sret .= "<tr>";
	for($j=0; $j < $cols; $j++)
	{
		$sret .= "<td>".$data[($i+($j*$offset))]."</td>";
	}
	$sret .= "</tr>\n";
}
$sret .= "</table>\n";
return $sret;
}

I've had a look, but I can't see where I would put the array. I've tried using this but I get no results...

 

while($row=mysql_fetch_array($issues_result)) {

$issues[]= '<input name="issue_number" type="checkbox" value="' . $issue_number . '" />Issue <strong>' .$row['issue_number'] . '</strong> ' . $row['issue_month'] . ' ' . $row['issue_year'] ;

} 


function table_04($data, $cols, $details="")
{
$sret = "<table ".$details.">\n";

$all = count($data);
$offset = ceil($all/$cols);

for($i=0; $i < $offset; $i++)
{
	$sret .= "<tr>";
	for($j=0; $j < $cols; $j++)
	{
		$sret .= "<td>".$data[($i+($j*$offset))]."</td>";
	}
	$sret .= "</tr>\n";
}
$sret .= "</table>\n";
return $sret;
}

$data = $issues;

echo table_04($data, "border='0' width='100%'");

 

Sorry for being such a pain!!

 

James

This puts your data into 3 parts, before it was a single dimension array...

 

while($row=mysql_fetch_array($issues_result)) {
$issues[]= array('<input name="issue_number" type="checkbox" value="' . $issue_number . '" />Issue <strong>' .$row['issue_number'] . '</strong> ' ,  $row['issue_month'] , $row['issue_year']) ;
}

 

see if that helps,

 

also your missing the number of columns...

echo table_04($data, 3, "border='0' width='100%'");

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.