Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate php array into HTML table

Tags:

html

php

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.

like image 573
user765081 Avatar asked Aug 06 '26 23:08

user765081


2 Answers

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>
like image 94
element119 Avatar answered Aug 08 '26 22:08

element119


<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>
like image 43
KyleWpppd Avatar answered Aug 08 '26 21:08

KyleWpppd