konrad00 Posted May 7, 2009 Share Posted May 7, 2009 Hi again ! Thanks for helping me with my previous issues. Problem: from html form im getting users responses ie name="q1[]". Then in array.php file I want to read this answers and compare them with result from database. Simple but how can I compare two arrays: one from S_SESSION containing answers q1,q2,q3 and sql result from database? If someone could help me that would be great! Thanks Below are snippets of my scripts: ---html form <?php session_start(); ?> <html> <head> <title>Take the Coolness Quiz!</title> </head> <body> <h1>English Test</h1> <h2>Please choose correct answer.</h2> <form action="array.php" method="post"> <b>Question 1: How old ... you?</b><br> <input type="radio" name="q1[]" value="is">is<br> <input type="radio" name="q1[]" value="are">are<br> <input type="radio" name="q1[]" value="be">be <br> <b>Question 2: Where ... you live?</b><br> <input type="radio" name="q2[]" value="does"> does <br> <input type="radio" name="q2[]" value="do"> do <br> <input type="radio" name="q2[]" value="done"> done <br> <b>Question 3: What ... your name?</b><br> <input type="radio" name="q3[]" value="are"> are<br> <input type="radio" name="q3[]" value="have"> have <br> <input type="radio" name="q3[]" value="is"> is<br> <br> <input type="submit" value="Determine Your Coolness"> <?php $_SESSION['q1']=$_POST['q1']; $_SESSION['q2']=$_POST['q2']; $_SESSION['q3']=$_POST['q3']; ?> </form> </body> </html> ---html form end --array.php <?php session_start(); ?> <?php $_SESSION['q1']=$_POST['q1']; $_SESSION['q2']=$_POST['q2']; $_SESSION['q3']=$_POST['q3']; $conn = mysql_connect("localhost", "root", ""); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("ajax")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql = "SELECT q_id, ans FROM answers "; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } foreach(array_values($_SESSION) as $value) { echo $value ."</br>"; } while ($row = mysql_fetch_assoc($result)) { echo $row["q_id"]; echo $row["ans"]; } mysql_free_result($result); ?> Link to comment https://forums.phpfreaks.com/topic/157234-comparing-arrays-problem-php/ Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 How do you want to compare them? Define compare. Link to comment https://forums.phpfreaks.com/topic/157234-comparing-arrays-problem-php/#findComment-828483 Share on other sites More sharing options...
ignace Posted May 7, 2009 Share Posted May 7, 2009 <?php $array = mysql_fetch_assoc($result); $correct_answers = array_intersect($array, $_POST); ?> Link to comment https://forums.phpfreaks.com/topic/157234-comparing-arrays-problem-php/#findComment-828488 Share on other sites More sharing options...
konrad00 Posted May 7, 2009 Author Share Posted May 7, 2009 Sorry for not being more specific. I need to compare q1,q2,q3 values (extracted from S_SESSION array) with values of "ans" field pulled from mysql table. array_1=$_SESSION[]=[q1]->is,[q2]->are,[q3]->do //data extracted from html/checkboxes $sql_array[]=q1->is,q2->do,q3->is //data from mysql Is there a way to compare these two? array_diff_assoc? Thanks Link to comment https://forums.phpfreaks.com/topic/157234-comparing-arrays-problem-php/#findComment-828585 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.