Nimetu
Nimetu

Reputation: 59

Check if element is in array then get value from another array tied to it

I'm making a page where a user can select all of their services that are affected by an outage and set their status.

The way it works is, I have them check a checkbox, after the checkbox is checked the user is presented with a selection of statuses. Now I can get all of the affected services but when I try to get their status things don't work at all. Basically what I'm trying to achieve is that the system sets the new status for every affected service. I have also tried different approaches and my most recent one works if only one service is selected and it is just a mess.

HTML Code:

<label style="width: 100%;">
    <input type="hidden" name="id[]" value="Service ID">
    <input type="checkbox" class="affectedChecker" name="affected[]" value="Service ID">
    <span>Service Title</span>
    <select name="status[]" class="form-control animated--grow-in ml-2" style="width: 30%; display: inline;" hidden="hidden">
        <option value="Operational">Operational</option>
        <option value="Degraded Performance">Degraded Performance</option>
        <option value="Minor Outage">Minor Outage</option>
        <option value="Partial Outage">Partial Outage</option>
        <option value="Major Outage>Major Outage</option>
    </select>
</label>

Current PHP code (Works with only one service and is a complete mess):

if(!empty($_POST['id'])) {
    foreach($_POST['id'] as $id) {
        if(!empty($_POST['affected'])) {
            if(in_array($id,$_POST['affected'])){
                if(!empty($_POST['status'])) {
                    foreach($_POST['status'] as $status) {
                        $cstatus->updateStatus($status, $page_id, $id);
                    }
                }
            }
        }
    }
}

Old PHP code (Doesn't work but I think there is just something simple I'm doing wrong here):

if(!empty($_POST['affected'])) { //Kui on valitud affected serviceid, siis
    $affected = implode(",",$_POST['affected']); //Paneb komadega järjestusse kõik servicid, mis valiti, need ongi need servicid, mis on affected
    $cstatus->createIncident($title, $incidentstatus, $message, $affected, $page_id, $cid); //Loob intsidendi andmebaasis
    if(isset($_POST['affected'])) {
        print_r($_POST); //It works until this point and gets the correct amount of affected services in the array
        foreach($_POST['affected'] as $value){ //Doesn't work anymore from here
            $id = $_POST['id'];
            $status = $_POST['status'];
            echo $status;
            $cstatus->updateStatus($status, $page_id, $id);
        }
    }
}

Thanks, Nimetu.

Upvotes: 0

Views: 33

Answers (1)

zaidysf
zaidysf

Reputation: 502

[Opinionated] I think it's better to have default value of status as "Not Affected", so you should not create or confusing about 2 data at the same time. And also put id inside each status.

HTML :

<label style="width: 100%;">
    <span>Service Title</span>
    <select name="status[<?php echo $value->id; ?>]" class="form-control animated--grow-in ml-2" style="width: 30%; display: inline;">
        <option value="">Not Affected</option>
        <option value="Operational">Operational</option>
        <option value="Degraded Performance">Degraded Performance</option>
        <option value="Minor Outage">Minor Outage</option>
        <option value="Partial Outage">Partial Outage</option>
        <option value="Major Outage">Major Outage</option>
    </select>
</label>

PHP :

foreach($_POST['status'] as $key => $value) {
    if (empty($value)) {
        continue;
    }
    $cstatus->updateStatus($value, $page_id, $key);
}

Upvotes: 2

Related Questions