Invalid argument supplied for foreach()

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
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Invalid argument supplied for foreach()

Post by invisibled »

Ok so here is my deal. I have two class's, globalController and pagesController. globalController holds random snippets of code that i would like to reuse around the site, and pagesController is for the "pages" section of my app. when i call a foreach loop it gives me the invalid argument warning. here is my pagesController function:

Code: Select all

function update(){
 
        require 'globalController.php';
        $g = new globalController();
            
        //Grabs the page id from the url
        $id = $_GET['id'];
        
        //Saves the changes to the database
        if(isset($_POST['content'])):
            foreach($_POST as $var => $value):
                $$var = $value;
                $$var = str_replace( "'", "'", $$var);
            endforeach;
            
            mysql_query("UPDATE ida_pages SET 
                title='$title', 
                content='$content' 
            WHERE entryID = '$id'") or die(mysql_error());
            
            $g->alert_success();
        endif;
        
        //Set's the mysql Query
        $mysql = mysql_query("SELECT * FROM ida_pages WHERE entryID = '$id'");
        $data = mysql_fetch_assoc($mysql);
        
        //Set's all $data value's as var's
        $g->all_data();
        
        //Loads the Tinymce Scripts
        print '<script type="text/javascript" src="../lib/tiny_mce/tiny_mce_gzip.js"></script>';
        print '<script type="text/javascript" src="../lib/tiny_mce/pagesScript.js"></script>';
        
        //The Form
        print '<form action="'.$PHP_SELF.'" method="post">';
            print '<button type="submit" class="save" title="Save">&nbsp;</button>';
            print '<a href="./" title="Back" class="back">&nbsp;</a>';
            print '<input type="text" name="title" value="'.$title.'" />';
            print '<textarea name="content">'.$content.'</textarea>';   
        print '</form>';
    }
the $g->all data() is the problem function ($g->alert_success() works fine for some reason) and here is its code from global controller:

Code: Select all

//Set's all $data value's as var's
    function all_data(){
        foreach($data as $var => $value):
            $$var = $value;
        endforeach;
    }
the loop works fine if it's right in the update function but gives the error when i call it from the globalController class.

Suggestions? help? Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Invalid argument supplied for foreach()

Post by requinix »

How do you expect all_data to know what $data is? You never told it.

By the way there's no point in having that all_data function when something like it already exists.
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: Invalid argument supplied for foreach()

Post by invisibled »

I assumed it could read the code above it? Am i wrong?

Code: Select all

//Set's the mysql Query
        $mysql = mysql_query("SELECT * FROM ida_pages WHERE entryID = '$id'");
        $data = mysql_fetch_assoc($mysql);
        
        //Set's all $data value's as var's
        $g->all_data();
How could i utilize extract() in this situation? I think i'm missing your point, but would love to use it if it works!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Invalid argument supplied for foreach()

Post by requinix »

invisibled wrote:Am i wrong?
Yep.
invisibled wrote:How could i utilize extract() in this situation? I think i'm missing your point, but would love to use it if it works!
It does the exact same thing your function does, except you have to tell it what array to use.
There's example code on that page.
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: Invalid argument supplied for foreach()

Post by invisibled »

hahah holy crap dude, i wish i knew about this function sooner. Thanks alot :)
Post Reply