Jump to content

dinamically add rows/columns to table depending on number of results...


mac007

Recommended Posts

hello, all:

 

I'm a newbie, and been trying to work out a way so that the results from an array (or mysql recordset for that matter), are nicely aranged in a table. I want them to show in a grid-like manner, with rows and columns added according to number of "items".

 

I worked out this small code snippet, with a sample array, and for the life of me, cant figure out how to make it so it automatically "breaks" columns at 5 and start new row. I got it to show me right number of rows, but then it repeats all 10 names within them, as opposed to just showing 5 names in each row...

 

Appreciate the help...

 

<?php
$names = array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia');
$numberColumns = 5;
$numberNames = count($names);
$numberRows = ceil($numberNames / $numberColumns);

echo "<table border='1' width='400' cellspacing='0' cellpadding='0'>";

for ($i=1; $i<=$numberRows; $i++)
	{ 
	echo "<tr>";
		foreach ($names as $name)
		{
		echo "<td>" . $name . "</td>";
		}
	echo "</tr>"; 
	}

echo "</table>";
?>

i'm not sure this is what you're after, but this is what i use to place data in rows instead of columns:

 


/// PUT DATA IN ROWS INSTEAD OF COLUMNS //////////
$items = array('ALABAMA','ALASKA');//create an array with your data

// Default # of Columns
$numcols = 4;

// Number of Items
$numitems = count($items);

// Number of Rows
$numrows = ceil($numitems/$numcols);

echo '<table>';
for ($row=1; $row <= $numrows; $row++) {
$cell = 0;
echo ' <tr>'."\n";
for ($col=1; $col <= $numcols; $col++) {
	echo '  <td>'."\n";

	if ($col===1) {
		$cell += $row;
		print $items[$cell - 1];
	} else {
		$cell += $numrows;
		print $items[$cell - 1];
	}

	echo '  </td>'."\n";
}
echo ' </tr>'."\n";
}
echo '</table>';

You are awesome... I see I was going in the right direction, but all those for loops pretty much turned me into a pretzel! Gonna have to study your code carefully here and see how in the heck the cells get embedded in there..

 

Thanks for your help!

<?php
$names         = array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia');
$numberColumns = 5;
$names         = array_chunk($names, $numerColumns);
$k              = count($names) - 1;
$names[$k] = array_pad($names[$k], 5, '');

echo "<table border='1' width='400' cellspacing='0' cellpadding='0'>";

   foreach($names as $k => $v)
   {
      echo '<tr><td>' . implode('</td><td>', $names) . '</td></tr>';
   }

echo "</table>";
?>

wow, Andy, you threw php functions in there that I had never seen before... very interesting. I tried your code but didnt seem like it worked... didnt seem to output anything. Gotta say, a very different way to approach the problem.. never seen it addressed this way

<?php
$names         = array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia');
$numberColumns = 5;
/*
the following line chunks the array into a multi-dimentional array with each sub-array containing 5 elements
see below.
*/
$names         = array_chunk($names, $numberColumns);
/*
the following two lines pas the last sub-array to five values, filling any extra values with an empty string ''
*/
$k             = count($names) - 1;
$names[$k]     = array_pad($names[$k], 5, '');

echo "<table border='1' width='400' cellspacing='0' cellpadding='0'>\r\n";

   foreach($names as $v)
   {
      echo "\t<tr>\r\n\t\t<td>" . implode("</td>\r\n\t\t<td>", $v) . "</td>\r\n\t</tr>\r\n";
   }

echo "</table>";
?>

 

array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia')

is chunked into

array (
[ 0 ] => array ('Charles', 'Henry', 'Manny', 'Philip', 'Rose')
[ 1 ] => array ('Evelyn', 'Peter', 'Julia', 'Cary', 'Sophia')
      )

 

My bad, works now, tested and outputted:

 

<table border='1' width='400' cellspacing='0' cellpadding='0'>
<tr>
	<td>Charles</td>
	<td>Henry</td>
	<td>Manny</td>
	<td>Philip</td>
	<td>Rose</td>
</tr>
<tr>
	<td>Evelyn</td>
	<td>Peter</td>
	<td>Julia</td>
	<td>Cary</td>
	<td>Sophia</td>
</tr>
</table>

 

 

array_chunk

array_pad

count

implode


Note, \r\n outputs a carrige return (newline) and \t outputs a tab spacer. It's for the formatting of the HTML.

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.