Page 1 of 1

Invalid argument supplied for foreach()

Posted: Sun Oct 26, 2008 9:15 pm
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!

Re: Invalid argument supplied for foreach()

Posted: Sun Oct 26, 2008 9:59 pm
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.

Re: Invalid argument supplied for foreach()

Posted: Sun Oct 26, 2008 10:06 pm
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!

Re: Invalid argument supplied for foreach()

Posted: Sun Oct 26, 2008 10:17 pm
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.

Re: Invalid argument supplied for foreach()

Posted: Sun Oct 26, 2008 10:26 pm
by invisibled
hahah holy crap dude, i wish i knew about this function sooner. Thanks alot :)