Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • how can I use php to display question

    • 0
    • 0
    • 0
    • 9
    • 0
    • 0
    • 0
    • 544
    Answer it

    how can I use php to display question stored in mysql database and choose the right answer for it and give grade if the answer is right and store it back in mysql database (sorry I don't speak English very well )

 9 Answer(s)

  • Hi

    Please assign the function return array to $questions variable and then use this variable

    $questions = getQuestions();

    // loop this $questions in foreach like this below

    if ( ! empty ( $questions ) )
        $i = 1;
        echo "<table width='100%'>";
        foreach ( $questions as $qstn )
            echo "<tr><td>Q- " . $i . "  " . $qstn[ "question" ] . "</td></tr>";
            echo "<tr><td>a . " . $qstn[ "a" ] . "</td></tr>";
            echo "<tr><td>b . " . $qstn[ "b" ] . "</td></tr>";
            $i ++;
        echo "</table>";

    I have just display the questions and its options in the table, you can use radio buttons for the options in the code. Please let me know , if you face any issue

  • thank you man it's working i appreciate your efforts now i have to add radio buttons but if you still could help with another solution how to add true solution and hide it to compare it with the answer you give and tell the student whether his answer wrong or true and give him grade

  •     <?php 
    $link = mysql&#95;connect("localhost","root",""); 
    if (!$link) { 
        die('Could not connect to MySQL: ' . mysql&#95;error()); echo"<br>"; 
     $db = mysql&#95;select&#95;db("test");
        die("No database found".mysql&#95;error());

    this's my code of connection

  • <?php  function getQuestions()  {  require("connection.php"); $questions
    = array();  $query = mysql&#95;query("SELECT * FROM  question ")or dir(mysql&#95;error());  if (mysql&#95;num&#95;rows($query) > 0)  {  while ($fetch = mysql&#95;fetch&#95;assoc($query))  {   $questions[] = $fetch;  }   }   return $questions; 
         }   getQuestions() ;

    i tried this code but it's not working

  • HI

    Thanks for explaining database structure
    As I can see that you have created five fields, id, question, ans1, ans2 and TrueAns. I suggest you to change your database structure to perform better, you can change question field datatype to text, change ans1 field to a datatype text, change ans2 field to b datatype text and change TrueAns field to ca (which means correct answer) field datatype varchar 1

    Now you will have these columns id, question, a,b,ca among these columns a,b are the fields which will contain answers. ca will contain correct answers options like a or b.

    Now suppose you have one record inserted question = Who is the current PM of India? a = Narendra Modi b = Jawahar Lal Nehru ca = a

    when you will display the questions in the form , you will need to create a database connection and run select query and then fetch records in while loop.
    I am writing a function and a query through which you can fetch the questions and its options

        function getQuestions()
            global $con;
            $questions = array();
           $query - mysqli&#95;query($con, "SELECT * FROM `table&#95;name`");
             if (mysqli&#95;num&#95;rows($query) > 0)
              while ($fetch = mysqli&#95;fetch&#95;assoc($query))
               $questions[] = $fetch;
              return $questions;

    In above code please replace _ with underscore _. Now in your view Page
    write the code and call the function under php tags

    $questions = getQuestions();
    // here you can loop a $questions and render the fields in your Html Page

    Please try this and let me know if you find any issues or if found helpful please mark correct.

    Enjoy :)

  • ===Database test

    == Table structure for table question

    |------ |Column|Type|Null|Default |------ |id|int(3)|No| |question|varchar(100)|No| |ans1|varchar(50)|No| |ans2|varchar(50)|No| |TrueAns|varchar(30)|No| == Dumping data for table question

    |10|how we define variables in php language ?|#s|$S|$s == Table structure for table question

    this is my database where i have three fields the first and the second is for the answering and the third one is for the true answer so you can compare you what choose with the correct answer

  • Hi ,

    I'd suggest 3 data tables in your database: students, tests, and scores.

    Each student needs to have fields for an ID and whatever else (name, dob, etc) you want to record about them. Tests should have fields for an ID and whatever else (name, date, weight, etc). Scores should have the student ID, a test ID, and the score (any anything else).

    This means you can query a student and join with the scores table to get all the student's scores. You can also join the test table these results to get labels put onto each score and calculate a grade based on scores and weight.

    Alternately you can query for a test and join with the scores to get all the scores on a given test to get the class stats.

    Happy coding.

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: