Connecting Tech Pros Worldwide Forums | Help | Site Map

Working with an array and HTML

Member
 
Join Date: Apr 2007
Posts: 60
#1: May 30 '07
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)
Member
 
Join Date: Feb 2007
Posts: 94
#2: Jun 1 '07

re: Working with an array and HTML


Can you give some more details? I'm sure it's possible, but it's a bit tricky to say how without more details.
Member
 
Join Date: Apr 2007
Posts: 60
#3: Jun 1 '07

re: Working with an array and HTML


which details do you want me to give ???
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#4: Jun 1 '07

re: Working with an array and HTML


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>';
  9.  
If that does not help, you will need to give us more information.
Member
 
Join Date: Apr 2007
Posts: 60
#5: Jun 4 '07

re: Working with an array and HTML


The problem with your script is that $row isn't an array so it will show an error the script..
Member
 
Join Date: Jun 2007
Posts: 101
#6: Jun 4 '07

re: Working with an array and HTML


[PHP]
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>';
[/PHP]

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:

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

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.
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#7: Jun 4 '07

re: Working with an array and HTML


Quote:

Originally Posted by pplers

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.
Member
 
Join Date: Apr 2007
Posts: 60
#8: Jun 4 '07

re: Working with an array and HTML


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 ????
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#9: Jun 4 '07

re: Working with an array and HTML


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>'.
Reply