how to include php in javascript for retriving data from database to form a paper with MCQ questions -
<?php $server = "localhost"; $username = ""; $password = ""; $db = "test"; $conn = new mysqli($server,$username,$password,$db); if($conn->error){ die("error :- ". $conn->error); } $query = "select * mcq_questions"; $result = $conn->query($query); ?> <script type="text/javascript"> var n = <?php echo $result->num_rows ?>; var i; for(i = 0; < n; i++){ var question = ""; question = '<?php $row = $result->fetch_assoc(); echo $row['question']; ?>'; document.write(question); } </script>
i wanted print mcq questions present in database along multiple options, problem when doing first question being printed n number of time.... can 1 me this.
you need iterate inside php , print out string js array question
variable
<script type="text/javascript"> var n = <?php echo $result->num_rows ?>; var i; for(i = 0; < n; i++){ var question = ""; question = '<?php while($row = $result->fetch_assoc()) { echo $row['question'] . ',' } ?>'; document.write(question); } </script>
look @ explanation: http://www.w3schools.com/php/php_mysql_select.asp
Comments
Post a Comment