Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invalid argument supplied for foreach()

Status
Not open for further replies.

plarsen

Programmer
Jul 30, 2002
27
DK
Hi

I have the a foreach loop in my code, but i'm getting "Invalid argument supplied for foreach()" error.

At the beginning of the file i have these two lines:

Code:
require_once("classes/Countries.php");
$countries = new Countries;

And later on i have these lines:

Code:
if (! isset($_GET['nocountries'])) {
  foreach ($countries as $c => $value) {
    echo "<a href=\"start.php?country=$c->iso2\">\n";
    echo "<img src=\"$c->picfile\" alt=\"$c->name\" title=\"$c->name\" style=\"border:0\" />\n";
    echo "</a>\n";
  }
}

The Countries.php file in the classes folder just reads data from a countries table in a database.

What could be wrong here?

/Peter
 
foreach() takes as its first parameter an array, so at this line:
[tt]foreach ($countries as $c => $value) {[/tt]
$countries should be an array.

But you're initializing $countries in this line:
[tt]$countries = new Countries;[/tt]
which means $countries is an object.

Foreach() cannot operate on an object, only an array.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I think that $countries is an array.

Here is the code for the country.php file.

Code:
<?php
  require_once("classes/Countries.php");
  
  if (isset($_GET['country'])) {
    $country = new Country;
    $country->from_iso2($_GET['country']);
    $country->register_session();
    unset($_SESSION['spot_shown']);
  }

  // Check if a country is set ...
  if (! (isset($_SESSION['country_id']) && isset($_SESSION['country_lang_file']))) {
    // display welcome page... choose country
    ob_clean();
    header("Location: welcome.php");
    ob_end_flush();
    exit;
  } else {
    if (is_file("lang/" . strtolower($_SESSION['country_lang_file']))) {
      require_once("lang/" . strtolower($_SESSION['country_lang_file']));
    } else {
      // display welcome page... choose country
      ob_clean();
      header("Location: welcome.php");
      ob_end_flush();
      exit;
    }
  }
?>

and the countries.php file.

Code:
<?php
  require_once("Sql.php");

  class Country {
    var $country_id;
    var $name;
    var $iso2;
    var $iso3;

    var $lang_file;
		var $picfile;
		
		var $home;
		var $spot;
		var $news_title;
		var $news_text;

    function Country($id = 0) {
      if ($id > 0) {
        $this->from_id($id);
      }
    }

    function from_id($id) {
      if ($this->from_sql('countries_id',$id)) return true;
      else return false;
    }

    function from_iso2($iso2) {
      if ($this->from_sql('countries_iso_code_2',$iso2)) return true;
      else return false;
    }

    function from_sql($type,$value) {
      $sql = new SqlData;
      $sql->connect_db();     
      $sql->queryStr = "SELECT * FROM countries";
      $sql->queryStr .= " WHERE `$type` = '$value';";
      $sql->run(false,MYSQL_BOTH);
      // $sql->close();

      if ($sql->num == 1) {
        $this->country_id = $sql->data[0]['countries_id'];
        $this->name = $sql->data[0]['countries_name'];
        $this->iso2 = $sql->data[0]['countries_iso_code_2'];
        $this->iso3 = $sql->data[0]['countries_iso_code_3'];
        $this->lang_file = $sql->data[0]['language_file'];
				$this->picfile = $sql->data[0]['picfile'];
				$this->home = $sql->data[0]['home'];
				$this->spot = $sql->data[0]['spot'];
				$this->news_title = $sql->data[0]['news_title'];
				$this->news_text = $sql->data[0]['news_text'];
      }

      return true;
    }

    function register_session() {
      $_SESSION['country_id'] = $this->country_id;
      $_SESSION['country_name'] = $this->name;
      $_SESSION['country_iso2'] = $this->iso2;
      $_SESSION['country_lang_file'] = $this->lang_file;

      return true;
    }
  }

  class Countries {
    var $list = array();

    function Countries($all = false,$limit = 0) {
      $sql = new SqlData;
      $sql->connect_db();
      $sql->queryStr = "SELECT countries_id FROM countries";
      if (! $all) $sql->queryStr .= " WHERE topbeauty = 1";
      $sql->queryStr .= " ORDER BY countries_name;";
      $sql->run();
      $sql->close();
      
      foreach ($sql->data as $data) {
        $this->list[] = new Country($data['countries_id']);
      }
    }

    function get_array($size = 'full',$world = false) {
      $array = array();
      if ($world) $array[0] = $world;
      foreach ($this->list as $country) {
        $id = $country->country_id;
				if ($size == 'full') $array["$id"] = $country->name;
				elseif ($size == 'small') $array["$id"] = $country->iso2;
      }
      return $array;
    }
  }

?>
 
I think that $countries is an array.
"[tt]$countries[/tt]"? That variable doesn't even appear in the code you've just posted. The only thing close is [tt]$country[/tt].

But going back to your original post, what makes you think $countres is an array? You're using the [tt]new[/tt] operator on it, which according to is used to instantiate a class into an object.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
As Sleipnir Said, and checking the code, $countries is not an Array it si indeed and Object.

Code:
...
 class Country {
    var $country_id;
    var $name;
...

That in any book is a definition of an object not an array.

To get the array from countries you should use the defined
Code:
function: function get_array($size = 'full',$world = false) 
[code]

That function which is defined in your code specifically returns an array. 


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Sorry it got submitted somehow.

This is also and object

Code:
class Countries {
      var $list = array();

The array resides inside the object countries. so you would have to access it using somethin like:
Code:
$countries->list

This is an array. $countries is not.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top