读取文件并写入html表

时间:2016-04-20 22:06:41

标签: php html html5 file

我在第二个foreach循环中第24行提供了无效参数的错误。你能看出错误吗?它必须最初接收联系人并将其显示在html中的表中。我们将非常感激任何见解。

<!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结构如下

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

1 个答案:

答案 0 :(得分:1)

$ contact是一个字符串,而不是可遍历的(http://php.net/manual/de/class.traversable.php)。

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

您错过了爆炸命令以拆分数据集

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

整个例子

<!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>

样品

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 : ......;