Results 1 to 5 of 5
  1. #1
    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;
    }


    In case your wondering, $num[1] through $num[4] are POST variables that the user has submitted. $option['math_db'] is the file to the flat file database I have. Here is an example of what is in it:
    PHP Code:
    0001::Add::[num1]+[num2]::[num1],,[num2
    My function doesn't work the way its supposed to because when I test it out it doesn't return false when I leave a blank empty, unless its $num[1] or $num[2]. Any ideas? Thanks.

  2. #2
    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
    this will iterate through each element in the $num array and assign the array key to $key and the value to $val. Then, we check to see if $val is set (not null). If it IS null, we simply return false. At the end, if we make it all the way through, then all values were filled in and we can return true
    Protollix - Sean Finkel
    Email
    Website
    Shared Hosting :: Cloud Hosting

  3. #3
    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?

  4. #4
    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

  5. #5
    Join Date
    Feb 2003
    Location
    Virginia
    Posts
    237
    Shep's method works as well.

    Weird that that did not work for you, though...
    Protollix - Sean Finkel
    Email
    Website
    Shared Hosting :: Cloud Hosting

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •