2016-04-20 153 views
0

我得到一個錯誤,說第二個foreach循環第24行提供了無效參數。你能看到這個錯誤嗎?它最初必須將聯繫人顯示在html中的表格中。任何洞察力將非常感激。讀取文件並寫入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

$接觸是一個字符串,而不是一筆畫(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 : ......; 
+0

您的代碼完美的作品。只是困擾我的佈局。 http://s31.postimg.org/mjn05701n/jpeg.jpg這是輸出。 AS聯繫人0名字在第1行第1行中,第2行第1行中的其餘部分的詳細名稱。是否有方法在彼此之間顯示名字,姓氏等等? –

+0

Contacts.txt的全部內容如何? –

+0

我的例子[截圖](http://postimg.org/image/ysatxxwxj/) –