如果codeigniter中只有一个值,则显示一个文本框

时间:2014-03-09 14:57:48

标签: php codeigniter

<div class="span3">  
    <select name="u_home_country" data-validation-required-message="Enter your home country" class="form-control" required id="user_home_country">                             
    <?php foreach ($countryOptionshome as $key => $countryName) { ?>
        <option value="<?php echo $key ?>"><?php echo $countryName ?></option>
    <?php } ?>
    </select>
</div>

如果数组中只有一个值,如何显示输入文本框。我正在使用codeigniter

编辑:

我从控制器获取数组值countryOptionshome,因此无法使用count

 foreach ($jsonhome as $queryhome) {
                foreach ($queryhome['results']['place'] as $placeshome) {

                    if (!empty($placeshome['country']['content'])) {
                        $aData['countryOptionshome'][] = $placeshome['country']['content'];
                    }
                    if (!empty($placeshome['admin1']['content'])) {
                        $aData['stateOptionshome'][] = $placeshome['admin1']['content'];
                    }
                    if (!empty($placeshome['admin2']['content'])) {
                        $aData['cityOptionshome'][] = $placeshome['admin2']['content'];
                    }
                }
            }


            $this->load->view('header_register');
            $this->load->view('body_complete_register', $aData);
            $this->load->view('footer_register');

2 个答案:

答案 0 :(得分:1)

我会尝试类似的东西(检查数组中的一个值并创建一个文本框输入:

<?php

if(count($countryOptionshome) == 1)     //or sizeof() which is an alias for count  
       echo form_input('inputName', 'inputValue');
 else {
      //do other stuff
 }

?>

这与如何处理您的代码更相关,但似乎您并不总是设置名为$countryOptionshome的数组,如果能够或不能设置,请使用isset()函数检查视图尝试使用count()

访问它
if (isset($countryOptionshome)) {
  //do your thing with the array

   if(count($countryOptionshome) == 1)     //or sizeof() which is an alias for count  
       echo form_input('inputName', 'inputValue');
   else {
      //do other stuff
 }
}

如果可以调试,如果不注释这一行:

    /*$this->load->view('header_register');
    $this->load->view('body_complete_register', $aData);
    $this->load->view('footer_register');*/

在评论之后添加以下行并请发布/检查输出:

  echo var_dump($aData);

如果需要更多控制输入的属性,请参阅form_helper doc: http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

答案 1 :(得分:0)

尝试这样的事情:

<div class="span3">  
   <?php if(count($countryOptionshome) <= 1)
        echo '<input type="text" name="something" >';
   else { ?>
    <select name="u_home_country" data-validation-required-message="Enter your home country" class="form-control" required id="user_home_country">                             
    <?php 
        foreach ($countryOptionshome as $key => $countryName) { ?>
            <option value="<?php echo $key ?>"><?php echo $countryName ?></option>
    <?php } ?>
    </select>
   <?php } ?>
</div>