By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

Working with an array and HTML

P: 60
I want to "put" the values of an array in a <table>. The catch is that i have to arrange the table in a such a way that the number of columns is variable.

I tried with whiles, foreach but with no luck. Then i thought using count() and intval() to get the integer number of how many values should go per column, but i don't know how to continue....

Some help ??? (if possible not the solution)
May 30 '07 #1
Share this Question
Share on Google+
8 Replies

P: 93
Can you give some more details? I'm sure it's possible, but it's a bit tricky to say how without more details.
May 31 '07 #2

P: 60
which details do you want me to give ???
Jun 1 '07 #3

Expert 2.5K+
P: 3,235
Expand|Select|Wrap|Line Numbers
  1. echo '<table>';
  2. foreach($arr as $row)
  3. {
  4.   echo '<tr>';
  5.   foreach($row as $column) echo "<td>$column</td>";
  6.   echo '</tr>';
  7. }
  8. echo '</table>';
If that does not help, you will need to give us more information.
Jun 1 '07 #4

P: 60
The problem with your script is that $row isn't an array so it will show an error the script..
Jun 4 '07 #5

P: 103
echo '<table>';
foreach($arr as $row)
if (is_array($row) && !empty($row) {
echo '<tr>';
foreach($row as $column) echo "<td>$column</td>";
echo '</tr>';
echo '</table>';

You are very mysterious in your requests. If you posted the array you are using it would be easier to post a useful response. I adapted the previous response simply to check if $row is an array. That way the row is only added if there is something inside it.

You may also have a varying number of columns. In which case I suggest two options. 1. if you know the maximum, then use something like inside the first foreach loop:

$c=0; //counter
$m=5; //max number of cols
foreach ($row as $column) {
//echo the row inside conditional as above
if ($c<$m) {
for ($i=$c+1;$c<=$m;$i++) {
echo '<td>&nbsp;</td>';

If you don't know $m (ie you might have more cols or less depending on data), then you must determine it in advance by looping through the array and counting the sub-array (ie $m=count($row)). Bit heavy handed I suppose.
Jun 4 '07 #6

Expert 2.5K+
P: 3,235
The problem with your script is that $row isn't an array so it will show an error the script..
I think you missed the foreach($arr as $row) section of the code. That small segment of code will iterate through any two dimensional array and build a table from it.
Jun 4 '07 #7

P: 60
You got an array, $a = array('asd','a8sy','af9','dsfk','35','4w6','spn',' 9eit',...........) with a large number of elements. And also there's a form with a only one textbox for the user to introduce a number.The elements of the array $a will have to be arranged in N columns (N = number introduced by user).

OK ????
Jun 4 '07 #8

Expert 5K+
P: 5,821
So you want the number of columns to be set by the User, but you want to make sure that every element in the array gets output. Is this correct?

Consider this:

If you were to use a counter to keep track of how many elements you've output, how could you keep track of how many columns you've output? How about modulus?

If (($counter % $_GET['nCols']) === 0), then you should precede your output with a '</tr><tr>'.
Jun 4 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.