简体   繁体   中英

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.

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...

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()

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.

$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

$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.

$Scores = file("scores.txt");
$Poss = file("Poss.txt");

and then use the first foreach loop to process these 2 arrays

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM