Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)


<?
//Template Class - handless all templating tasks (displaying templates, alerts, errors)

class Template
{
    private $data;
    private $alertTypes;

    //Constructor
    function __construct() {}

    //Functions
    function load($url)
    {
        include($url);
    }

    function redirect($url)
    {
        header("Location: $url");
    }

    //Get/Set Data

    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 '';
        }
    }

    // Get/Set Alerts
    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
PHP
// Check if $myList is indeed an array or an object.
if (is_array($alertTypes) || is_object($alertTypes))
{
  // If yes, then foreach() will iterate over it.
  foreach($this->alertTypes as $alert){
      //Do something.
  }
}

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:
PHP
<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>
Posted
Updated 18-Dec-20 7:43am
v6
Comments
Richard Deeming 18-Dec-20 11:42am    
It's not clear which is line 59 in your first code block, since there are two foreach loops.

I've guessed that it's the inner one that's iterating over the session variable.
Demetri K.2 18-Dec-20 11:46am    
The outer one: " foreach ($this->alertTypes as $alert)".

Richard MacCutchan 18-Dec-20 13:30pm    
Where is the code that sets the alertTypes? We can see the function, but not the code that actually passes the array into it.
Demetri K.2 18-Dec-20 13:46pm    
function setAlert($value, $type = null)
{
if ($type == ''){$type = $this->alertTypes[0];}

$_SESSION[$type][] = $value;

This?
Richard MacCutchan 19-Dec-20 4:33am    
Nothing in that code sets the value of alertTypes. Is this code that you wrote, or copied from the internet somewhere?

1 solution

It's not clear if the code block is part of a class, but if it is not then what does $this->alert refer to? A common use of this-> would be to access a class member but in a plain vanilla function, what scope do you think it's coming from?

You need to figure out where is that external-to-the-function array coming from and, if not a class member of an (unseen in the post) class then you'll need to pass it in as a function argument?
 
Share this answer
 
v2
Comments
Demetri K.2 18-Dec-20 13:30pm    
It's part of a class. I've updated the original post with the entire file.
W Balboos, GHB 18-Dec-20 13:38pm    
/ Check if $myList is indeed an array or an object.
if (is_array($myList) || is_object($myList))
{
// If yes, then foreach() will iterate over it.
foreach($myList as $arrayItem){
//Do something.
}
}

Makes the error go away. Think: that means it doesn't try to execute the foreach() loop. That means the it failed both of your conditional tests. And if you think about it, that is why it fails in your program.

Check to see if you're actually putting data in $myList before you try to loop through it. If it has no data (but exists) it may be null or an arbitrary value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900