【问题标题】:Dropdown select based on database entries基于数据库条目的下拉选择
【发布时间】:2011-01-11 21:50:58
【问题描述】:

所以,我想要一个选择来自动填充下拉菜单的“是”选择,或者“否”。数据库查询的返回值将用于评估将选择哪个下拉列表。

我曾想过,因为它是一个真/假的自动填充,只写 2 个条件,但我认为会有更好的(阅读:不那么混乱的代码)编写代码的方式来自动填充正确的选择下拉列表,基于数据库的结果。

我正在尝试编写的代码的基础是针对所有选择检查变量,然后附加一个字符串,该字符串将为视图中的用户选择该下拉列表。

我的问题是有没有更简单的方法来做到这一点,而不是为每个可能的下拉值编写条件?

请求代码,用 CodeIgniter PHP 编写:

 $this->db->select('row'); 
 $result = $this->db->get('table');

 // This just selects and returns the values. This code does work, I'm just looking for a better way to do this task that someone might know, because I'm going to have drop downs with hundreds of possibilities, of which, I want the predefined one to be selected.

 // Assume $result->row = "Yes"

 if ( $result = "Yes" ) {  
    #Code for echo'ing radio button with "Yes" selected
       }

 else {  
    #Code for echo'ing radio button with "No" selected
       }

【问题讨论】:

  • 你能详细说明你的问题吗?也许显示一些代码?
  • 展示你凌乱的代码,我们会告诉你如何做得更好。

标签: php mysql database codeigniter


【解决方案1】:

你在 CI 中检查过Form Helper 吗?您可以使用form_radio 并在第三个参数中提供您要选择的值:

form_radio('field_name', 'Yes', $result == 'Yes');
form_radio('field_name', 'No', $result == 'No');

第一个参数是字段名称,第二个参数是字段的值,第三个参数是布尔值(TRUE:选中单选,FALSE:未选中)。由于这是一个收音机,所以字段名称应该相同。

【讨论】:

  • 别忘了 $result 是一个行数组,所以需要在 $result = $this->db->get('table')->row()->whateverthefieldnameis;
【解决方案2】:

这样的事情应该可以工作。不过,我对您的要求和数据做了很多假设。

// The selected value
$selectedValue = 'foo';

// Database results, assuming a structure like so
$results = array(
  array(
    'id' => 1,
    'value' => 'foo'
  ),
  array(
    'id' => 2,
    'value' => 'bar'
  )
);

echo '<select name="selectField">';

foreach ($results as $result) {
  echo '<option value="'.$result['id'].'"';

  // Here we see if this result is the selected value.
  // If so, we spit out the HTML so the user's browser renders it as such.
  if ($result['value'] == $selectedValue) {
    echo ' selected="selected"';
  }

  echo '>'.$result['value'].'</option>';
}

echo '</select>';

【讨论】:

  • 这不是你在 CodeIgniter 中的做法。
  • 然而,这个答案并没有错,因为它可以很容易地应用于 Code Igniter。 Code Igniter 没有做任何事情来使这种方法出错。事实上,Code Igniter 鼓励更糟糕的事情,我不会推荐它。把你的反对票投到别处。
【解决方案3】:

您的问题似乎有点令人困惑。您在代码中提到了单选按钮,但您的问题涉及下拉菜单。

如果您需要根据给定值显示一组单选按钮或下拉选项,您可以将您的选择定义为一个数组:

$data = array(
    'yes' => array('option1', 'option2', 'option2', 'etc'),
    'no' => array('option1', 'option2', 'option2', 'etc'),
    'somethingelse' => array('option1', 'option2', 'option2', 'etc')
);

并像这样使用它:

$query = $this->db->select('row')->get();

if ($query->num_rows() > 0) {
    $result = $query->row();

    if (array_key_exists(strtolower($result->row), $data)) {
        foreach ($data[$result->row] as $row) {
            // Use $row as you need
        }
    }
}

您甚至可以将其包装到一个函数中以实现可重用性。

【讨论】:

    【解决方案4】:

    谢谢 Erisco,你刚刚通过你的帖子帮了我一些忙。

    虽然我的版本更基本且精简,没有引用任何键 => 值对。它只是设置一个包含上一个下拉菜单中的所有选项的数组,循环遍历它[因为它也填充下拉菜单],如果它与用户之前选择的选项匹配,则将选项设置为“已选择”并且存入数据库:

    <?php           
    $naybpopulate = array('Bernal Heights', 'Castro', 'Chinatown', 'Cole Valley', 'Fishermans Wharf', 'Forest Hill', 'Haight-Ashbury', 'Hayes Valley', 'Inner Richmond', 'Inner Sunset', 'Japantown', 'Marina', 'Mission', 'Mission Bay', 'Nob Hill', 'Noe Valley', 'North Beach', 'Outer Richmond', 'Outer Sunset', 'Pacific Heights', 'Potrero Hill', 'Presidio', 'Russian Hill', 'SoMa', 'South Beach', 'Telegraph Hill', 'Tenderloin', 'Union Square', 'Western Addition', 'West Portal');
    
            echo '<select name="neighborhood" id="neighborhood">';
    
    foreach ($naybpopulate as $nayb) {
    echo '<option value="'.$nayb.'"';
    
    // Here we see if this result is the selected value.
    // If so, we spit out the HTML so the user's browser renders it as such.
    if ($nayb == $neighborhood) {
    echo ' selected="selected"';
    }
    
    echo '>'.$nayb.'</option>';
    }
    
    echo '</select>';
    ?>
    

    堆栈溢出是最好的。你们摇滚:)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-10
      • 2021-10-23
      • 2012-06-11
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 2015-12-28
      • 2013-05-15
      相关资源
      最近更新 更多