[英]PHP : multiple arrays into html table
I have this code where I want to submit two arrays into a table in html. It works fine until I try and add the other array into the table.我有这段代码,我想将两个 arrays 提交到 html 中的一个表中。它工作正常,直到我尝试将另一个数组添加到表中。
enter image description here this is what it looks like before I add the next array which I want to display in the possible scores column.在此处输入图像描述这是我添加下一个要在可能的分数列中显示的数组之前的样子。 After I add the new array all of the rows disappear, but I am left with the column headers.
添加新数组后,所有行都消失了,但剩下列标题。 I would like to keep the same format of what I have going on so far if possible.
如果可能的话,我想保持与目前所做的相同的格式。
<!DOCTYPE html>
<html>
<body>
<table border="1">
<th></th><th>Student Score</th><th>Possible Score</th><th>Percentage</th>
<?php
//Scores to table
$Scores = fopen("scores.txt", "r");
$Poss = fopen("Poss.txt", "r");
$ind = 0;
while(!feof($Scores)) {
$Scoresarray[$ind] = fgets($Scores);
$ind++;
}
while(!feof($Poss)) {
$Possarray[$ind] = fgets($Poss);
$ind++;
}
fclose($Poss);
fclose($Scores);
if(sizeof($Scoresarray,$Possarray)>1){
$i=1;
while($i<sizeof($Scoresarray,$Possarray)){
echo "<tr>
<td>".$i."</td>
<td>".$Scoresarray[$i-1]."</td>
<td>".$i."</td>
<td>".$Possarray[$i-1]."</td>
</tr>";
$i++;
}
}
?>
</table>
</body>
</html>
Assuming the 2 arrays are the same length you can do...假设 2 arrays 的长度相同,你可以做......
All the counters can be removed, they are not needed, the sizeof()
will only count one array at a time and is a alias for the count()
function anyway so I used count()
所有计数器都可以删除,不需要它们,
sizeof()
一次只会计算一个数组,并且是count()
function 的别名,所以我使用了count()
If you use a foreach()
to get the occurance and the index of an array all in one go like this foreach ($Scoresarray as $i => $score ){
you can traverse one of the arrays and using the index get the corresponding occurance from the other array as well.如果你使用
foreach()
来获取一个数组的出现和索引,就像这样foreach ($Scoresarray as $i => $score ){
你可以遍历 arrays 中的一个并使用索引获取相应的其他阵列的出现也是如此。
$Scores = fopen("scores.txt", "r");
$Poss = fopen("Poss.txt", "r");
while(!feof($Scores)) {
$Scoresarray[] = fgets($Scores);
}
while(!feof($Poss)) {
$Possarray[] = fgets($Poss);
}
fclose($Poss);
fclose($Scores);
if( count($Scoresarray) > 1 && count($Possarray) > 1){
foreach ($Scoresarray as $i => $score ){
echo "<tr>
<td>".$i."</td>
<td>".$score."</td>
<td>".$i."</td>
<td>".$Possarray[$i]."</td>
</tr>";
}
}
Again assuming the files are the same length you could build an Assoc array of the values so the processing of the result becomes even easier再次假设文件的长度相同,您可以构建一个 Assoc 值数组,这样结果的处理就变得更加容易
$Scores = fopen("scores.txt", "r");
$Poss = fopen("Poss.txt", "r");
while(!feof($Scores)) {
$scoreAndPos[] = ['score' => fgets($Scores), 'Poss' =>fgets($Poss)];
}
foreach ($scoreAndPos as $i => $sp ){
echo "<tr>
<td>".$i."</td>
<td>".$sp['score']."</td>
<td>".$i."</td>
<td>".$sp['poss']."</td>
</tr>";
}
Or you could use file()
which read a whole file into an array of lines.或者您可以使用
file()
将整个文件读入行数组。
$Scores = file("scores.txt");
$Poss = file("Poss.txt");
and then use the first foreach loop to process these 2 arrays然后使用第一个 foreach 循环来处理这些 2 arrays
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.