Page 1 of 1

Invalid argument supplied for foreach

Posted: Wed Feb 17, 2010 2:37 pm
by chopsmith
Hello, All. Here's the deal:

I have a script which instantiates a class thusly:

Code: Select all

 
$searchResultsObject = new search_results($_GET,'simple');
 
As you can see, I'm passing the $_GET array to the constructor of the search_results class. Within the constructor of that class, I assign $_GET to $this->queryString, like so:

Code: Select all

 
function __construct($queryString,$type) {
     . . .
     $this->queryString = $queryString;
     . . .
}
 
Then, I iterate through $this->queryString in a member function:

Code: Select all

 
    private function set_searchFields() {
        foreach ($this->queryString as $field => $value) {
            //don't deal with search button portion of the query string
            if ($field != 'submit') {
                //but deal with everything else here
                if ($value != '') {
                    //these are the criteria entered
                    $this->searchFields[$field] = $value;
                }
            }
        }       
    }
 
The intent of the code is achieved: a search is performed (a select query constructed) using only those fields for which criteria were entered by the user. However, I get an "Invalid argument supplied for foreach()" warning for the foreach loop in the member function (line 3) above. Can't figure out why. I've "echo'd" $this->queryString, which outputs, as expected, "Array()". I've "print_r"'d the array, and the associative array is printed out. Any ideas? Thanks.

Re: Invalid argument supplied for foreach

Posted: Wed Feb 17, 2010 3:02 pm
by AbraCadaver
How is set_searchFields() being called?

Re: Invalid argument supplied for foreach

Posted: Wed Feb 17, 2010 3:07 pm
by chopsmith
It is called from within the constructor, after i assign $_GET ($queryString) to $this->queryString:

Code: Select all

 
    function __construct($queryString,$type) {
        $this->queryString = $queryString;
        . . .
        $this->set_searchFields();
        . . .
    }
 
AbraCadaver wrote:How is set_searchFields() being called?

Re: Invalid argument supplied for foreach

Posted: Wed Feb 17, 2010 3:22 pm
by AbraCadaver
It must be some of your other code, because just what you have posted works fine, with or without get parameters in the URL.

Re: Invalid argument supplied for foreach

Posted: Wed Feb 17, 2010 3:25 pm
by chopsmith
Yeah, I can't figure it out. Nothing between assigning $queryString to $this->queryString and calling $this->set_searchFields() has anything to do with $this->queryString. Thanks for looking, though. Like I said, the code works as expected, but I just don't understand why I'm getting the warning.
AbraCadaver wrote:It must be some of your other code, because just what you have posted works fine, with or without get parameters in the URL.

Re: Invalid argument supplied for foreach

Posted: Wed Feb 17, 2010 3:31 pm
by AbraCadaver
What does just this do for you by itself?

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
 
class search_results {
 
    function __construct($queryString,$type) {
        $this->queryString = $queryString;
        $this->set_searchFields();
    }
 
    private function set_searchFields() {
        foreach ($this->queryString as $field => $value) {
            //don't deal with search button portion of the query string
            if ($field != 'submit') {
                //but deal with everything else here
                if ($value != '') {
                    //these are the criteria entered
                    $this->searchFields[$field] = $value;
                }
            }
        }      
    }
}
 
$searchResultsObject = new search_results($_GET,'simple');
print_r($searchResultsObject->searchFields);
?>

Re: Invalid argument supplied for foreach

Posted: Wed Feb 17, 2010 3:40 pm
by chopsmith
I had to add

Code: Select all

 
var $searchFields = array();
 
within the class, but I get "Array()" without the error. Unfortunately, I still can't figure out my problem, and I'd probably have to post all the code in order for someone here to figure it out, which I don't really want to do. Thanks again, though.

Edit: Also, if I add a query string to the end of the url like:

Code: Select all

 
http://localhost/testing/search.php?id= ... 787&blank=
 
I get Array([id] => 3010078787)