php - redirecting a survey user to last page that he/she was on -
i have been working on survey , done, there's 1 requirement has eaten me up.
a user can take survey, , there 6 different sets of questions , each set of questions in question group 1-6. when user question group 1-4 , logs out, he/she should log in , redirected question group 5. far code working , redirecting first 3 pages (questions1.php, people.php , environment.php), refusing rest.
i think problem if elseif statement. achieve desired result appreciated. cheers!
here code
if ($row['userid']==$user){ while($row = mysql_fetch_assoc($result_set)){ if($row['question_group_id']==1 && $row['choosen']==1){ header("location:people.php"); } elseif($row['question_group_id']==2 && $row['choosen']==1){ header("location:environment.php"); } elseif($row['question_group_id']==3 && $row['choosen']==1){ header("location:widerorganization.php"); } elseif($row['question_group_id']==4 && $row['choosen']==1){ header("location:procurement.php"); } elseif($row['question_group_id']==5 && $row['choosen']==1){ header("location: communication.php"); } elseif($row['question_group_id']==6 && $row['choosen']==1){ header("location: notificationpage.php"); }}} else { header("location:questions1.php"); }
it's matter of rendering clean codes.
i reformatted code. here's code before:
if ($row['userid']==$user) { while($row = mysql_fetch_assoc($result_set)) { if ($row['question_group_id']==1 && $row['choosen']==1) { header("location:people.php"); } else if($row['question_group_id']==2 && $row['choosen']==1) { header("location:environment.php"); } else if ($row['question_group_id']==3 && $row['choosen']==1) { header("location:widerorganization.php"); } else if ($row['question_group_id']==4 && $row['choosen']==1) { header("location:procurement.php"); } else if ($row['question_group_id']==5 && $row['choosen']==1) { header("location: communication.php"); } else if ($row['question_group_id']==6 && $row['choosen']==1) { header("location: notificationpage.php"); } } } else { header("location:questions1.php"); }
solution: since every condition has predictable pattern, why don't put pages associative array instead of creating tedious if
conditions? anyhow, proposed adjustment code.
<?php $locations = array( // question group id => landing page 1 => 'people.php', 2 => 'environment.php', 3 => 'wideorganization.php', 4 => 'procurement.php', 5 => 'communication.php', 6 => 'notificationpage.php' ); if ($row['userid']==$user) { while($row = mysql_fetch_assoc($result_set)) { // here's question group id $questiongroupid = $row['question_group_id']; // determine here if (isset($locations[$questiongroupid]) && $row['choosen'] == 1) { // defined associative array above header(sprintf("location:%s", $locations[$questiongroupid])); exit(); } } } else { header("location:questions1.php"); }
now it's easier debug. please try code ;)
Comments
Post a Comment