简体   繁体   English

PHP下拉问题

[英]PHP drop down issue

My first question, if not appropriate, please ignore. 我的第一个问题,如果不合适,请忽略。

I have a drop down coded in php which is used to select a value. 我有一个下拉列表,用php编码,用于选择一个值。 The value set is 'Off', 0, 1, 2, ... 179 (ie 181 values in total). 设置的值是“关”,0、1、2,... 179(即总共181个值)。

In the beginning, the drop down shows all 181 values correctly. 首先,下拉菜单会正确显示所有181个值。

After the value is selected from drop down, some other button is pressed to call a mysql procedure. 从下拉列表中选择值后,按下其他按钮以调用mysql过程。 On call completion, page is refreshed. 通话完成后,页面将刷新。

The drop down is supposed to retain the selected value across page refreshes, which it does. 下拉菜单应该在页面刷新过程中保留所选的值。 User should be allowed to change the values though. 虽然应该允许用户更改值。

However, after the first page refresh, If I try to select a different value from the drop down, All I see in there is the previously selected value 181 times. 但是,在刷新第一页之后,如果我尝试从下拉列表中选择一个不同的值,那么我所看到的全部就是先前选择的值181次。

Below is the code, could someone please help fix it. 下面是代码,有人可以帮助修复它。

I am not a php programmer, just trying to fix a glitch that my resource left in there. 我不是PHP程序员,只是试图解决我的资源留在那里的故障。

<select style="width: 87px; height:35px;color:#113C83;margin-bottom:0;border-right-width:0;" id="angle1">
<option value="Off">Off</option>
<?php
for($i=0;$i<=179;$i++)
{
      if(isset($_SESSION['angle1']))
      {
         ?>
         <option value="<?php echo $_SESSION['angle1']; ?>" selected><?php echo $_SESSION['angle1']; ?></option>
         <?php
      }
      else
      { ?>
     <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
     <?php 
      } 
    } ?>
</select>

This is because you are testing the existance of $_SESSION['angle1'] with isset($_SESSION['angle1'] but not that the value it contains matches the current loop counter $i 这是因为您正在使用isset($_SESSION['angle1']测试$_SESSION['angle1']的存在,但并不是包含的值与当前循环计数器$i相匹配。

This tests correctly, assuming that $_SESSION['angle1'] contains the value of the previous selected angle. 假设$_SESSION['angle1']包含上一个选定角度的值,则此测试可以正确测试。

This also simplifies your code considerably by using a ternary operator to make that decision 通过使用三元运算符做出决定,这也大大简化了代码

<select style="width: 87px; height:35px;color:#113C83;margin-bottom:0;border-right-width:0;" id="angle1">
    <option value="Off">Off</option>
<?php
    for($i=0; $i <= 179; $i++) {
        $sel = isset($_SESSION['angle1']) && $_SESSION['angle1'] == $i ? 'selected="selected" : '';

        echo '<option ' . $sel . ' value="' . $i . '">' . $i .'</option>';       
    }
?>

That's because you don't specify the correct condition: 那是因为您没有指定正确的条件:

<select style="width: 87px; height:35px;color:#113C83;margin-bottom:0;border-right-width:0;" id="angle1">
<option value="Off">Off</option>
<?php
for($i=0;$i<=179;$i++)
{
      if(isset($_SESSION['angle1']) && ($i == $_SESSION['angle1'])) //when you refresh isset($_SESSION['angle1']) will be always true
      {
         ?>
         <option value="<?php echo $_SESSION['angle1']; ?>" selected><?php echo $_SESSION['angle1']; ?></option>
         <?php
      }
      else
      { ?>
     <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
     <?php 
      } 
    } ?>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM