• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assuming a function argument/parameter is an array?

#1
[eluser]zxcv[/eluser]
Hello people!

This is more a PHP problem more than it is a CI one...but I assume people are familiar with PHP enough to help me with this Tongue

What I have is an database...with members, where each one have their own unique e-mail. My site has adminspace, which allows a person with admin authority to add new users, remove users and so on.

The problem is when a admin is adding a new user. Normally, if someone registers the form validation would check the database if the typed in email already exists, it would also require the E-mail field to be set. The thing with the admin is that he/she can choose NOT to add emailinformation when adding a new member, allowing the member itself to set it manually as he/she signs in for the first time.

Here's the problem...IF the admin choose to type in an email, the process of doing so will always return true, even though the email already is registered. That is because the function I use for this action is weird and behaves badly.
Here's the code:

Code:
public function validate_email($email)
    {
       /* The function below checks if elements in an array with contains characters
          if they do, it gives an error +1. The values in the array are emails. */

        function check_null($array) {
            
            if(empty($array)) {
                return "Parameter for check_null() is not an array, or is empty.";
            }
            
            else
            {
                $error = 0;
                foreach($array as $email) {    //[b]LINE 74[/b]
                    if($email != NULL || $email != '') {
                        $error += 1;
                    }
                }
                
                if($error == 0) {
                    return TRUE;
                }
                elseif($error > 0) {
                    return FALSE;
                }
            }
        }
        
        $mysql = "SELECT * FROM members
                WHERE Email = '$email'";
        $query = $this->db->query($mysql);
        
        if($query->num_rows > 0) {
            
                        // Store all emails in an array                        

            $i = 0;
            foreach($query->result() as $check) {
                $email[$i] = $check->Email;
                $i++;
            }
            
                        //Here is where the function check_null() is used.
            if(check_null($email)) {
                return TRUE;
            }
            
            else {
                return FALSE;
            }
        }
        
        elseif($query->num_rows == 0) {
            return TRUE;
        }
    }


This is a function used in my model, maybe that's the problem?
However, here is the error message:


Quote:A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

....

Line 74

I marked Line 74 with a comment in the code...how do I solve this?

Thank you
zxcv

#2
[eluser]zxcv[/eluser]
Never mind, it worked now!

I think the problem was that I used the variable email in the function, when the main modelfunction has it parameter called "$email", also I had a variable email as I retrieved all the emails from the database. :red:


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.