This piece of code should display a set of alert messages.
Warning: Invalid argument supplied for foreach() in C:\Program Files\Ampps\www\Login OOP MCV\models\m_template.php on line 59
The line that causes the error -
foreach ($this->alertTypes as $alert)
<?
class Template
{
private $data;
private $alertTypes;
function __construct() {}
function load($url)
{
include($url);
}
function redirect($url)
{
header("Location: $url");
}
function setData($name, $value)
{
$this->data[$name] = htmlentities($value, ENT_QUOTES);
}
function getData($name)
{
if (isset($this->data[$name]))
{
return $this->data[$name];
}
else
{
return '';
}
}
function setAlertTypes($types)
{
$this->$alertTypes = $types;
}
function setAlert($value, $type = null)
{
if ($type == ''){$type = $this->alertTypes[0];}
$_SESSION[$type][] = $value;
}
function getAlerts()
{
$data = '';
foreach ($this->alertTypes as $alert)
{
if (isset($_SESSION[$alert]))
{
foreach($_SESSION[$alert] as $value)
{
$data .= '<li class"' . $alert . '">' . $value . '</li>';
}
unset($_SESSION[$alert]);
}
}
return $data;
}
}
What I have tried:
I've tried putting the foreach in question in an if which checks if it's an array or an object
if (is_array($alertTypes) || is_object($alertTypes))
{
foreach($this->alertTypes as $alert){
}
}
The error goes away, but I still don't get the alert messages to show.
Maybe it's a problem with the code to display the messages?
Here it is:
<h1><?php echo $this->getData('name');?></h1>
<?php
$alerts = $this->getAlerts();
if ($alerts != '') {echo '<ul class="alerts">' . $alerts . '';}
?>
<div id="content">
<p>Test</p>
<a href="members.php">Link</a>
</div>