使用PHP生成随机数据


Using PHP to generate random data

我正在开发一个应用程序,该应用程序有一个题库,我想通过它随机生成一份试卷。我使用的是PHP和HTML。我使用html作为我的前端和后端PHP。我已经开发了代码,但它有一个问题,它为一个问题生成了4个文本区域2,我无法调试它

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "rgpv";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ques FROM bank WHERE sub='$_POST[sub]' ORDER BY RAND() LIMIT 2";
$result = $conn->query($sql);

$result = $conn->query($sql);
    // output data of each row
    while($row = $result->fetch_assoc()) {

        ?>
<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
    <title>Login Form</title>
      <!-- Calling CSS -->
      <link rel="stylesheet" href="css/reset.css">
      <link rel='stylesheet prefetch' href='http://fonts.googleapis.com/css?family=Roboto:400,100,300,500,700,900|RobotoDraft:400,100,300,500,700,900'>
      <!--File for fonts-->
      <link rel='stylesheet prefetch' href='css/bootstrap.css'>
      <link rel='stylesheet prefetch' href='css/font-awesome.min.css'>
      <link rel="stylesheet" href="css/style.css">
  </head> <!-- end Head -->
  <body background="images/journal.jpg">
    <!-- Mixins-->
      <!-- Form Title-->
    <div class="pen-title">
      <h1>University Institute of Technology- RGPV</h1><h2>Random Question Paper Generator Portal</h2>
</div>

       <fieldset class="form-group">
    <label for="exampleTextarea">Question 1</label>
    <textarea class="form-control txt" id="name" rows="3" value="" disabled><?php echo $row["ques"]; ?></textarea>
  </fieldset>

       <fieldset class="form-group">
    <label for="exampleTextarea">Question 2</label>
    <textarea class="form-control txt" id="name" rows="3" value="" disabled><?php echo $row["ques"]; ?></textarea>
  </fieldset>

    </body>
</html>
<?php
}
?>     
<?php
function get_question()
{
  // Connect to your database storing your questions
  $connection = new mysqli("hostname", "username", "password", "database");
  if($connection->connect_errno)
  {
    die("Error connecting to database.");
  }
  // Load all your questions from your database into an array
  $query = "SELECT * FROM questions";
  $result = $connection->query($query);
  $questions = $result->fetch_all();
  // Randomly select a question from your array to output
  $number = rand(0, count($questions) - 1);
  return $questions[$number];
}
$question = get_question();
// Use var_dump() to view the raw output from your database
// You can now output your question and format it however you'd like
var_dump($question);
// You can call this function as many times as you'd like using a loop
for($i = 0; $i < 10; $i++)
{
    $question = get_question();
    // Output your question properly formatted here
}
?>

使用这样的函数&从数据库中提取数据:

什么时候你这样称呼它:UniqueRandomNumbersWithinRange(0,25,5)

您将得到一个由$min$max之间的混乱数字组成的数组$quantity

只需从后端按数组顺序提取问题。