Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

php session array problem

Avatar of mattibutt
mattibuttFlag for United States of America asked on
PHP
10 Comments1 Solution581 ViewsLast Modified:
hi
i am trying to modify a quiz , previously it used to select all the questions on the same screen but i wanted to make sure one question appear at a time thanks prokvk expert he has helped me resolve this complicated problem but now i ran into another problem the php session is not keeping all the quiz data its only keeping one last response inside the session other values are lost
i need a way to keep all these user responses either inserting in sql temporary table or fixing the  array
//index.php
 
<?php include("config.php"); ?>
<?php include("connect.php"); ?>
<?php include("session_ini.php"); ?>
<?php
 
global $quiz_ready;
$quiz_ready = 'no';
if($_GET['menu']== 'start' && $quiz_ready=='no')
{
global $error,$total_question,$time_sec,$time_min,$cat1,$use_tedu,$total_rows;
 if(isset($_POST['next_step']) && isset($_POST['total_question']) && isset($_POST['time']) )
 {
 	// Get random questions
	$stamp = date('mdyHis');
	$_SESSION['quiz_date'] = date('d-M-y ,D');
	$_SESSION['quiz_time'] = date('H:i:s');
	$_SESSION['quiz_id'] = $stamp;
	$_SESSION['full_name'] = $_POST['name_full'];
	$_SESSION['email'] = $_POST['email_id'];
	$_SESSION['quiz_total_question'] = $_POST['total_question'];
	$_SESSION['quiz_time_sec'] = $_POST['time'] * 60;	// time in seconds
	$_SESSION['quiz_time_min'] = $_POST['time'];
	$_SESSION['quiz_cat1'] = $_POST['cat1'];
	$query = "SELECT * FROM ".$crtp_quiz;
	$sql = mysql_query($query);
	$total_rows = mysql_num_rows($sql);
	$i=0;
	array($rand_arr);
	while($i < $_SESSION['quiz_total_question'])
	{
	$rand_num = rand(1,$total_rows - 1);
	if(!@array_search($rand_num,$rand_arr))
	{
	
	$rand_arr[$i] = $rand_num;
	$i++;
	}
	}
	global $quiz_data;
	for($i=0;$i < $_SESSION['quiz_total_question'];$i++)
	{
	$query = "SELECT * FROM ".$crtp_quiz." WHERE id = ".$rand_arr[$i];
	$sql = mysql_query($query);
	$quiz_data[$i] = mysql_fetch_array($sql);
		echo	$_SESSION["current_question"];
	echo $_SESSION['quiz_total_question'];
	}
	$_SESSION['quiz_data']=$quiz_data;
	//print_r($quiz_data);
 }
 elseif(isset($_POST['next_step']))
 {
 	$error = "Some of the fields are left empty !";
	header('Location: index.php?menu=setup');
 }
}
elseif($_GET['menu']== 'finish')
{
	global $correct,$wrong,$unanswered;
	$correct=0;
	$wrong=0;
	$unanswered=0;
	$_SESSION['full_name']=$_POST['full_name'];
	$_SESSION['quiz_time_unused'] = $_POST['quiz_time_unused']; 
	for($i=1;$i<=$_SESSION['quiz_total_question'];$i++)
	{
	//echo $_SESSION['quiz_data'][$i-1]['answer'];
	//echo "Comp :".$_POST[$i].":".$_SESSION['quiz_data'][$i-1]['answer'].":<br>";
 
	$_SESSION['quiz_user_ans'][$i-1]= trim($_POST[$i]);
		if(isset($_POST[$i]))
		{
			if(trim($_POST[$i])==trim($_SESSION['quiz_data'][$i-1]['answer']))
				{
					$correct++;
				}
			else
			{
				$wrong++;
			}
		}
		else
		{
			$unanswered++;
		}
	}
	$_SESSION['quiz_correct_ans']=$correct;
	$_SESSION['quiz_wrong_ans']=$wrong;
	$_SESSION['quiz_unanswered']=$unanswered;
	$_SESSION['quiz_score'] = round(($correct / $_SESSION['quiz_total_question']) * 100,2);
	if($_SESSION['quiz_score'] >= 95)
	{
		$_SESSION['quiz_grade']="A+";
	}
	elseif($_SESSION['quiz_score'] >= 90)
	{
		$_SESSION['quiz_grade']="A";
	}
	elseif($_SESSION['quiz_score'] >= 85)
	{
		$_SESSION['quiz_grade']="B+";
	}
	elseif($_SESSION['quiz_score'] >= 80)
	{
		$_SESSION['quiz_grade']="B";
	}
	elseif($_SESSION['quiz_score'] >= 75)
	{
		$_SESSION['quiz_grade']="C+";
	}
	elseif($_SESSION['quiz_score'] >= 70)
	{
		$_SESSION['quiz_grade']="C";
	}
	elseif($_SESSION['quiz_score'] >= 65)
	{
		$_SESSION['quiz_grade']="D+";
	}
	elseif($_SESSION['quiz_score'] >= 60)
	{
		$_SESSION['quiz_grade']="D";
	}
	else
	{
		$_SESSION['quiz_grade']="Fails";
	}
	$query = "SELECT * FROM ".$quiz_stats." WHERE quiz_id = '".$_SESSION['quiz_id']."' AND username = '".$_SESSION['username']."'";
	$sql   = mysql_query($query);
	if(mysql_num_rows($sql) <= 0)
	{
		$query = "INSERT INTO ".$quiz_stats." VALUES ('','".$_SESSION['quiz_id']."','".$_SESSION['username']."','".$_SESSION['full_name']."','".$_SESSION['email']."',NOW(),'".$_SESSION['quiz_date']."','".$_SESSION['quiz_time']."','".$_SESSION['quiz_total_question']."','".$_SESSION['quiz_unanswered']."','".$_SESSION['quiz_correct_ans']."','".$_SESSION['quiz_wrong_ans']."','".$_SESSION['quiz_time_min']."','".$_SESSION['quiz_score']."','".$_SESSION['quiz_grade']."')";
		//echo $query;
	
		$sql = mysql_query($query);
	}
	else
	{
		
	}
} 
 ?>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
 
 
 <title>Welcome To ProQuiz</title>
<style>
TD {font-family: tahoma; font-size: 11px;}
 
.bold{font:900}
.right{font-size:12px;font-family:verdana}
a.menu{color:1C5679;font-size:11;text-decoration:none;font:100}
a.menu:hover{color:red}
a.menu:visited{color:1C5679}
</style>
 
<script language="JavaScript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
 
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
 
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
 
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
 
</head>
<body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" onLoad="MM_preloadImages('images/b2r.jpg','images/b3r.jpg','images/b4r.jpg','images/b5r.jpg'),start')">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
	<tr>
	  <td height="34" background="images/top_quiz.jpg" width="100%" colspan="2" align="center" style="background-repeat:no-repeat; background-position:center"><font style="font:900">Welcome to ProQuiz </font></td>
	</tr>
	<?php include('top_header.php');  ?>
  </table>
<?php 							  
if($_GET['tandc']=='tandc')
include('../tandc.php'); ?>
&nbsp;<br>&nbsp;<br>&nbsp;<br>
<?php 
if(!isset($_GET['menu']))
{
include('tandc.php');
}
elseif($_GET['menu']=='setup')
{
include('setup.php');
}
//elseif($_GET['menu']=='start')
elseif (in_array($_GET['menu'],array('start','process')))
{
    if (!$_SESSION["current_question"])
    {
        $_SESSION["current_question"] = 0;
    }
    
    $_SESSION["current_question"]++;
 
	include('quiz.php');
}
elseif($_GET['menu']=='finish')
{
	include('finish.php');
}
elseif($_GET['menu']=='summary')
{
	include('summary.php');
}
 
?>
 
<br>
 
<?php include('index_footer.php'); ?>
</center>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of prokvk
prokvk

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answers