danflo Posted November 19, 2008 Share Posted November 19, 2008 Hello, new to the forum need some advices regarding a application. Need to write a application that connects to the MYSQL database, pulls a couple of random question from there. The user answers the question via a form with checkboxes he must be able to select multiple options or none. <html> <head> <title>Examen BD</title> </head> <body> <form method="post" action="test.php"> <?php include 'config.php'; $link = mysql_connect(HOST_BD, USER_BD, PAROLA_BD); if(!$link) { die('Nu ne putem conecta la server: ' . mysql_error()); } $db = mysql_select_db(BD); if(!$db) { die("Baza de date nu poate fi selectata!"); } $random_rows = array(); $corect_answer = array(); $test = array(); $query = mysql_query("SELECT * FROM questions ORDER BY RAND() LIMIT 3"); while($row = mysql_fetch_array($query)) { $random_rows[] = $row; echo '<table width="99%" align="center">'; echo '<tr>'; echo '<td>'; echo '<p style="font: bold 16px Verdana, Arial, Helvetica, sans-serif; color: #99CC00; border-bottom: 1px dashed #E6E8ED;"> Question '.$row[0].' : '.$row[1].'</p>'; echo '</td>'; echo '</tr>'; echo '<tr><td><p style="font: bold 12px Verdana, Arial, Helvetica, sans-serif;"><input type="checkbox" name="question'.$row[0]."[]".'" value="a"> '.$row[2].'</p></td></tr>'; echo '<tr><td><p style="font: bold 12px Verdana, Arial, Helvetica, sans-serif;"><input type="checkbox" name="question'.$row[0]."[]".'" value="b"> '.$row[3].'</p></td></tr>'; echo '<tr><td><p style="font: bold 12px Verdana, Arial, Helvetica, sans-serif;"><input type="checkbox" name="question'.$row[0]."[]".'" value="c"> '.$row[4].'</p></td></tr>'; echo '</table>'; $corect_answer[] = $row[5]; $test[] = "question".$row[0]; } ?> <input type="submit" value="Submit" /> </form> OK so now the questions. After i submit the form with multiple answers i can retrieve them with $_POST, if i print_r($_POST) it shows : Array ( [question19] => Array ( [0] => b ) [question7] => Array ( [0] => a [1] => b ) [question20] => Array ( [0] => a [1] => b ) ) So from what i can see here it posts an array into another, i need the answer to be in this format: "ab","abc" etc. Any suggestion regarding how i can retrieve the answers and also how i can compare the answers with the correct answers from the database are welcomed. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/133356-multiple-questions/ Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 I think what you would need is implode. <?php foreach ($_POST as $key => $val) { if (stristr($key, "question") !== false) { $questions[$key] = implode("", $val); } } print_r($questions); ?> I think that is what you are getting at. Quote Link to comment https://forums.phpfreaks.com/topic/133356-multiple-questions/#findComment-693576 Share on other sites More sharing options...
danflo Posted November 19, 2008 Author Share Posted November 19, 2008 Thank You that was exactly what i was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/133356-multiple-questions/#findComment-693580 Share on other sites More sharing options...
danflo Posted November 23, 2008 Author Share Posted November 23, 2008 I wan the array keys to be only numbers so i can compare the answers with the id's from database. thank you <?php foreach ($_POST as $key => $val) { if (stristr($key, "question") !== false) { $questions[$key] = implode("", $val); } } print_r($questions); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133356-multiple-questions/#findComment-697003 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.