Results 1 to 5 of 5
-
02-06-2004, 09:27 AM #1Web Hosting Master
- Join Date
- Feb 2003
- Posts
- 717
[php]code not working the way its suppossed to
OK, I'm working on this little application right now, and I am stuck. I have a function called check_dep() which is supposed to check if the user has entered in all the required information.
This is the function taht I have done so far(doesn't work):
PHP Code:function check_dep($var1)
{//function to check if the user has filled in all the required fields;returns FALSE if the user didn't fill them all in
global $option,$num;//make the $math_db,$num variables availiable to this function
$file = file($option['math_db']);
foreach ($file as $value)
{
//go through the file and strip out the "::" and put them into an array
$temp = explode('::', trim($value));
$math[$temp[0]]['form'] = $temp[1];//the value that users see in the script
$math[$temp[0]]['exp'] = $temp[2];//the expression
$math[$temp[0]]['req'] = $temp[3];//the dependincies;
$reqd = explode(',,', trim($math[$temp[0]][req]));
for ($i = 0; $i < 3; $i++) {
switch ($reqd[$i])
{
case "[num1]":
$return = (!$num[1]) ? FALSE : TRUE;
break;
case "[num2]":
$return = (!$num[2]) ? FALSE : TRUE;
break;
case "[num3]":
$return = (!$num[3]) ? FALSE : TRUE;
break;
case "[num4]":
$return = (!$num[4]) ? FALSE : TRUE;
break;
}
}
return $return;
}
}
PHP Code:0001::Add::[num1]+[num2]::[num1],,[num2]
-
02-06-2004, 10:26 AM #2Junior Guru
- Join Date
- Feb 2003
- Location
- Virginia
- Posts
- 237
It looks to me like the problem is that you are overwriting $return with each iteration of the loop. So if the first field is blank, it will set it to FALSE. However, when it checks the next field and sees there is a value, it will set it to TRUE.
try replacing your loop with something like:
PHP Code:foreach( $num as $key=>$val ) {
if (!isset($val)) {
return false;
}
}
return true;
-
02-06-2004, 11:00 AM #3Web Hosting Master
- Join Date
- Feb 2003
- Posts
- 717
Sorry, doesn't work. But you did point out to me a mistake (the overwriting of $return). Anyone else have any ideas?
-
02-06-2004, 12:07 PM #4Web Hosting Master
- Join Date
- Dec 2002
- Location
- The Shadows
- Posts
- 2,925
What you should do, is have a variable called "error" and only set it, if a error exists, otherwise do nothing. Sorta like this:
PHP Code:$return = '';
for ($i = 0; $i < 3; $i++) {
switch ($reqd[$i])
{
case "[num1]":
if(!$num[1])
{
$return = false;
}
break;
case "[num2]":
if(!$num[2])
{
$return = false;
}
break;
case "[num3]":
if(!$num[3])
{
$return = false;
}
break;
case "[num4]":
if(!$num[4])
{
$return = false;
}
break;
}
}
Dan Sheppard ~ Freelance whatever
-
02-06-2004, 04:17 PM #5Junior Guru
- Join Date
- Feb 2003
- Location
- Virginia
- Posts
- 237