那么必须有一种更简单,更自动的方法。 我基本上都在为每个单选按钮查询数据库。 这一切都有效,但实施更多内容会让我痛苦,有人能给我一些提示吗?
这是我迄今为止所做的。
checkStatus.php
http://localhost/myproject/first
skap.php
require_once 'connectdb.php';
$skap_NummerID_A1_1 = "A1-1";
$skap_NummerID_A1_2 = "A1-2";
$skap_NummerID_A1_3 = "A1-3";
$skap_NummerID_A1_4 = "A1-4";
$skap_NummerID_A1_5 = "A1-5";
//THIS IS LOCKER NUMBER A1_1
$displayStatus_query_A1_1 = "SELECT Skap_Nummer, Status FROM testskap WHERE Skap_Nummer ='$skap_NummerID_A1_1'";
$resultStatus_A1_1 = $connect_DB->query($displayStatus_query_A1_1);
if ($resultStatus_A1_1->num_rows > 0) {
//output data of each row
while($rowStatus_A1_1 = $resultStatus_A1_1->fetch_assoc()) {
$skap_sjekkStatus_A1_1 = $rowStatus_A1_1["Status"];
}}
//THIS IS LOCKER NUMBER A1_2
$displayStatus_query_A1_2 = "SELECT Skap_Nummer, Status FROM testskap WHERE Skap_Nummer ='$skap_NummerID_A1_2'";
$resultStatus_A1_2 = $connect_DB->query($displayStatus_query_A1_2);
if ($resultStatus_A1_2->num_rows > 0) {
//output data of each row
while($rowStatus_A1_2 = $resultStatus_A1_2->fetch_assoc()) {
$skap_sjekkStatus_A1_2 = $rowStatus_A1_2["Status"];
}}
//THIS IS LOCKER NUMBER A1_3
$displayStatus_query_A1_3 = "SELECT Skap_Nummer, Status FROM testskap WHERE Skap_Nummer ='$skap_NummerID_A1_3'";
$resultStatus_A1_3 = $connect_DB->query($displayStatus_query_A1_3);
if ($resultStatus_A1_3->num_rows > 0) {
//output data of each row
while($rowStatus_A1_3 = $resultStatus_A1_3->fetch_assoc()) {
$skap_sjekkStatus_A1_3 = $rowStatus_A1_3["Status"];
}}
//THIS IS LOCKER NUMBER A1_4
$displayStatus_query_A1_4 = "SELECT Skap_Nummer, Status FROM testskap WHERE Skap_Nummer ='$skap_NummerID_A1_4'";
$resultStatus_A1_4 = $connect_DB->query($displayStatus_query_A1_4);
if ($resultStatus_A1_4->num_rows > 0) {
//output data of each row
while($rowStatus_A1_4 = $resultStatus_A1_4->fetch_assoc()) {
$skap_sjekkStatus_A1_4 = $rowStatus_A1_4["Status"];
}}
//THIS IS LOCKER NUMBER A1_5
$displayStatus_query_A1_5 = "SELECT Skap_Nummer, Status FROM testskap WHERE Skap_Nummer ='$skap_NummerID_A1_5'";
$resultStatus_A1_5 = $connect_DB->query($displayStatus_query_A1_5);
if ($resultStatus_A1_5->num_rows > 0) {
//output data of each row
while($rowStatus_A1_5 = $resultStatus_A1_5->fetch_assoc()) {
$skap_sjekkStatus_A1_5 = $rowStatus_A1_5["Status"];
}}
我喜欢其中的25个,而且我需要做更多,但我想知道有更简单的方法吗?
对于那些想知道的人,这是http://prntscr.com/bjziml
答案 0 :(得分:1)
checkstatus.php
$nummers = array("A1-1", "A1-2", "A1-3", "A1-4", "A1-5");
$output = array();
foreach($nummers as $nummer) {
$output[$nummer] = array();
//THIS IS LOCKER NUMBER A1_1
$query = "SELECT Skap_Nummer, Status FROM testskap WHERE Skap_Nummer ='$nummer'";
$result = $connect_DB->query($query);
if ($result->num_rows > 0) {
//output data of each row
while($row = $result->fetch_assoc()) {
$output[$nummer][] = $row["Status"];
}}
}
skap.php
$nummers = array("A1-1", "A1-2", "A1-3", "A1-4", "A1-5");
foreach($nummer as $nummer) {
echo '<input type="radio" id="'.$nummer.'" name="radios" value="'.$nummer.'" class="radio-toolbar-all-input" '.($output[$nummer] === "Aktiv") ? "disabled" : "").'>
<label class="'.($output[$nummer] === "Aktiv") ? $changecsstoo_Aktiv : $changecsstoo_Inaktiv).'" for="'.$nummer.'">'.$nummer.'</label>';
}
你应该从某个配置文件中获取$ nummers数组
答案 1 :(得分:1)
是的,每当你发现自己重复这样的代码时,通常需要使用一个简单的循环:
$displayStatusResults = [];
$displayStatusQuery = "SELECT Skap_Nummer, Status FROM testskap WHERE Skap_Nummer IN('A1-1','A1-2','A1-3','A1-4','A1-5')";
$result = $connect_DB->query($displayStatusQuery);
if ($result->num_rows > 0) {
while($row= $result->fetch_assoc()) {
$displayStatusResults[$row['Skap_Nummer']] = $row['Status'];
}
}
skap.php
<div class="row">
<?php $count=0;
foreach($displayStatusResults as $skap => $status): $count++;?>
<input
type="radio"
id="<?=$skap?>"
name="radios"
value="<?=$skap?>"
class="radio-toolbar-all-input"
<?= ($status === "Aktiv")? 'disabled':''?>>
<label
class="<?= ($status === "Aktiv")? $changecsstoo_Aktiv : $changecsstoo_Inaktiv?>"
for="<?=$skap?>">
<?=$skap?>
</label>
<?php if($count==8):?>
</div><!--end row--><div class="row">
<?php endif; $count=0;?>
<?php endforeach;?>
</div><!--end row-->