读取文件并写入html表格
问题描述:
我得到一个错误,说第二个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 : ......;
..............
答
$接触是一个字符串,而不是一笔画(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 : ......;
您的代码完美的作品。只是困扰我的布局。 http://s31.postimg.org/mjn05701n/jpeg.jpg这是输出。 AS联系人0名字在第1行第1行中,第2行第1行中的其余部分的详细名称。是否有方法在彼此之间显示名字,姓氏等等? –
Contacts.txt的全部内容如何? –
我的例子[截图](http://postimg.org/image/ysatxxwxj/) –