6

Can someone help me with this array that I have? I want to create a table that is a maximum of 5 columns and a maximum of 15 rows. If there are only 4 rows, for example, then only 4 rows should be shown instead of the 15. If there are only 3 cells that have data then the the remaining 2 should be padded with $nbsp;.

Here is my sample array:

Array
(
    [0] => Array
        (
            [name] => test1
            [item_id] => 1
        )
    [1] => Array
        (
            [name] => test2
            [item_id] => 2
        )
    [2] => Array
        (
            [name] => test3
            [item_id] => 3
        )
    [3] => Array
        (
            [name] => test4
            [item_id] => 4
        )
    [4] => Array
        (
            [name] => test5
            [item_id] => 5
        )
    [5] => Array
        (
            [name] => test6
            [item_id] => 6
        )
)

My data repeats if there is a new row added. This is my issue at present.

$row = count( $array ) / 5;
$col = 5;

echo'<table border="1" width="700">';

for( $i = 0; $i < $row; $i++ )
{
    echo'<tr>';
    for( $j = 0; $j < $col; $j++ ) {
        if( ! empty( $array[$j] ) ) {
            echo '<td>'.$array[$j]['item_id'].'</td>';
        }
    }
    echo'</tr>';
}

echo'</table>';
jim
  • 69
  • 1
  • 1
  • 3
  • 1
    What have you tried? And do you know the basic control structures of PHP (`if`/`else`/`elseif`, `for`/`foreach`, `while`, `do`-`while`, `break`, etc.)? http://www.php.net/manual/en/language.control-structures.php http://www.php.net/manual/en/language.types.array.php – Lèse majesté Dec 25 '10 at 10:08
  • N-level array into table has answered here https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array/47015800#47015800 – Delickate Oct 30 '17 at 13:13
  • This thread has been answered here [Click here](https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array/47068295#47068295) – Delickate Nov 02 '17 at 05:44

3 Answers3

11

Let's call your array $rows, ok?

echo "<table>";
foreach ($rows as $row) {
   echo "<tr>";
   foreach ($row as $column) {
      echo "<td>$column</td>";
   }
   echo "</tr>";
}    
echo "</table>";

Using foreach is more idiomatic for looping trough arrays in php, and it greatly increase your code's readability. Plus, the only variable you need for this is one containing the array itself.

cbrandolino
  • 5,873
  • 2
  • 19
  • 27
  • Thanks cbrandolino, I'll give this a try now. brb – jim Dec 25 '10 at 11:36
  • cbrandolino, This does work but what I need is something that will allow me the flexibility to specify 5 columns. Any additional data in the array should be wrapped into a new row. Your code simply echos the data out exactly as it is in the loop. – jim Dec 25 '10 at 11:40
  • Sorry, I didn't get you then. I would suggest to filter the array first and then go with this, but it's likely that you would find that code more complicated. Just use @Sarfraz code and change the for condition like this: `for ($i=$start; $i=$start+5, $i++)`, where $start is the first element you want to display. – cbrandolino Dec 25 '10 at 12:12
  • Thanks for your help cbrandolino. If you compare my code with that of Sarfraz's, you'll see that the functionality is the same. I need to rethink this a bit but I really do appreciate your help. What is happening with my code is that I am getting repeating rows. The rows are cutting off at 5 columns which is exactly what I need but the first row is then repeated on the seconf row. – jim Dec 25 '10 at 12:15
  • @jim, you're welcome. Anyway, there's really much to rethink: just assign $start a value and use this code http://pastebin.com/du220DTy - based on yours - instead of what you posted. – cbrandolino Dec 25 '10 at 12:20
2

Here's a snippet I wrote to test converting a php array to html.

    $row = array(
        'column 1' => 'value',
        'column 2' => 'value',
        'column 3' => 'value',
        'column 4' => 'value',
        'column 5' => 'value',
        'column 6' => 'value',
        'column 7' => 'value',
    );

    $rows = array($row, $row, $row, $row, $row, $row, $row, $row, $row, $row);

    print array_to_html($rows);


function array_to_html($data) {
    $report = "";

    if (count($data) > 0) {

        $report .= "<table>";
        $report .= sprintf("<tr><th>%s</th></tr>", join("</th><th>", array_keys($data[0])));

        foreach ($data as $row) {

            $report .= "<tr>";

            foreach ($row as $column) {
                $report .= "<td>$column</td>";
            }
            $report .= "</tr>";
        }
        $report .= "</table>";
    } else {
        $report = "No data";
    }

    return $report;
}
jbrahy
  • 4,228
  • 1
  • 42
  • 54
2

Check out:

PHP array to table

Or this class

It is basically simple logic something like this:

echo "<table border=\"5\" cellpadding=\"10\">";

for ($i=0; $i < count($input); $i++)
{
    echo "<tr>";
    for ($c=0; $c<$cols; $c++)
    {
      echo "<td>$input[$i]</td>";
    }
    echo "</tr>";
}

echo "</table>"; 
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • Thanks Sarfraz, can you please have a look at what I've got above? I've edited my post. – jim Dec 25 '10 at 10:12