foreach not working

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
jrja
Forum Newbie
Posts: 17
Joined: Wed Nov 19, 2008 10:28 am

foreach not working

Post by jrja »

I'm having a problem getting this code to work in a foreach loop - perhaps someone can see what I'm not. In the if statement I have to use the names instead of the variable the names are asigned to :

this the way it should be, but it doesn't work--

Code: Select all

    $required_fields = array('menu_name', 'position', 'visible');
        foreach($required_fields as $fieldname) {
            if(!isset($_POST[$fieldname]) || 
                (empty($_POST[$fieldname]) && !is_int($_POST[$fieldname]))) {
                $errors[] = $fieldname;
            }
        }
this is what I've settled with -- it works

Code: Select all

    $required_fields = array('menu_name', 'position', 'visible');
        foreach($required_fields as $fieldname) {
            if(!isset($_POST['position']) || 
                (empty($_POST['menu_name']) && !is_int($_POST['visible']))) {
                $errors[] = $fieldname;
            }
        }
any suggestions would be greatly appreciated
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: foreach not working

Post by aceconcepts »

You could use the in_array() function
jrja
Forum Newbie
Posts: 17
Joined: Wed Nov 19, 2008 10:28 am

Re: foreach not working

Post by jrja »

aceconcepts wrote:You could use the in_array() function

where?

anyway, I put back the $fieldname variable where the db names were, used is_int( ) instead of !is_int( ) then unset($fieldname) at the end -- it works now. Not sure why - but it does.
Post Reply