The function "render_table" is one I got from someone else, but it doesn't seem to work. I have entered a simple array to test it and I get this error: Warning: Invalid argument supplied for foreach(). I have marked the line in which this occurs.

I don't have the experience to know exactly what is causing the error. Please gander at this and see if the error or errors are apparent.

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 ) 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 )     // < ERROR HERE !!
    {
      $tr [] = '<td>' . htmlspecialchars ( $value ) . '</td>';
    }
    $tbody[] = '<tr>' . "\n" . implode ( "\n", $tr ) . '</tr>';
  }
  $html .= "\n" . '<tbody>' . "\n" . implode ( "\n", $tbody ) . '</tbody>';
  $html .= '</table>';
  return $html;
}
?>

    I see that $row needs to contain an array - but you're only giving it strings ("January", "February", etc.)

      Thanks. I have no idea what is going on here. I was hoping it was some missing brackets or something.

        As to what Weedpacket tells
        you can easily fix this.

        $data = array(
        array("a" => "January", "b" => "February", "c" => "March" )
        );
        
        render_table ( $data ) ;
        echo $html ; 
          Write a Reply...