Invalid argument supplied for foreach()
Posted: Thu May 15, 2003 9:20 pm
I am getting an “Invalid argument supplied for foreach()” error using the following code:
Which accesses this file:
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!
Code: Select all
<?
$sep = "";
foreach ( $current_item->features as $key => $val )
{
print $sep . $features_tableї$key];
$sep = ", ";
}
?>Which accesses this file:
Code: Select all
<?
class models
{
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()
{
$this->AuthorID = $_SESSIONї'PHPAUCTION_LOGGED_IN'];
$this->features = array();
$this->category = 0;
}
function save()
{
if ( empty($this->ID) )
{
$query = "INSERT INTO models SET ";
$this->Created = time();
$this->Expires = $this->Created + $this->duration*24*60*60;
}
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) )
{
$query .= safe_pair(" WHERE ID=",$this->ID);
}
$q = mysql_query($query) or models_error(mysql_error());
$id = mysql_insert_id();
if ( empty($this->ID) && $id > 0 )
{
$this->ID = $id;
}
return $this->ID;
}
function scan_form($a,$features)
{
$errors = array();
if ( $this->ID != $aї'ID'] )
{
models_error("Intruder alert! ID mismatch.");
}
unset($aї'ID']);
foreach ( $a as $k => $v )
{
$aї$k] = strtr($aї$k],array("\''"=>"'",""e;"=>"""));
}
if ( empty($aї'title']) )
{
$errorsї'title'] = "You must provide an item title.";
}
if ( empty($aї'description']) )
{
$errorsї'description'] = "A description is required!";
}
global $models_duration_table;
if ( !isset($models_duration_tableї$aї'duration']]) )
{
$errorsї'duration'] = "Choose a standard duration.";
}
if ( empty($aї'city']) )
{
$errorsї'city'] = "City where model is located.";
}
if ( empty($aї'state']) )
{
$errorsї'state'] = "State (or country) where model is located.";
}
if ( empty($aї'category']) || $aї'category'] < 1 )
{
$errorsї'category'] = "You must select a primary category.";
$aї'category'] = 0;
}
foreach ( get_class_vars('models') as $k => $v )
{
if ( isset($aї$k]) )
$this->$k = $aї$k];
}
return $errors;
}
}
$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ї'features']);
if ( isset($features) )
{
foreach ( $features as $key => $val )
{
if ( isset($features_tableї$val]) )
{
$this->featuresї$val] = true;
}
else
{
$errorsї'features'] = "Internal error: unknown feature";
unset($featuresї$key]);
}
}
}
if ( empty($features) && !isset($errorsї'features']) )
{
$errorsї'features'] = "Select at least one feature option.";
};
function get_models($search_id)
{
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ї'object']);
$obj->ID = $rowї'ID']; // may not be assigned when INSERTed
return $obj;
}
function models_search($cat,$quant)
{
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" );
}
function models_textsearch($key,$limit)
{
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" );
}