Jump to content

Can someone help me with this code??


Beauford

Recommended Posts

2 weeks of screwing around with this and I just can't see what I am doing wrong. I have a trivia  script which produces a question and multiple answers taken from a MySQL database and for the most part it works perfect.

 

This is some example source from the page, which is correct.

 

<br><br>32.)   This is question 32?<br>

<br><input type=radio name='a32' value='A'>Answer A

<br><input type=radio name='a32' value='B'>Answer B

<br><input type=radio name='a32' value='C'>Answer C

<br><input type=radio name='a32' value='D'>Answer D

 

This is what I am trying to do. If the user clicks on submit on the form, I want the choices that they chose to be checked when the page refreshes - so if the user chooses B it will be checked - Like below.

 

<br><br>32.)   This is question 32?<br>

<br><input type=radio name='a32' value='A'>Answer A

<br><input type=radio name='a32' value='B' checked>Answer B

<br><input type=radio name='a32' value='C'>Answer C

<br><input type=radio name='a32' value='D'>Answer D

 

No matter what I have tried, it wants to check them all, and as the last answer is the last one in the loop, it always shows as checked.

 

<br><br>32.)   This is question 32?<br>

<br><input type=radio name='a32' value='A' checked>Answer A

<br><input type=radio name='a32' value='B' checked>Answer B

<br><input type=radio name='a32' value='C' checked>Answer C

<br><input type=radio name='a32' value='D' checked>Answer D

 

This is the exact line for line code I am using, and for the life of me I just can't see why it is doing the above. I have gone over the logic of the code 1000 times, and when I echo the variables they show what I am expecting, but the end result is the above, so obviously I am just missing something.

 

Any help is greatly appreciated.

 

$result = "select questions, questions.idq, GROUP_CONCAT(answers.answers SEPARATOR '|') as answers, GROUP_CONCAT(answers.letters SEPARATOR '|') as letters from questions join reference join answers where reference.idq=questions.idq and reference.ida=answers.ida GROUP BY questions order by idq";

$query = mysql_query ($result)  or $mysqlerror = mysql_error();

if(!$mysqlerror) {

  while($row = @mysql_fetch_array($query, MYSQL_ASSOC)) {

 

echo "<br><br>".$row['idq'].".)   ".$row['questions']."<br>\n";

$asplit = explode("|", $row['answers']);

$lsplit = explode("|", $row['letters']);

 

$postvalue = "a".$row['idq'];

 

for($i = 0; $i < count($asplit); $i++) {

 

$checked = "<br><input type=radio name='a".$row['idq']."' value='".$lsplit[$i]."' checked>".$asplit[$i]."\n";

$unchecked = "<br><input type=radio name='a".$row['idq']."' value='".$lsplit[$i]."'>".$asplit[$i]."\n";

 

switch($_POST[$postvalue]) {

 

case "A": echo $checked; break;

case "B": echo $checked; break;

case "C": echo $checked; break;

case "D": echo $checked; break;

case "E": echo $checked; break;

default: echo $unchecked;

}

}

}

}

Link to comment
https://forums.phpfreaks.com/topic/97322-can-someone-help-me-with-this-code/
Share on other sites

What happens if you replace the contents of the for loop with this?

 

if($_POST[$postvalue] == $lsplit[$i]) {

            echo "
<input type=radio name='a".$row['idq']."' value='".$lsplit[$i]."' checked>".$asplit[$i]."\n";

} else {

            echo "
<input type=radio name='a".$row['idq']."' value='".$lsplit[$i]."'>".$asplit[$i]."\n";

}

Change:

         for($i = 0; $i < count($asplit); $i++) {

            $checked = "
<input type=radio name='a".$row['idq']."' value='".$lsplit[$i]."' checked>".$asplit[$i]."\n";
            $unchecked = "
<input type=radio name='a".$row['idq']."' value='".$lsplit[$i]."'>".$asplit[$i]."\n";

            switch($_POST[$postvalue]) {
            
               case "A": echo $checked; break;
               case "B": echo $checked; break;
               case "C": echo $checked; break;
               case "D": echo $checked; break;
               case "E": echo $checked; break;
               default: echo $unchecked;
            } 

to:

         for($i = 0; $i < count($asplit); $i++)
         {
            $key = 'a'.$row['idq'];
            $checked = ($_POST[$key] == $lsplit[$i]) ? ' checked="checked"' : null;
            echo '<input type="radio" name="'.$key.'" value="' . $lsplit[$i] . '"' . $checked . '>' . $asplit[$i] . "\n";
         }

Ok wildteen88, I owe you my first born .

 

But just so I understand this.

 

$checked = ($_POST[$key] == $lsplit[$i]) ? ' checked="checked"' : null;

 

So if these match $_POST[$key] == $lsplit[$i] then $checked will equal checked="checked", if not it will equal null.

 

Also, I'm just wondering too why you have checked="checked" as just ' checked' works fine as well.

 

Thanks again, and to Boom for his sugestion.

Also, I'm just wondering too why you have checked="checked" as just ' checked' works fine as well.

 

Because checked="checked" is the correct HTML code that will work with all browers, whereas just 'checked' only works with sloppy implementations of the html standard :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.