U
U
Uzair Izha2015-10-30 20:25:46
PHP
Uzair Izha, 2015-10-30 20:25:46

How to try on cycles? For one to work in the other?

I have this code. The result of this code is this. That 40 checkboxes are displayed. That is, they are copied twice. Because 2 values ​​are output from the database, if 3 values ​​are output, then the checkboxes are copied 3 times. I need the condition to work in checkboxes in such a way that 20 checkboxes will be displayed and 2 of them will be inactive (well, or how many values ​​will be displayed from the database). What's my mistake? How can one loop be directly made to work independently of the other? How to make $d iterate over the values ​​in the second loop and the check goes as it should.

<?php
$rf= mysql_query("SELECT * FROM `zRestoran` WHERE idrest='$id' and data='$data' and time='$time'");
while($row = mysql_fetch_assoc($rf)) {  $d = $row['nomer']; 
for( $x = 1; $x <= 20; $x++ ) {   ?>
<input type="checkbox" <? if($d==$x){?> disabled="disabled" <? } ?> name="stolik[]" value="<? echo $x; ?>"><? echo $x; ?>
<?php } } ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-10-30
@UZEIR

The error is that for each line of the selection from the database, you work out in a cycle.
UPD:
Solution:

<?php
$rf= mysql_query("SELECT * FROM `zRestoran` WHERE idrest='$id' and data='$data' and time='$time'");
$nums = [];
while($row = mysql_fetch_assoc($rf)) {
  $nums[] = $row['nomer']; 
}

for( $x = 1; $x <= 20; $x++ ) {
  <input type="checkbox" <? if (in_array($x, $nums)) echo 'disabled="disabled"'; } ?> name="stolik[]" value="<?= $x ?>"><?=$x ?>
} 
 ?>

A
Alexey, 2015-10-30
@dzheka3d

Why do you need a for loop at all? what are you comparing with what and what do you want to get as a result?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question