2010-02-01 100 views
2

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

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

我想写将检查对所有选择的变量,则追加,将选择下拉在视图中的用户的字符串代码的基础。

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

代码,写在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 
     } 
+2

你能详细解释一下你的问题吗?也许显示一些代码? – 2010-02-01 21:02:10

+1

显示你的乱码,我们会告诉你如何做得更好。 – Flavius 2010-02-01 21:07:13

回答

3

你有没有检查CI Form Helper?您可以使用form_radio和供应要在第三个参数选择数值:

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

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

+1

不要忘记$ result是一个行数组,所以你需要添加$ result = $ this-> db-> get('table') - > row() - > whateverthefieldnameis; – 2010-02-02 14:59:58

0

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

// 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>'; 
+0

这实际上不是你在CodeIgniter中做的。 – 2010-02-02 14:55:37

+0

然而,这个答案并没有错,因为它可以很容易地应用于代码点火器。Code Igniter没有做任何事情来使这种方法错误。实际上,Code Igniter鼓励更糟糕的事情,我不会推荐它。在其他地方投下你的票。 – erisco 2010-02-02 18:19:46

0

你的问题似乎有点混淆。你在代码中提到了单选按钮,但是你的问题是关于下拉菜单的问题。

如果您需要显示一组单选按钮或基于给定值下拉选项中,你可以定义你的选择,作为一个数组:

$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 
     } 
    } 
} 

你甚至可以把它包装成一个可重用的函数。

0

感谢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>'; 
?> 

Stack Overflow是最好的。你们好棒 :)。

相关问题