Page 1 of 1

comparing multiple fields

Posted: Sat Jul 17, 2004 12:58 am
by fresh
hey

Code: Select all

$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
$d = $_GET['d'];

// what the input should have been
$e = "answer 1";
$f = "answer 2";
$g = "answer 3";
$h = "answer 4";

//if wrong what they will be
$j = "wrong 1";
$k = "wrong 2";
$l = "wrong 3";
$m = "wrong 4";

$i = $_GET['name'];

if($a != $e) {
} elseif ($b != $f) {
} elseif($c != $g) {
} elseif($d != $h) {
} elseif($e != $i) {

include("webchal1wrong.php");

} else {

include("webchal1right.php");
}
as you can see I'm trying to get the input from a user and then compare that input to my stored values of what they should have inputted, and if not then it would pass the other varibles, to the webchal1wrong.php page where their values would be printed. Any help on this one, thanks :)

Posted: Sat Jul 17, 2004 1:04 am
by feyd
webchal1wrong.php would have access to all the $_GET/$_POST/whatever variables that page would..

sorry

Posted: Sat Jul 17, 2004 1:07 am
by fresh
was still working on the code as you posted... Im trying to determine if the stuff they send to that script is correct and if it isnt then I need a way to send another set of variables to the other webchal1wrong.php page if wrong, and another set of variables to the webchalright.php page if right... and it's either one or the other not both... is this possible.. ??? thanks :)

Posted: Sat Jul 17, 2004 1:13 am
by feyd
asside from not closing a few of those elseif's, the logic would only include one or the other..

Posted: Sat Jul 17, 2004 1:57 am
by fresh
so your saying if I do this:

Code: Select all

// the user input
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
$d = $_GET['d'];
$e = $_GET['name'];

// what the input should have been
$f = "answer 1";
$g = "answer 2";
$h = "answer 3";
$i = "answer 4";

$j = $_SESSION['username'];

if($a != $f) {

} elseif ($b != $g) {
$j = "wrong 1";

} elseif($c != $h) {
$k = "wrong 2";

} elseif($d != $i) {
$l = "wrong 3";

} elseif($e != $j) {
$m = "wrong 4";

include("webchal1wrong.php");

} else {

include("webchal1right.php");
}
it will work??? :)

Posted: Sat Jul 17, 2004 2:18 am
by feyd
I don't see why that wouldn't work (without running it)

nope

Posted: Sat Jul 17, 2004 3:11 am
by fresh
didn't work, just displayed a blank white page... hmm.. how would you go about doing this same thing feyd???

heres my code as is:

Code: Select all

// the user input
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
$d = $_GET['d'];
$e = $_GET['name'];

// what the input should have been
$f = "answer 1";
$g = "answer 2";
$h = "answer 3";
$i = "answer 4";

$j = $_SESSION['username'];

if($a != $f) {

} elseif ($b != $g) {
$j = "wrong 1";

} elseif($c != $h) {
$k = "wrong 2";

} elseif($d != $i) {
$l = "wrong 3";

} elseif($e != $j) {
$m = "wrong 4";

include("webchal1wrong.php");

} else {

include("webchal1right.php");

}
thanks :)

Posted: Sat Jul 17, 2004 4:01 am
by feyd

Code: Select all

<?php

$valid = $correct = array();
$answers = !emtpy($_GET['answers']) ? $_GET['answers'] : array();
$correct[] = 'correct answer 1';
$correct[] = 'correct answer 2';
$correct[] = 'correct answer 3';
$correct[] = 'correct answer 4';

if(!is_array($answers) || sizeof($correct) != sizeof($answers))
  die('answers in wrong format or mismatch of answers to answer key');

$right = false;
foreach($correct as $k => $v)
{
  $right |= ($valid[$k] = ($v == $answers[$k]));
}

if($right)
{
  include('webchal1right.php');
}
else
{
  include('webchal1wrong.php');
}

?>

oh

Posted: Sat Jul 17, 2004 5:31 am
by fresh
you can make arrays like in JS?? Sweet... so I would just lable those brackets right?? thanks feyd :)

Posted: Sat Jul 17, 2004 8:51 am
by feyd
you don't have to label the brackets, they are auto labeled with numeric indices just like that.