comparing multiple fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

comparing multiple fields

Post 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 :)
Last edited by fresh on Sat Jul 17, 2004 1:04 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

webchal1wrong.php would have access to all the $_GET/$_POST/whatever variables that page would..
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

sorry

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

asside from not closing a few of those elseif's, the logic would only include one or the other..
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post 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??? :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't see why that wouldn't work (without running it)
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

nope

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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');
}

?>
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

oh

Post by fresh »

you can make arrays like in JS?? Sweet... so I would just lable those brackets right?? thanks feyd :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't have to label the brackets, they are auto labeled with numeric indices just like that.
Post Reply