dongpenggan6812 2016-01-30 18:35
浏览 38

如何使用会话值自动选择下拉框

        <select>
        <?php  foreach($result as $city) { ?>
        <option value="<?php echo $city->city_name; ?>" <?php if( strtolower($this->session->city) == strtolower($city->city_name)  ) { echo "selected"; }  ?>
        > <?php  echo $city->city_name; ?>  </option>
        <?php   } ?>
        </select>

Hi i am trying to auto select drop down box with the help of session but i am not able to select please some one help me out how could i auto select drop down box.

  • 写回答

2条回答 默认 最新

  • dpjtn48868 2016-01-30 18:54
    关注

    First of all. You should always provide the whole code, eg. in format of PHPFiddle.

    Secondly, you should not mix HTML and PHP.

    Also your problem cannot be deciphered from your question, because there might be several reasons. Like the city name could be in uppercase or capitalized in your session variable.

    Your session variable might not be initialized with your session in the class constructor.

    Also you might even not have a class at all, so the reference to $this would be obsolete.

    But those aside, here's a code that might help you:

    <?php
    class MockSession {
        function __construct() {
        }
    }
    class Foo {
    
    function __construct() {
        $this->session = new MockSession();
        $this->session->city = "Helsinki";
    }
    function printSelect() {
        $result = array("London","New York","Helsinki");
    
        echo '<select>';
        foreach ($result as $city) {
            $sel = '';
            echo $city." ".$this->session->city;
            if (strtolower($city)==strtolower($this->session->city)) {
            $sel = ' selected="selected" ';
            }
            echo '<option value="'.$city.'" '.$sel.'>'.$city.'</option>';
        }
    echo '</select>';
    }
    
    }
    
    $foo = new Foo();
    $foo->printSelect();
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?