SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: foreach error

  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question foreach error

    Hi.. I am in trouble again

    I am getting the following error when one of the validation checks (empty) is true.

    Code:
    Warning: Invalid argument supplied for foreach() in .......
    PHP Code:
    elseif ($install==step1a){


    // Begin Form Error Checking
        
    $errors = array();
        

    // Check for a database password
    if(empty($dbpass)){
          
    $errors 'Please enter a password.';
       } 
       
    // Check for a database username
    if(empty($dbuser)){
          
    $errors 'Please enter a username.';
       }   

    // Check for a database host
    if(empty($host)){
          
    $errors 'Please enter a host name.';
       }  
       
    // If form validation went okay and there were no errors, continue.    
        
    if (empty($errors)) {


    // do something here


    } else { // if errors array contains a value
        
            
    echo "<table align=\"center\"><tr><td>The following error(s) occurred:<br />";
            
                foreach (
    $errors as $msg) { // Print each error.
                
    echo "* $msg<br />\n";
                }
                
            echo 
    '</td></tr></table>';
            
        } 
    // End of if (empty($errors)) IF. 
    Can anyone possibly help?

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are setting the errors as a string when you should be setting them in an array. Try this:
    PHP Code:
    elseif ($install==step1a){





    // Begin Form Error Checking

        
    $errors = array();

        



    // Check for a database password

    if(empty($dbpass)){

          
    $errors['pass'] = 'Please enter a password.';

       } 

       

    // Check for a database username

    if(empty($dbuser)){

          
    $errors['user'] = 'Please enter a username.';

       }   



    // Check for a database host

    if(empty($host)){

          
    $errors['host'] = 'Please enter a host name.';

       }  

       

    // If form validation went okay and there were no errors, continue.    

        
    if (empty($errors)) {





    // do something here





    } else { // if errors array contains a value

        

            
    echo "<table align=\"center\"><tr><td>The following error(s) occurred:<br />";

            

                foreach (
    $errors as $errtype => $msg) { // Print each error.

                
    echo "* $msg<br />\n";

                }

                

            echo 
    '</td></tr></table>';

            

        } 
    // End of if (empty($errors)) IF. 
    Kayzio - We don't hesitate, we accelerate.

  3. #3
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you soo much it works a treat
    Not sure what I had done wrong though as I have used variation of that code for months on heaps of different apps with no probs.


Bookmarks

Posting Permissions

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