0

In this example i am reading in data from a text file and then I am outputting the data bases on whether the data is from a male or a female into a table. I am having trouble with the line: echo " $male_votes['class-int']"; I am receiving a parse error. I am just trying to access the first element in the array $male_votes with is named "class-int".

<?php
//Keep track of male votes
$male_votes =array("class-int" => 0, "class-gui" => 0, "class-net" => 0, "class-oop"=> 0);
//Keep track of the female votes
$female_votes = array("class-int" => 0, "class-gui" => 0, "class-net" => 0, "class-oop" => 0);

//Open the file for reading
$myfile = fopen("results.txt", "r") or die("Unable to open file!");

echo "<table border='1'>";
echo "<tr> <th> <strong> Class </strong> </th>";
echo "<th> <strong> Males </strong> </th>";
echo "<th> <strong> Females <strong> </th>";
echo "<th> <strong> Total </strong> </th> </tr>";
echo "<tr> <td> Internet Development </td>";

// Output one line until end-of-file
while(!feof($myfile)) {
   //Read a single line from the file
   $line = fgets($myfile);

   //Make sure we didn't read an empty line
   if(strlen($line) >0)
   {
       $class = substr($line,0,9);
       $gender = substr($line,10,1);
       echo "$class<br>";
       if($gender == 'M')
       {
            $male_votes[$class]++;
       }
       else
       {
           $female_votes[$class]++;
       }
   }
}
echo "<td> $male_votes['class-int']</td>";

// echo"<td> $male_votes[$class] </td>";
// echo"<td> $female_votes[$class]</td>";
// echo "<td> $male_votes[$class] + $female_votes[$class]";

fclose($myfile);
?>

1 Answer 1

1

Try like this:

echo "<td> ${male_votes['class-int']}</td>";

Or like this:

echo "<td> {$male_votes['class-int']}</td>";

Or like this:

echo '<td> '.$male_votes['class-int'].'</td>';
Sign up to request clarification or add additional context in comments.

3 Comments

How would I add two of my arrays to make a total. I want to add my male votes and female votes.
Depends on if You want to add them together or write them separatedly.
This would sum both.. echo '<td>'.($male_votes['class-int'] + $female_votes['class-int']).'</td>'; - IF You konw that votes are actually numbers/numerics/integers. ... and if You want to add votes one after another, than just leave a blank space within echoed string or separator of Your choice, than write in female votes, just as male votes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.