Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 disapear but i am left with the coloumn 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>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading