PHP realizes the effect of similar question bank
Hello everyone, I think someone is back. I learned a little bit about PHP recently, and then I want to write a simple example to try. So I wrote something similar to extracting questions from the question bank, which is to input the number of questions to be extracted first, and then randomly extract questions from the database.
I hope you guys spray lightly.
Suppose I have such a question bank now:
Huh? Why use English? Because I didn't solve the coding problem.
Then I'll take a question:
In this way, three questions are randomly selected.
Now let's talk about my thinking. I hope you guys will give me some advice.
First of all, to realize this function, I need three pages, one is the user input page input.html, one is the background processing page select.php, and the other is the error warning page error.html (if the user input is empty, or the number of selected questions exceeds the number of question banks, the error will be reported). Then the database is divided into two columns, one is the question , which is used to store the questions. The other column is id, which is used to identify the question.
Then judge the user's input:
input.html page:
<html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <form action="select.php" method="get"> //Please input the number of questions to be randomly generated: < input type = "text" name = "input" / > <input type="submit" name="Submit" value="Submission" /> </form> </html>
error.html page:
<html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <p>Your input is wrong, please input again! </p> </html>
select.php page:
<?php header("Content-type:text/html;charset=utf8"); $connect=new mysqli('localhost','wy','000000','test'); if(!$connect){ die("Database connection failed!"); } //Connect to database $sql="select id from test"; $result=$connect->query($sql); $array=array(); $i=0; while($row=$result->fetch_row()){ foreach($row as $val){ $array[$i]=$val; $i ++; } } //Store the topic id in an array $input=$_GET['input']; //Accept user input if(empty($input) || $input>count($array)){ //Judge whether the user's input is empty or the input is greater than the number of question banks header("Location:error.html"); //Redirect input error to error prompt page }else{ shuffle($array); //Random sorting of the array containing the topic id $k=0; //id used to retrieve the question in the rand array for($j=0;$j<$input;$j++){ $rand=array_slice($array,0,$input); //Starting from the first number of the array, take the number of user input IDs and store them in an array $sql2="select * from test where id='{$rand[$k]}'"; //Find the question corresponding to each id in the rand array $result2=$connect->query($sql2); //Store the result set returned by mysql statement while($row2=$result2->fetch_assoc()){ echo $row2['question']; //Return the content corresponding to the question echo '<br />'; } $k++; } $result2->free(); //Free memory } $result->free(); //Free memory $connect->close(); //Close connection ?>
Everybody, if you don't mind, I just want some traffic( Personal blog).