Nniouz Nniouz - 5 months ago 9
SQL Question

Easier way to get individual records, and write them automatically

Well there has got to be an easier, more automatic way to this.
I'm basically querying the database for each radio button.
This is all working, but it'll be a pain for me to implement more, can anyone give me some tips?

This is what I've done so far.

checkStatus.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"];
}}


skap.php

<input type="radio" id="A1-1" name="radios" value="A1-1" class="radio-toolbar-all-input" <?php if ($skap_sjekkStatus_A1_1 === "Aktiv") {echo "disabled";}?>>
<label class="<?php if ($skap_sjekkStatus_A1_1 === "Aktiv") {echo $changecsstoo_Aktiv;} else {echo $changecsstoo_Inaktiv;}?>" for="A1-1">A1-1</label>

<input type="radio" id="A1-2" name="radios" value="A1-2" class="radio-toolbar-all-input" <?php if ($skap_sjekkStatus_A1_2 === "Aktiv") {echo "disabled";}?>>
<label class="<?php if ($skap_sjekkStatus_A1_2 === "Aktiv") {echo $changecsstoo_Aktiv;} else {echo $changecsstoo_Inaktiv;}?>" for="A1-2">A1-2</label>

<input type="radio" id="A1-3" name="radios" value="A1-3" class="radio-toolbar-all-input" <?php if ($skap_sjekkStatus_A1_3 === "Aktiv") {echo "disabled";}?>>
<label class="<?php if ($skap_sjekkStatus_A1_3 === "Aktiv") {echo $changecsstoo_Aktiv;} else {echo $changecsstoo_Inaktiv;}?>" for="A1-3">A1-3</label>

<input type="radio" id="A1-4" name="radios" value="A1-4" class="radio-toolbar-all-input" <?php if ($skap_sjekkStatus_A1_4 === "Aktiv") {echo "disabled";}?>>
<label class="<?php if ($skap_sjekkStatus_A1_4 === "Aktiv") {echo $changecsstoo_Aktiv;} else {echo $changecsstoo_Inaktiv;}?>" for="A1-4">A1-4</label>

<input type="radio" id="A1-5" name="radios" value="A1-5" class="radio-toolbar-all-input" <?php if ($skap_sjekkStatus_A1_5 === "Aktiv") {echo "disabled";}?>>
<label class="<?php if ($skap_sjekkStatus_A1_5 === "Aktiv") {echo $changecsstoo_Aktiv;} else {echo $changecsstoo_Inaktiv;}?>" for="A1-5">A1-5</label>


And I have like 25 of these, and I'm going to need to make even more, but I was wondering is there an easier way to do this?

And for those of you that are wondering, this is http://prntscr.com/bjziml

Answer

Yes, whenever you find yourself repeating code like this, its usually a sign you need to use a simple loop:

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