使用foreach循环将数组打印到表中.html / php


Printing an array into a table using a foreach loop. html/php

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

条目以一个新的行字符/返回字符结束,条目的数量应被计算并显示在表格中。

我相信我的问题是在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>

PHP_EOL是一个常量。如果你把它用引号括起来,它就变成了一个字符串。所以在你的explosion()中试试这个。

$wordlist = explode(PHP_EOL, $text);