检查元素是否在数组中,然后从与其绑定的另一个数组中获取值

发布于 2025-01-11 01:42:57 字数 2323 浏览 0 评论 0原文

我正在制作一个页面,用户可以在其中选择所有受中断影响的服务并设置其状态。

它的工作方式是,我让他们选中一个复选框,选中该复选框后,会向用户显示一系列状态选择。 现在我可以获得所有受影响的服务,但是当我尝试获取它们的状态时,事情根本不起作用。 基本上我想要实现的是系统为每个受影响的服务设置新状态。 我也尝试过不同的方法,如果只选择一项服务,我最近的方法就有效,而且一团糟。

HTML 代码:

<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>

当前的 PHP 代码(仅适用于一项服务,而且一团糟):

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

旧的 PHP 代码(不起作用,但我认为我在这里做错了一些简单的事情):

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

谢谢, 尼梅图。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩世 2025-01-18 01:42:57

[意见] 我认为最好将状态默认值设置为“不受影响”,因此您不应该同时创建或混淆 2 个数据。并将 id 放入每个状态中。

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

[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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文