嗨,我正在尝试在while循环中的一系列文本框中显示一些数据。我想根据上一页的选择选项来填写输入字段。如果选择的值为25,则数据应仅每四行显示一次。如果为50,则应在每个备用行中显示数据,对于100%,将显示所有值。我已经标记了需要应用所选值的条件的位置。
previous.php
<select class="form-control" name="select36">
<option value="100">100%</option>
<option value="50">50%</option>
<option value="25">25%</option>
add.php
<?php
<!-- MYSQL QUERY -->
$sql = "select id ,code, addl_item_code_barcode, main_group,
item_name_w_shade_category, shade_name, size , current_stock from items
where main_group in (SELECT distinct(main_group) FROM items WHERE
addl_item_code_barcode IN ($d) and status='active') and status='active'
order by item_name_w_shade_category, size, shade_name";
$result = pg_query($db, $sql);
$counter = 1;
$i = 1;
while ($res = pg_fetch_assoc($result)) { <!-- 1st loop -->
$i++;
?>
<td>
<!-- THIS IS WHERE THE CONDITION NEEDS TO BE APPLIED FOR SELECTED VALUE-->
<?php
if(isset($_REQUEST['select36'])){
<!-- FOR 100% (DISPLAY DATA IN EVERY ROW)-->
if($_REQUEST['select36'] == 100){
?>
<input type="text" class="form-control" size ="1" name="p36" value =<?php
echo $balqty; ?> disabled> <br>
<?php
if (isset($_REQUEST['add1'])) {
$var1 = $_REQUEST['add1'];
if ($var1 > $balqty) {
?>
<input type="text" class="form-control check-change<?php echo $counter; ?>
saisie add1" style="background-color: whitesmoke;" size ="5"
name="qty_1<?php echo $res['id'] ?>" id="qty_1<?php echo $res['id'] ?>"
value ="<?php echo $balqty ?>" onchange="calculateTotal()" readonly>
<?php }
else {
?>
<input type="text" class="form-control check-change<?php echo $counter; ?>
saisie add1" maxlength="4" size ="5" name="qty_1<?php echo $res['id'] ?>"
id="qty_1<?php echo $res['id'] ?>" onchange="calculateTotal()" value ="<?
php echo $var1 ?>">
<?php
}
?>
<?php
} else {
$var1 = 0;
if ($var1 > $balqty) {
?>
<input type="text" class="form-control check-change<?php echo $counter; ?>
saisie add1" style="background-color: whitesmoke;" size ="5" name="qty_1<?
php echo $res['id'] ?>" id="qty_1<?php echo $res['id'] ?>" value ="<?php
echo $balqty; ?>" onchange="calculateTotal()" readonly>
<?php }
else {
?>
<input type="text" class="form-control check-change<?php echo $counter; ?>
saisie add1" maxlength="4" min="0" size ="5" name="qty_1<?php echo
$res['id'] ?>" id="qty_1<?php echo $res['id'] ?>" value ="<?php echo $var1 ?
>" onchange="calculateTotal()" readonly>
<?php
}
}
}
<!-- END FOR 100% (DISPLAY DATA IN EVERY ROW)-->
<!-- FOR 50% (DISPLAY DATA IN ALTERNATE ROWS) -->
elseif($_REQUEST['select36'] == 50){
if ($i%2==0) {
?>
<input type="text" class="form-control" size ="1" name="p36" value =<?php
echo $balqty; ?> disabled> <br>
<?php
if (isset($_REQUEST['add1'])) {
$var1 = $_REQUEST['add1'];
if ($var1 > $balqty) {
?>
<input type="text" class="form-control check-change<?php echo $counter; ?>
saisie add1" style="background-color: whitesmoke;" size ="5"
name="qty_1<?php echo $res['id'] ?>" id="qty_1<?php echo $res['id'] ?>"
value ="<?php echo $balqty ?>" onchange="calculateTotal()" readonly>
<?php }
else {
?>
<input type="text" class="form-control check-change<?php echo $counter; ?>
saisie add1" maxlength="4" size ="5" name="qty_1<?php echo $res['id'] ?>"
id="qty_1<?php echo $res['id'] ?>" onchange="calculateTotal()" value ="<?
php echo $var1 ?>">
<?php
}
?>
<?php
} else {
$var1 = 0;
if ($var1 > $balqty) {
?>
<input type="text" class="form-control check-change<?php echo $counter; ?>
saisie add1" style="background-color: whitesmoke;" size ="5" name="qty_1<?
php echo $res['id'] ?>" id="qty_1<?php echo $res['id'] ?>" value ="<?php
echo $balqty; ?>" onchange="calculateTotal()" readonly>
<?php } else {
?>
<input type="text" class="form-control check-change<?php echo $counter; ?>
saisie add1" maxlength="4" min="0" size ="5" name="qty_1<?php echo
$res['id'] ?>" id="qty_1<?php echo $res['id'] ?>" value ="<?php echo $var1 ?
>" onchange="calculateTotal()" readonly>
<?php
}
}
}
}
<!-- END FOR 50% (DISPLAY DATA IN ALTERNATE ROWS) -->
}
?>
<!-- THIS IS WHERE THE CONDITION NEEDS TO END FOR THE SELECTED VALUE -->
</td>
<?php
$counter = $counter + 1;
}
This is how it should look like
if(isset($_REQUEST['select36'])){
if($_REQUEST['select36'] == 25){
display data in every 4th row <!-- KINDLY HELP ME WITH THIS LOGIC -->
}
else if($_REQUEST['select36'] == 50){
display data in alternate row
}
else if($_REQUEST['select36'] == 100){
display data in every row
}
}
还请记住,即使未显示数据,也需要显示空白文本框。任何帮助表示赞赏。谢谢!
答案 0 :(得分:0)
我无法提供完整的源代码。
但是,我可以向您解释适用于您的伪代码:
如果100%->无需逻辑,只需为每个文本框赋值。
如果50%->进行偶数校验
else if($_REQUEST['select36'] == 100) {
if ($i%2==0) {
$value = 'Your-Input-Value';
}
else {
$value = '';
}
如果百分比为25%,则必须为每个第4个字段赋值。
这有点棘手。
手动运行:
0 -- No Value
1 -- No Value
2 -- No Value
**3 -- Yes Value**
4 -- No Value
5 -- No Value
6 -- No Value
**7 -- Yes Value**
8 -- No Value
9 -- No Value
10 -- No Value
**11 -- Yes Value**
12 -- No Value
在此处采用当前值1
的迭代变量
将其循环增加。
将计数器移位:
1 -- No Value
2 -- No Value
3 -- No Value
**4 -- Yes Value**
5 -- No Value
6 -- No Value
7 -- No Value
**8 -- Yes Value**
9 -- No Value
10 -- No Value
11 -- No Value
**12 -- Yes Value**
13 -- No Value
现在,每一个可被4整除的计数器都具有价值。
因此,在您的循环中:
$i=1;
while (...) {
++$i;
if ($i%4==0) {
$value = 'Your-Input-Value';
}
else {
$value = '';
}
}
还有您的文本框:
<input type="text" class="form-control check-change<?php echo $counter; ?> saisie add1" maxlength="4" min="0" size ="5" name="qty_1<?php echo $res['id'] ?>" id="qty_1<?php echo $res['id'] ?>" value ="<?php echo $value;?>" onchange="calculateTotal()" readonly>