1

I've got a 2D array of movies with keys which was generated via phpMyAdmin via a MySQL Db.

(Plot cut off from copy pasting)

$films = array(
  array('title' => 'Batman Begins','yr' => '2005','genre' => 'Action, Adventure','plot' => 'After training with his mentor, Batman begins his w...')
  array('title' => 'Ted 2','yr' => '2015','genre' => 'Comedy','plot' => 'Newlywed couple Ted and Tami-Lynn want to have a baby, but in order to...')
  array('title' => 'Ted','yr' => '2012','genre' => 'Comedy, Fantasy','plot' => 'As the result of a childhood wish, John Bennett\'s teddy bear, ...')
  array('title' => 'Interstellar','yr' => '2014','genre' => 'Adventure, Sci-Fi','plot' => 'A team of explorers travel through a wormhole in spa...')
);

I've also got a foreach loop, to loop through the 2D array and return the results as a table:

$out = "";
$out .= "<table>";
foreach($films as $key => $element){
  $out .= "<tr>";
  foreach($element as $subk => $subel){
    $out .= "<td>$subel</td>";
  }
  $out .= "</tr>";
}
$out .="<table>";

echo $out;

And from the results I see on the web page, this displays it like this:

Batman Begins     2005     Action, Adventure     After training ...

How would I be able to display the key as the column header? I've tried making another foreach loop inside the main one, but that returned the header like this: titleyrgenreplottitleyrgenreplot etc.

How would I be able to properly format it in a table, so headers appear?

Also just a small, quick question: instead of exporting the database from phpMyAdmin as a PHP array, how would I be able to update the PHP/webpage when a change has been made in the MySQL database table?

2 Answers 2

1

here is how you will get your headers:

$headers="<thead><tr>";
foreach($films as $key => $element){
   $headers.= "<th>$key</th>";
  }
$headers.= "</tr></thead>";

So your code should now look like:

$out = "";
$out .= "<tbody>";
$headers="<table><thead><tr>";
foreach($films as $key => $element){
  $headers.= "<th>$key</th>";
  $out .= "<tr>";
  foreach($element as $subk => $subel){
      $out .= "<td>$subel</td>";
  }
  $out .= "</tr>";
}
$out .="</tbody><table>";
$headers.= "</tr></thead>";

echo $headers;
echo $out;
Sign up to request clarification or add additional context in comments.

Comments

0

For displaying fields as header we just have to loop through the keys of 1st sub array and add another row(<tr>) with table header(<th>) before loop through the entire result. Generally we display headers only once.

 $out = "";
 $out .= "<table>";
 $out .= "<tr>";
 foreach($films[0] as $key => $value)
 {
   $out  .= "<th>".$key."</th>";
 }
 $out .= "</tr>";
 foreach($films as $key => $element){
  $out .= "<tr>";

  foreach($element as $subk => $subel){
   $out .= "<td>$subel</td>";
  }
  $out .= "</tr>";
 }
 $out .="<table>";

 echo $out;

4 Comments

Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
As you can see @JayBlanchard. This seems to be pretty straight forward loop question. So I just answered with respect to Inquirer. However I would take your advice and update my answer.
I understand that it is straightforward, but consider than there will be n number of newbies who will not understand what you have done.
I will make this a point that I explain my each and every answer so that It may be helpful for other too. Thanks @JayBlanchard

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.