Page 1 of 1

Invalid argument supplied for foreach()

Posted: Thu May 15, 2003 9:20 pm
by AaronSmith
I am getting an “Invalid argument supplied for foreach()” error using the following code:

Code: Select all

<?
	$sep = "";
	foreach ( $current_item->features as $key => $val )
	&#123;
	    print $sep . $features_table&#1111;$key];
	    $sep = ", ";
	&#125;
?>

Which accesses this file:

Code: Select all

<?

class models
&#123;
    var $ID;
    var $Created;
    var $Expires;
    var $AuthorID;
    var $title;
    var $description;
	var $filename;
    var $pictureID;
    var $duration;
    var $city;
    var $state;
    var $features;
    var $category;

    function models()
    &#123;
	$this->AuthorID = $_SESSION&#1111;'PHPAUCTION_LOGGED_IN'];
	$this->features = array();
	$this->category = 0;
    &#125;

    function save()
    &#123;
	if ( empty($this->ID) )
	&#123;
	    $query = "INSERT INTO models SET ";
	    $this->Created = time();
	    $this->Expires = $this->Created + $this->duration*24*60*60;
	&#125;
	else 
	    $query = "UPDATE models SET ";

	$query .= safe_pair("AuthorID=",$this->AuthorID);
	$query .= safe_pair(", Category=",$this->category);
	$query .= safe_pair(", Created=",$this->Created);
	$query .= safe_pair(", Expires=",$this->Expires);
	$query .= safe_pair(", object=",serialize($this));

	if ( !empty($this->ID) )
	&#123;
	    $query .= safe_pair(" WHERE ID=",$this->ID);
	&#125;

	$q = mysql_query($query) or models_error(mysql_error());
	$id = mysql_insert_id();
	if ( empty($this->ID) && $id > 0 )
	&#123;
	    $this->ID =  $id;
	&#125;
	return $this->ID;
    &#125;

    function scan_form($a,$features)
    &#123;
	$errors = array();

	if ( $this->ID != $a&#1111;'ID'] )
	&#123;
	    models_error("Intruder alert!  ID mismatch.");
	&#125;
	unset($a&#1111;'ID']);

	foreach ( $a as $k => $v )
	&#123;
	     $a&#1111;$k] = strtr($a&#1111;$k],array("\''"=>"'","&quote;"=>"""));
	&#125;

	if ( empty($a&#1111;'title']) )
	&#123;
	    $errors&#1111;'title'] = "You must provide an item title.";
	&#125;

	if ( empty($a&#1111;'description']) )
	&#123;
	    $errors&#1111;'description'] = "A description is required!";
	&#125;

	global $models_duration_table;
	if ( !isset($models_duration_table&#1111;$a&#1111;'duration']]) )
	&#123;
	    $errors&#1111;'duration'] = "Choose a standard duration.";
	&#125;

	if ( empty($a&#1111;'city']) )
	&#123;
	    $errors&#1111;'city'] = "City where model is located.";
	&#125;

	if ( empty($a&#1111;'state']) )
	&#123;
	    $errors&#1111;'state'] = "State (or country) where model is located.";
	&#125;



	if ( empty($a&#1111;'category']) || $a&#1111;'category'] < 1 )
	&#123;
	    $errors&#1111;'category'] = "You must select a primary category.";
	    $a&#1111;'category'] = 0;
	&#125;

	foreach ( get_class_vars('models') as $k => $v )
	&#123;
	    if ( isset($a&#1111;$k]) )
		$this->$k = $a&#1111;$k];
	&#125;

	return $errors;
    &#125;
&#125;


$models_duration_table = array(
	10 => "10 days",
	30 => "1 month",
	999999 => "1 year"
);


$features_table = array(
	'fashion' => "Fashion",
	'glamour' => "Glamour",
	'video' => "Video",
	'nude' => "Nude",
	'editorial' => "Editorial"
);

	global $features_table;
	$this->features = array();
	unset($a&#1111;'features']);
	if ( isset($features) )
	&#123;
	    foreach ( $features as $key => $val )
	    &#123;
		if ( isset($features_table&#1111;$val]) )
		&#123;
		    $this->features&#1111;$val] = true;
		&#125;
		else
		&#123;
		    $errors&#1111;'features'] = "Internal error: unknown feature";
		    unset($features&#1111;$key]);
		&#125;
	    &#125;
	&#125;
	if ( empty($features) && !isset($errors&#1111;'features']) )
	&#123;
	    $errors&#1111;'features'] = "Select at least one feature option.";
	&#125;;

function get_models($search_id)
&#123;
	global $query_string;
	$q = safe_query("select * from models"
		. " where ID =", $search_id)
		or models_error(mysql_error() . "<br><pre>" . $query_string . "</pre>");

	if ( mysql_num_rows($q) != 1 )
		models_error("Internal error: row not unique");

	$row = mysql_fetch_assoc($q);
	$obj = unserialize($row&#1111;'object']);
	$obj->ID = $row&#1111;'ID']; // may not be assigned when INSERTed
	return $obj;
&#125;

function models_search($cat,$quant)
&#123;
    global $query_string;
    return snarf_array('models','ID','ID',
	" WHERE (Category=$cat OR Category2=$cat OR Category3=$cat) "
	. " AND Expires>" . time()
	. " ORDER BY Created DESC LIMIT $quant" );
&#125;

function models_textsearch($key,$limit)
&#123;
    global $query_string;
    return snarf_array('models','ID','ID',
	" WHERE UPPER(object) LIKE UPPER('%". mysql_escape_string($key) . "%')"
	. " AND Expires>" . time()
	. " ORDER BY Created DESC $limit" );
&#125;
I just can’t figure out what I am doing wrong. It works in other files where I have “payment” as a variable instead of “features.” Any help is appreciated!

Posted: Fri May 16, 2003 2:10 am
by []InTeR[]

Code: Select all

unset($a&#1111;'features']); 
   if ( isset($features) ) 
   &#123; 
       foreach ( $features as $key => $val )
$features is not set here, try $this->features.

Posted: Fri May 16, 2003 2:52 am
by AaronSmith
That didn't work, but thanks for responding.

I was able to get it working, but I have no idea how because I changed so many things at once. This was a good lesson...