I’m going to apologize in advance. This is probably a very stupid question, but I can’t figure it out. What I’m trying to do is to use a radio button selection to look through a file and print out the things that align with the radio selection. Unfortunately, all it does is print the entire thing at once. How do I fix this?
<section id="leftcol"> <h1> Select Grade to List:</h1> <form action="hwk9.php" method="post"> <input type="radio" name="l_grade" value="A"> A <br /> <input type="radio" name="l_grade" value="B"> B <br /> <input type="radio" name="l_grade" value="C"> C <br /> <input type="radio" name="l_grade" value="D"> D <br /> <input type="radio" name="l_grade" value="F"> F <br /> <input type="submit" name="submit" value="Submit"> </form> </section> <section id="rightcol"> <? $submit=$_POST['submit']; if (isset($submit)) { $l_grades=$_POST['l_grade']; $names=file("student.names.txt"); $grades=file("student.grades.txt"); $names=explode(PHP_EOL, $names); $grades=explode(PHP_EOL, $grades); print "<table> <th> Name </th> <th> Grade </th>"; $i = 1; if ($l_grades="A" and $grades >= '90') { foreach ($names as $i=>$name) { print "<tr> <td> $names </td> <td> $grades </td> </tr>"; } }
I only included one of the smaller conditionals because they all look the same, seeing as I copy/pasted them and only changed the bits that needed to be changed.
Advertisement
Answer
You are on the right track. You just missed on = sign between $l_grades and “A”. Since it is $l_grades=”A” it is assigning the value “A” to $l_grades so it is always true. Try using:
if ($l_grades=="A" and $grades >= '90') { foreach ($names as $i=>$name) { print "<tr> <td> $names </td> <td> $grades </td> </tr>"; } }