-
how can I use php to display question
about 9 years ago
-
about 9 years ago
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
Thanks
-
about 9 years ago
HI
Can you tell me your connection.php code ?
Thanks
-
-
about 9 years ago
HI
Can you tell me the database structure how you made the tables and columns so that I can tell you in a better way , that how to display the questions and its four options
Thanks
-
-
about 9 years ago
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
-
-
about 9 years ago
<?php $link = mysql_connect("localhost","root",""); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); echo"<br>"; } $db = mysql_select_db("test"); if(!$db){ die("No database found".mysql_error()); } ?>
this's my code of connection
-
-
about 9 years ago
<?php function getQuestions() { require("connection.php"); $questions = array(); $query = mysql_query("SELECT * FROM question ")or dir(mysql_error()); if (mysql_num_rows($query) > 0) { while ($fetch = mysql_fetch_assoc($query)) { $questions[] = $fetch; } } return $questions; } getQuestions() ; ?>
i tried this code but it's not working
-
-
about 9 years ago
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 1Now 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<?php function getQuestions() { global $con; $questions = array(); $query - mysqli_query($con, "SELECT * FROM `table_name`"); if (mysqli_num_rows($query) > 0) { while ($fetch = mysqli_fetch_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 PagePlease try this and let me know if you find any issues or if found helpful please mark correct.
Enjoy :)
-
-
about 9 years ago
===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
-
-
about 9 years ago
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.
-
9 Answer(s)