Renier Swart
Renier Swart

Reputation: 85

Reading file and writing to an html table

I get an error saying invalid argument supplied in line 24 on the second foreach loop. Can you see the mistake. It must initially take the contacts and display it into a table in the html. any insight will greatly be appreciated.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
  <link rel="stylesheet" type="text/css" href="styles/session_Style.css">
    <title>User Session</title>
  </head>
  <body>
    <div class="container">

      <div id="table">
      <?php

$filename = "Contacts.txt";
$content = file_get_contents($filename);
$formContacts = explode('|----------|', $content);

foreach ($formContacts as $formContact => $contact) {

  echo '<table>';
  echo '<tr><th colspan="2">Contact '.$formContact.'</th></tr>';

  foreach ($contact as $detail) {
    $details = explode(':',$detail);
    echo '<tr><td>'.$details[0].'</td><td>'.$details[1].'</td></tr>';
  }
  echo '</table>';
}
?>
      </div>      
      <div class="logout">
        <h1>User Details</h1>
        <form method="post" action="">
          <p class="submit"><input type="submit" name="commit" value="Log Out" onclick="document.location.href='home-page.html';"></p>
        </form>
      </div>
    </div>
  </body>
</html>

Contacts.txt is structured like this

 First Name : ......;
    Last Name: ......;
    Contact Details:.....;
    Email Address:.....;
    Enquirer Type:.....;
    Contact Method:....;
    Message:....';
    |----------|
    First Name : ......;
    ..............

Upvotes: 0

Views: 25

Answers (1)

Volker Schukai
Volker Schukai

Reputation: 166

$contact is a string and not a traversable (http://php.net/manual/de/class.traversable.php).

print_r(gettype($contact));
foreach ($contact as $detail) {

You're missing the command explode to split the data set

$row = explode(';',$contact);

the entire example

<!DOCTYPE html>
<html>
   <head>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width">
          <link rel="stylesheet" type="text/css" href="styles/session_Style.css">
          <title>User Session</title>
      </head>
      <body>
          <div class="container">
               <div id="table">
                  <?php
                  $filename = "Contacts.txt";
                  $content = file_get_contents($filename);
                  $formContacts = explode('|----------|', $content);
                  if(!is_array($formContacts)) $formContacts = [];

                    foreach($formContacts as $formContact=> $contact) {

                        echo '<table>';
                        echo '<tr><th colspan="2">Contact '.$formContact.'</th></tr>';
                        $row = explode(';', $contact);
                        if(!is_array($row)) continue;
                        foreach($row as $detail) {
                            $details = array_pad(explode(':', $detail, 2), 2, null);
                            echo '<tr><td>'.$details[0].'</td><td>'.$details[1].'</td></tr>';
                        }
                        echo '</table>';
                    }
                    ?>
                </div>      
                <div class="logout">
                    <h1>User Details</h1>
                    <form method="post" action="">
                        <p class="submit"><input type="submit" name="commit" value="Log Out" onclick="document.location.href = 'home-page.html';"></p>
                    </form>
                </div>
            </div>
        </body>
    </html>

samples

First Name : ......;
Last Name: ......;
Contact Details:.....;
Email Address:.....;
Enquirer Type:.....;
Contact Method:....;
Message:....;
|----------|
First Name : ......;
Last Name: ......;
Contact Details:.....;
Email Address:.....;
Enquirer Type:.....;
Contact Method:....;
Message:....;
|----------|
First Name : ......;

Upvotes: 1

Related Questions