List of values and turning them on or off

Hi,

I have a list of values and I have two buttons on the side. If the value is “active” I want to disable it, and vice versa.

At this point I can list all values and display the buttons based on the “active” or “disabled” condition. I can also activate it or disable the values (almost).

I’m using a foreach loop and I got a problem because everything is based on the last element.
Let’s say all values are “active”, this way I can disable the first element because the last element is also active - the system checks the flag of the last element and allows me to disable, however, when the first element is “active” and the last element is “disabled”, I can’t disable the first element because the system sees that the last element is already disabled.

The code is the following:


$flag = array();
$id_notification= $_SERVER['QUERY_STRING'];

<?php
    if(is_array($pushNotification)){
        foreach($pushNotification as $hour){
						
	    $flag[$hour['id_hour']] = 2;
						
            if($hour['notification'] == 'Active'){
							
	        $flag[$hour['id_hour']] = 0;

                echo "<li class=\"teacher\">" . $hour['notification'] . " <a href=\"notification.php?{$hour['id_hour']}\"><img src=\"imagens/delete.png\" alt=\"Delete\" class=\"frm_btn\"/></a></li>";
	    
                if($id_notification!= ''){
	            if($flag[$hour['id_hour']] == 0){
		        $cancelNotification= $query->mudarNotificacaoC($id_notification);
		        if($cancelNotification)
		            header("location: notification.php");
		
                    elseif($flag[$hour['id_hour']] == 1){
			$activateNotification= $query->mudarNotificacaoA($id_notification);
			if($activateNotification)
			    header("location: notification.php");
	         }
}
						
             elseif($hour['notification'] == 'Disabled'){
							
                $flag[$hour['id_hour']] = 1;
							
                echo "<li class=\"teacher\">" . $hour['notification'] . " <a href=\"notification.php?{$hour['id_hour']}\"><img src=\"imagens/confirm.png\" alt=\"Delete\" class=\"frm_btn\"/></a></li>";

                if($id_notification!= ''){

                    if($flag[$hour['id_hour']] == 0){
	                $cancelNotification= $query->mudarNotificacaoC($id_notification);
	                if($cancelNotification)
	                    header("location: notification.php");
	            elseif($flag[$hour['id_hour']] == 1){
	                $activateNotification= $query->mudarNotificacaoA($id_notification);
	                if($activateNotification)
	                    header("location: notification.php");							
                 }
         }
    }
}
?>

So I now my problem is happening because everything is based on the state of the last element, however, I can’t understand why because I’m defining a different value on the flag because I’m using the id of element inside of the flag (array).

ideas?

Thanks in advance.