2016-05-14 73 views
-2

我正在開發一個應用程序,它有一個問題庫,通過它我想隨機生成問題論文。我正在使用PHP和HTML。我使用html作爲我的前端和後端PHP。我已開發的代碼,但是有一個問題,它,它產生4 textarea的2個問題,我無法調試它使用PHP生成隨機數據

<?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 

} 

?>  
+0

你可以從mysql中隨機獲取數據。如果你想隨機使用PHP,那麼試試rand函數。 –

+0

您想讓我們爲您寫信還是猜測? –

+0

呃..Buddy你想要一些像現成的麪包,爲你的早餐? –

回答

0

使用功能,像你的數據庫這個&拉數據:

當過你怎麼稱呼它,如:UniqueRandomNumbersWithinRange(0,25,5)

你會得到混亂的數字$min之間的陣列$quantity$max

從後端只需按照數組順序提取問題。

1
<?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 
} 
?>