I have this loop for list rows, what I want to do is return to base 1 or 0 (1 check, 0 uncheck) to that row that is listed. I manage to do this without the WHILE function, but in while function it won't work.
<?php
$interval = $conn->query("SELECT ID,Vrsta_segmenta, Active FROM `msa_segmenti`");
if ($interval->num_rows > 0) {
while($row = $interval->fetch_array()) {
$checked = $row["Active"];
?>
<form name="update" method="POST" action="msa_pauze_admin.php" class="form-horizontal form-label-left">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12"><?php echo $row["Vrsta_segmenta"];?></label>
<div class="col-md-9 col-sm-9 col-xs-12">
<div class="">
<label>
<input type="checkbox" name="check" value="<?php echo $row["Active"];?>" class="js-switch"
<?php
if ($checked == '1') { ?> checked
<?php
} else if ($checked == '0') ""
?>
</label>
</div>
</div>
</div>
<?php }} ?>
<button type="submit" class="btn btn-success" value="Save">Save</button>
Now i need to return value to the db when i click save button. I know i need to check click state so i try this but did not work:
if (isset($_POST["check"])) {
$check_value == '1';
$interval_check_save = "UPDATE `msa_segmenti` SET `Active` = '".$check_value."' WHERE `ID` = '".$id."'";
$mysqli->query($interval_check_save);
} else {
$check_value == '0';
$interval_check_save = "UPDATE `msa_segmenti` SET `Active` = '".$check_value."' WHERE `ID` = '".$id."'";
$mysqli->query($interval_check_save);
}
I manage to solve my problem:
<form name="update" method="POST" action="msa_pauze_admin.php" class="form-horizontal form-label-left">
<?php
if (!empty($_POST['Active'])) {
$interval_check = "UPDATE `msa_segmenti` SET `Active` = 0 ";
$mysqli->query($interval_check);
}
$interval = $conn->query("SELECT ID,Vrsta_segmenta, Active FROM `msa_segmenti`");
if ($interval->num_rows > 0) {
while ($row = $interval->fetch_array()) {
$id = $row["ID"];
if (isset($_POST['Active'][$id])) {
$interval_check = "UPDATE `msa_segmenti` SET `Active` = 1 WHERE `ID` = '" . $_POST['Active'][$id] . "'";
$mysqli->query($interval_check);
}
}
$interval = $conn->query("SELECT ID,Vrsta_segmenta, Active FROM `msa_segmenti`");
while ($row = $interval->fetch_array()) {
//print_r($row);
$checked = $row["Active"];
$id = $row["ID"];
//echo count($_POST['Active']);
//echo "<pre>"; print_r($_POST); exit;
?>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12"><?php echo $row["Vrsta_segmenta"]; ?></label>
<div class="col-md-9 col-sm-9 col-xs-12">
<div class="">
<label>
<input type="checkbox" name="Active[<?php echo $id; ?>]"
value="<?php echo $id; ?>"
class="js-switch" <?php echo ($checked == 1) ? 'checked' : ''; ?>>
</label>
</div>
</div>
</div>
<?php
}
}
?>
<button type="submit" class="btn btn-success" value="Save">Save</button>
</form>
You are using ==
to set the value of variable. This operator is used for equal not for assign value.
Change from
$check_value == '1';
$check_value == '0';
Into
$check_value = '1';
$check_value = '0';
2nd issue
There is missing >
near <?php } else if ($checked == '0') "" ?>
try this, Hope this will work
if (isset($checked)) { ?> checked
<?php
} else { ""
}
?>'
Note: this will work if you have 0 or 1 value in $checked variable No need for elseif :)
If you are unchecked, you will not get any value, so check the post data and write the code to match the behaviour, probably you will get null or empty,if unchecked.