Welcome Guest, Not a member yet? Register   Sign In
dropdown list from an array
#1

[eluser]xavieremerson[/eluser]
function form_dropdown_from_db($name = '', $array,$data,$selected = array(), $extra = '')
{
//$CI =& get_instance();
if ( ! is_array($selected))
{
$selected = array($selected);
}

// If no selected state was submitted we will attempt to set it automatically
if (count($selected) === 0)
{
// If the form name appears in the $_POST array we have a winner!
if (isset($_POST[$name]))
{
$selected = array($_POST[$name]);
}
}

if ($extra != '') $extra = ' '.$extra;

$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

$att = '';

foreach ($data as $key => $val)
{
$att .= $key . '="' . $val . '" ';
}

$form = '<select name="'.$name.'"'.$extra.$multiple. $att . ">\n";
if (count($array) > 0)
{
foreach ($array as $row)
{
$values = array_values($row);
if (count($values)===2){
$key = (string) $values[0];
$val = (string) $values[1];
//$this->option($values[0], $values[1]);
}

$sel = (in_array($key, $selected))?' selected="selected"':'';

$form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
}
}
$form .= '</select>';
return $form;
}
#2

[eluser]Dennis Rasmussen[/eluser]
Okay? Maybe give us some info and use the code tag?
#3

[eluser]xavieremerson[/eluser]
Please ad that stuff into your own helper

then

in model class

function tool_list()
{
$this->db->select('Tool_Id, Tool_Name');
$this->db->order_by("Tool_Name","ASC");
$query = $this->db->get("Tools");
$data = $query->result_array();
$query->free_result();
return $data;

}


in controller

$tool_list = $this->user_model->tool_list();
$opt = array(
'id' => 'tool_list',
'class' => 'text',
'size' => '3',
);
$data['tool_list'] = form_dropdown_from_db('tool_list[]',$tool_list,$opt, $this->input->post('tool_list'),'multiple');




Theme © iAndrew 2016 - Forum software by © MyBB