views:

83

answers:

3

A short time ago, a gentleman posted this function in an answer to a question. As a learner, I am interested in understanding the function. However, I can't get it to work as is. The poster did not say that he had tested the function, so it could have been a "conceptual" post, meant to show direction. OTOH, I may not be invoking it correctly. Please help as I have spent some time trying to understand it.

(The $data array is one that I just put in to run the function.)

Thanks!

<?php

    $data = array("a" => "January", "b" => "February", "c" => "March" );

    render_table ( $data ) ;
    echo $html ;

    //===========================================

    function render_table($data) {
      $html = '<table>';
      $tr = array();
      foreach (array_keys($data[0]) as $key) {
        $tr[] = '<th>' . htmlspecialchars($key) . '</th>';
      }
      $html .= "\n" . '<thead>' . "\n" . '<tr>' . "\n" . implode("\n", $tr) . '</tr>' . '</thead>';
      $tbody = array();
      foreach ($data as $row) {
        $tr = array();
        foreach ($row as $value) {
          $tr[] = '<td>' . htmlspecialchars($value) . '</td>';
        }
        $tbody[] = '<tr>' . "\n" . implode("\n", $tr) . '</tr>';
      }
      $html .= "\n" . '<tbody>' . "\n" . implode("\n", $tbody) . '</tbody>';
      $html .= '</table>';
      return $html;
    }

    ?>
+2  A: 

Try this way:

<?php

$data = array("a" => "January", "b" => "February", "c" => "March" );

$html = render_table ( $data ) ;
echo $html ;

//===========================================

function render_table($data) {
  $html = '<table>';
  $tr = array();
  foreach (array_keys($data[0]) as $key) {
    $tr[] = '<th>' . htmlspecialchars($key) . '</th>';
  }
  $html .= "\n" . '<thead>' . "\n" . '<tr>' . "\n" . implode("\n", $tr) . '</tr>' . '</thead>';
  $tbody = array();
  foreach ($data as $row) {
    $tr = array();
    foreach ($row as $value) {
      $tr[] = '<td>' . htmlspecialchars($value) . '</td>';
    }
    $tbody[] = '<tr>' . "\n" . implode("\n", $tr) . '</tr>';
  }
  $html .= "\n" . '<tbody>' . "\n" . implode("\n", $tbody) . '</tbody>';
  $html .= '</table>';
  return $html;
}

?>

It basically creates a table view of the $data.

Ekin Koc
Thank you for replying. I'm still getting errors:Warning: array_keys() [function.array-keys]: The first argument should be an array in E:\Wamp\www\temp\temp.php on line 22Warning: Invalid argument supplied for foreach() in E:\Wamp\www\temp\temp.php on line 29
dave
+1  A: 

The function creates a table to view the data that is passed to it. In your code, you have

...
render_table ( $data ) ;
echo $html ;

However, $html in this case is empty. The variable created in the render_table function isn't passed outside the function unless you assign the return value of the function to a variable, like this:

...
$html = render_table ( $data ) ;
echo $html ;

Then it echoes the table HTML.

davethegr8
Thanks. I believe the previous answers also suggested this as part of his response, but I'm still not getting it to work. Has it got anything to do with numeric vs. assoc. array?
dave
+5  A: 

First off, your data is wrong. The function takes an array of arrays as an argument.

Note the line

  foreach (array_keys($data[0]) as $key) {

...this function call is wrong as well; it should NOT be taking the first element, it should be taking the entire array, since it is using the keys from the array.

Try this: first, change the call to array_keys to use the entire array, i.e.

foreach(array_keys($data AS $key)

then change your input, and the function call:

$data= array(
  'data1' => array('one','two','three'),
  'data2' => array('four','five','six'),
  'data3' => array('seven','eight','nine')  
);
echo render_table($data);
bdl
Yes! That does the trick. I did not realize that the function was meant to operate on multi-dim arrays. So the errors were 1. how I tried to call the function; 2. I supplied the incorrect type of array; and 3. the line: foreach(array_keys($data AS $key). Thank you very much!
dave