dimanche 1 mars 2020

How to output the right question number from mysql database for my quiz ? %d in php?

Hi there I am creating a quiz app and I have been very greatful for the help I've been getting on here.

It really is so great, so thanks to everyone for their help.

So I have been stumbling across small problems as does every programmer.

The current problem I have is that the wrong values for the question number in my quiz is outputted. I keep getting question number 0 out of 0.

I am unsure of how to fix this. Although, I have a suspicion it is on this line :

Question

Mysql Question Table

Any help would be appreciated as always. Thank you as always.

#Question.php
<?php

    session_start();
    if( !isset( $_SESSION['score'] ) )$_SESSION['score']=0;
    $number = isset( $_GET['n'] ) && is_numeric( $_GET['n'] ) ? intval( $_GET['n'] ) : 1;


    include "database.php"; 



    $sql="select
            ( select count(*) from `questions` ) as `total`,
            q.`question_number` as `qid`, 
            q.`text` as `question`, 
            c.`id` as `aid`, 
            c.`text` as `answer` 
        from `questions` q
            join `choices` c on c.`question_number`=q.`question_number`
        where q.`question_number` = ?";
    $stmt=$conn->prepare( $sql );

    if( $stmt ){
        $stmt->bind_param('i',$number);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result( $total, $qid, $question, $aid, $answer );
    }
?>
<!DOCTYPE html PUBLIC'>
<html> 
    <head>
        <meta charset="utf-8" />
        <title>General Knowledge Quiz</title>
        <link href="css/other.css" rel="stylesheet" />
    </head>
    <body>
        <main>
            <div class="container" >

                <div class="current">Question <?php printf('%d of %d',$qid,$total); ?></div>
                <p class="question"><?php echo $question; ?></p>

                <form action="process.php" method="post">
                    <ul class="choices">
                    <?php
                        if( $stmt ){
                            while( $rs=$stmt->fetch() ){
                                printf('
                                    <li>
                                        <input type="radio" name="choice" value="%d" />%s
                                    </li>',
                                    $aid,
                                    $answer
                                );
                            }
                        }
                    ?>
                    </ul>
                    <input type="submit" name="submit" value="Sumbit" />
                    <input type="hidden" name="number" value="<?php echo $number; ?>" />
                </form>
            </div>
        </main>
    </body>
#Process.php
<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        if( isset( $_POST['number'], $_POST['choice'] ) && is_numeric( $_POST['number'] ) ){

            session_start();
            include "database.php";

            if( !isset( $_SESSION['score'] ) ) $_SESSION['score'] = 0;
            $choice=filter_input( INPUT_POST, 'choice', FILTER_SANITIZE_NUMBER_INT );
            $number=filter_input( INPUT_POST, 'number', FILTER_SANITIZE_NUMBER_INT );


            $sql='select 
                ( select count(*) from questions ) as `total`,
                `id`
                from `choices`
                where `question_number`=? and is_correct=1;';
            $stmt=$conn->prepare( $sql );
            if( $stmt ){
                $stmt->bind_param('i',$number);
                $res=$stmt->execute();
                if( $res ){
                    $stmt->store_result();
                    $stmt->bind_result($total,$id);

                    $stmt->fetch();
                    if( $id==$choice )$_SESSION['score']++;
                    $stmt->free_result();
                    $stmt->close();


                    if( $number==$total )exit( header('Location: final.php') );
                    else exit( header( sprintf('Location: question.php?n=%s',( $number + 1 ) ) ) );
                }
            }
        }
        exit();
    }
    http_response_code(400);
?>



Aucun commentaire:

Enregistrer un commentaire