使用foreach循环将数组打印到表中。 HTML / PHP

时间:2016-10-23 03:33:20

标签: php html

编程分配,为用户创建文本区域以输入文本行,然后将文本打印到包含项目和项目编号的表格中。

该项目以新行字符/返回字符结束,项目数应计算并显示在表格中。

我相信我的问题在foreach循环中的某个地方,但似乎无法找到解决方案。

任何帮助都会非常感谢。

<!doctype html>
<META HTTPEQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<meta httpequiv="expires" content="0" />
<html lang="en">
<head>
  <title>  </title>
  <link rel="stylesheet" href="hw7.css">
</head>
<body>
<?

$text = $_POST['stuff'];

$wordlist = explode("PHP_EOL", $text);
$wordcount = count($text);


print '<table>
  <tr>
	<th>Item Number</th>
	<th>Item</th>
  </tr>
';

//asort($wordlist);
foreach ($wordlist as $wordlists){
	
print "	<tr>
		    <td> $wordcount <br/> </td> 
		    <td> $wordlists <br/> </td>   
	    </tr>";
	
print '</table>';	
}


	

?>

</body>
</html>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<meta http-equiv="expires" content="0" />
<html lang="en">
<head>
  <title>  </title>
  <link rel="stylesheet" href="hw7.css">
</head>
<body>

<section id="info">
Enter a list of valuse or text to sort in the textbox to the right. <br/> <br/>

<ul>
<li>Value sort will sort each individual line.</li> <br/>
<li>Text sort will sort each individual word.</li>
</ul>

</section>

<form action="hw7.php" method="post">
<section id="items">
<textarea rows="20" cols="40" name="stuff" >
</textarea> <br/>
<input type="submit" name="submit" value="Sort!">
</section>
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

PHP_EOL是一个常量。如果将其封装在引号中,它将变为字符串。所以在你的爆炸()中尝试这个。

$wordlist = explode(PHP_EOL, $text);