3

Populate php array into HTML table

I have data in a php array (say, $rows2) that gives the below output in key-value pairs. I am trying to format this data into an HTML table:

0 => Month:
1 => 07/2011,
2 => 7-8
3 => / 1432 A.H
4 =>  
5 => Location:
6 => Plainfield,
7 => NJ,
8 => USA
9 => Calculation Method:
10 => Value 10
11 =>  
12 => Juristic Method:
13 => Standard 

I tried to use the below code to populate the table:

echo '<table border=1>';
while (list($key, $value) = each($rows2)) {

    echo $value2.$value;
        if($i%5 == 0){
            echo '<tr><td>'.$value2.'</td></tr>';

        }
        $value2='';
    $i++;
}
echo '</table>';

I get the below output:

--------------------------------------------------------
Month:      |   07/2011     |   7-8     |   1432 A.H
--------------------------------------------------------
Location:   |   Plainfield  |   NJ      |   USA
--------------------------------------------------------
Calculation |   ValueX      |  Juristic |   ValueY
Method:     |               |  Method:  |
--------------------------------------------------------

I am trying to split the last row into 2 rows and then colspan the second to look like the below result.

--------------------------------------------------------
Month:      |   07/2011     |   7-8     |   1432 A.H
--------------------------------------------------------
Location:   |   Plainfield  |   NJ      |   USA
--------------------------------------------------------
Calculation |           ValueX      
Method:     |           
--------------------------------------------------------
Juristic    |           ValueY
Method:     |               
--------------------------------------------------------

Could someone please help? Thanks in advance.

3
  • If your array always looks this way, you're much better off just hard-coding the table. If however your data structure is not constant, then how do you know that you need end the row? What details do you have? I mean, what is the indicator that a particular value must start the next row? Commented Jul 24, 2011 at 18:27
  • Hello. What is dynamic (changing) here? Obviously, the data can change, but will the number of rows or columns change as well? This influences the answer. Because if the data is always going to be like that, you may as well just code it in. Commented Jul 24, 2011 at 18:28
  • the structure of your array is a bit confusing. You generally don't put meta-data (column titles and such) where data is expected. Why don't you first arrange the array in such a way that the meta data are represented as keys instead? Secondly, is it necessary to create the table by using a loop? if you already know that it won't change I'd advise you to rather approach this with a table template. Commented Jul 24, 2011 at 18:31

3 Answers 3

3

If the following things are true:

  1. The number of rows will always be the same
  2. The number of columns will always be the same

Then you're probably better off just inserting parts of the array right into the table, like so:

<table>
<tr>
    <td><?= $array[0]?></td>
    <td><?= $array[1]?></td>
    <td><?= $array[2]?></td>
    <td><?= $array[3]?></td>
</tr>
<tr>
    <td><?= $array[9]?></td>
    <td colspan='3'><?= $array[10]?></td>
</tr>
<tr>
    <td><?= $array[12]?></td>
    <td colspan='3'><?= $array[13]?></td>
</tr>
</table>
Sign up to request clarification or add additional context in comments.

Comments

3
<table>
<?php
foreach( $row in $rows2) { 
    echo '<tr>';
    echo '<td>' . $row[0] . '</td>';
    echo '<td>' . $row[1] . '</td>';
    echo '<td>' . $row[2] . '</td>';
    echo '<td>' . $row[4] . '</td>';
    echo '</tr><tr>';
    echo '<td>' . $row[5] . '</td>';
    echo '<td>' . $row[6] . '</td>';
    echo '<td>' . $row[7] . '</td>';
    echo '<td>' . $row[8] . '</td>';
    echo '</tr><tr>';
    echo '<td>' . $row[9] . '</td>';
    echo '<td colspan="3">' . $row[10] . '</td>';
    echo '</tr><tr>';
    echo '<td>' . $row[12] . '</td>';
    echo '<td colspan="3">' . $row[13] . '</td>';
    echo '</tr>';
} // end foreach
?>
</table>

Comments

1

I think it would be easier if you just mixed the HTML and PHP together instead of trying to do it in a loop like what you're doing.

echo "<table>
<tr>
    <td>{$arr[0]}</td>
    <td>{$arr[1]}</td>
    <td>{$arr[2]}</td>
    <td>{$arr[3]}</td>
</tr>
<tr>
    <td>{$arr[9]}</td>
    <td colspan='3'>{$arr[10]}</td>
</tr>
<tr>
    <td>{$arr[12]}</td>
    <td colspan='3'>{$arr[13]}</td>
</tr>
</table>";

1 Comment

If you're going to use the bracket ({ and }) syntax, then you need to demonstrate that it will be within an echo statement with quotes in PHP. Otherwise, that code is invalid HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.