1

Using this code how can you make it only echo out the radio or checkbox if you have a b c d answers or a b answers. On my quiz its going to be a b c d and a (true) b (false) answers. To finish my quiz this is the only thing stopping me from finishing it. My question might not be to the point but I tried to get it ask close to what I did done. Thanks for any help.

<?php
    //retreive questions from database and put into question box

    $query2 = "SELECT `QuestionId`, `Question`, `Opt1`, `Opt2`, `Opt3`, `Opt4`,`Answer` FROM `pf_questions`";

    $question2 = mysql_query($query2);

    while($row = mysql_fetch_array($question2)){

        $id         = $row['QuestionId'];
        $question   = $row['Question'];
        $opt1       = $row['Opt1'];
        $opt2       = $row['Opt2'];
        $opt3       = $row['Opt3'];
        $opt4       = $row['Opt4'];
        $answer     = $row["Answer"];




    ?>
    <div id="ContainerQuestion">
        <span class="Question">Question <?php echo $id; ?>. <?php echo $question; ?></span>

            <p><input type=radio name='q<?php echo $id; ?>' <?=( $answer  == 'a')?("checked='checked'"):(""); ?>  value="a"> <?php echo $opt1; ?> </p>
            <p><input type=radio name='q<?php echo $id; ?>' <?=( $answer  == 'b')?("checked='checked'"):(""); ?> value="b"> <?php echo $opt2; ?> </p>
            <p><input type=radio name='q<?php echo $id; ?>' <?=( $answer  == 'c')?("checked='checked'"):(""); ?> value="c"> <?php echo $opt3; ?> </p>

            <p><input type=radio name='q<?php echo $id; ?>' <?=( $answer  == 'd')?("checked='checked'"):(""); ?> value="d"> <?php echo $opt4; ?> </p>


    </div>
    <?php
    }

    ?>
  • 4
    Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the *[red box](http://goo.gl/GPmFd)*? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). Also see [Why shouldn't I use `mysql_*` functions in PHP?](http://stackoverflow.com/q/12859942/871050) – Madara's Ghost Oct 12 '12 at 22:11
  • Are you trying to show only two radio buttons in the case that Opt3 and Opt4 fields are blank? – jedwards Oct 12 '12 at 22:18
  • @jedwards yes if the question has an a b c d then I want it to show the question with the four choices and if the question has a b then no c d should show. So yes in your question about the Opt3 & Opt4 field are blank. – Agustin Leyva Oct 12 '12 at 22:36
  • @MadaraUchiha Thats a great point, but it's worth offering him help with solving this query/logic before introducing broader DB abstraction. Hopefully he'll take it on board! – nickhar Oct 12 '12 at 23:06
  • Ok update ... The radio is works fine with the help from everyone that gave me input. Thanks! – Agustin Leyva Oct 12 '12 at 23:08
  • @nickhar: mysqli isn't necessary "broader database abstraction", it's called "common sense". – Madara's Ghost Oct 12 '12 at 23:11
  • @MadaraUchiha But it's only common sense and a better option to many when they've seen the other side of the fence! I think we're violently agreeing that it's the right way! – nickhar Oct 12 '12 at 23:17

2 Answers2

0

I think I understand what you're asking. you want to wrap each line in an "if" clause. For example:

<?php if( isset ($opt3) ) { ?>

<p><input type=radio name='q<?php echo $id; ?>' <?=( $answer  == 'c')?("checked='checked'"):(""); ?> value="c"> <?php echo $opt3; ?> </p>

<?php } 
      if( isset ($opt4) ) { ?>

<p><input type=radio name='q<?php echo $id; ?>' <?=( $answer  == 'd')?("checked='checked'"):(""); ?> value="d"> <?php echo $opt4; ?> </p>

<?php } ?>

somewhat ugly hack but it should work and you get the idea.

Joe T
  • 2,300
  • 1
  • 19
  • 31
  • An empty string is not null, and should return `TRUE` when passed to `isset()`. – jedwards Oct 12 '12 at 22:21
  • I made the assumption that a NULL Field in his database should be what he has for these cases. Otherwise you can add `&& ($opt3)` inside the if statements to make sure not blank. – Joe T Oct 12 '12 at 22:23
  • @user16081 I will try and also would like to Thank everyone for your input on my post. – Agustin Leyva Oct 12 '12 at 22:39
0

I'd probably replace your while loop with something like the following (untested):

$answerFields = array('Opt1'=>'a', 'Opt2'=>'b', 'Opt3'=>'c', 'Opt4'=>'d');

while($row = mysql_fetch_array($question2)){
    $id         = $row['QuestionId'];
    $question   = $row['Question'];

    // Print Question
    printf('<div id="ContainerQuestion">');
    printf('<span class="Question">Question %s. %s</span>', $id, $question);

    // Print Answers
    foreach($answerFields as $field=>$ans)
    {
        if(array_key_exists($field, $row) && $row[$field])
        {
            $checked = ($row["Answer"] == $ans) ? 'checked' : '';
            printf('<p><input type="radio" name="%s" %s value="%s">%s</p>', 
                $id, $checked, $ans, $row[$field]);
        }
    }
}

If the answer field in your database (Opt1, Opt, etc.) is not set, or an empty string, the <p> containing the response shouldn't be printed.

This solution allows you to extend your application to cases where 3/4 responses are filled and 1/4 is empty. It also cleans up your code a bit by using printf statements instead of hopping between inline php and html.

Lastly, obligatory note pointing out the important comment about prepared statements.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
jedwards
  • 29,432
  • 3
  • 65
  • 92
  • Thanks for the help. Also how can I make it where when they submit it; it sends the results to my email with the question and there answer and the right answer. Like example: Joe Doe quiz results 1. Question - Answer: A. true or C. the guy coding it - Right Answer: B. false or C: the guy coding it. and maybe a you got 10/20 right. If you know what I mean. – Agustin Leyva Oct 12 '12 at 23:06
  • Oh man, thats a completely separate question. If this worked, let us know, and consider looking into the [`mail()`](http://php.net/manual/en/function.mail.php) function, and creating a separate question here if you run into trouble. – jedwards Oct 12 '12 at 23:08
  • Thanks for the help using your help got it to function right Thanks again. I will look into it and try something out before I ask about this agian " Also how can I make it where when they submit it; it sends the results to my email with the question and there answer and the right answer. Like example: Joe Doe quiz results 1. Question - Answer: A. true or C. the guy coding it - Right Answer: B. false or C: the guy coding it. and maybe a you got 10/20 right. If you know what I mean." – Agustin Leyva Oct 12 '12 at 23:12
  • I almost forgot to ask this question in my post earlier couldn't get the
    to work with the coding above.
    – Agustin Leyva Oct 12 '12 at 23:17
  • Sorry, what? What do you want/ what are you trying to do? – jedwards Oct 12 '12 at 23:19
  • when your done with the quiz and then you hit the submit button it take you somewhere. Meaning sending the email. – Agustin Leyva Oct 12 '12 at 23:21
  • Right, you're looking for something like: `
    `. The action is the filename of the form handler and the method is either "post" or "get", depending on how your handler is written.
    – jedwards Oct 12 '12 at 23:22
  • how would I add the
    and the in the above code because I getting errors
    – Agustin Leyva Oct 12 '12 at 23:41
  • Wrap all the inputs in a `
    ` and `
    ` tag, including your `` button.
    – jedwards Oct 12 '12 at 23:42
  • Ok thanks again for your help. I got the submit button on without any errors. – Agustin Leyva Oct 12 '12 at 23:52