检查元素是否在数组中,然后从与其绑定的另一个数组中获取值
我正在制作一个页面,用户可以在其中选择所有受中断影响的服务并设置其状态。
它的工作方式是,我让他们选中一个复选框,选中该复选框后,会向用户显示一系列状态选择。 现在我可以获得所有受影响的服务,但是当我尝试获取它们的状态时,事情根本不起作用。 基本上我想要实现的是系统为每个受影响的服务设置新状态。 我也尝试过不同的方法,如果只选择一项服务,我最近的方法就有效,而且一团糟。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[意见] 我认为最好将状态默认值设置为“不受影响”,因此您不应该同时创建或混淆 2 个数据。并将 id 放入每个状态中。
HTML :
PHP :
[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 :
PHP :