Jump to content

Recommended Posts

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.