Welcome Guest, Not a member yet? Register   Sign In
dropdown problem
#1

[eluser]crwtrue[/eluser]
i have a form with dropdown which is filled with items gotten from sql-query. I also want a no item option in dropdown and i used array_push to insert the no item option but it dont work. Any ideas? i am using form helper.
#2

[eluser]bretticus[/eluser]
Shouldn't you be prepending to the front of the array?

See array_unshift.

Also, if you have arrays in an array, you can use the array_merge function to build a new array.

Code:
$records = array(array('id'=>1, 'name'=>'bob'), array('id'=>2, 'name'=>'alice'));

$prefix = array(array('0'=>'Please Choose'));

$new_records = array_merge($prefix, $records);
#3

[eluser]Johan André[/eluser]
I use this in my models (usually in MY_Model and then extend my models from it):
Code:
// <option value="$key">$value</option>
// $first_key is the topmost key/value pair of the dropdown (example $key = '-1', $value = 'Select a category').

function convert_to_list($data, $key = null, $value = null, $first_key = '-1', $first_value = 'Nothing selected')
    {
        if ($data)
        {
            
            if($first_key != NULL)
            {
                $keys[] = $first_key;
                $vals[] = $first_value;
            }
            
            foreach ($data as $row)
            {
                $keys[] = $row[$key];
                $vals[] = $row[$value];
            }
                
            if (!empty($keys) && !empty($vals))
            {
                $return = array_combine($keys, $vals);
                return $return;
            }            
        }
        else
        {
            return false;
        }
    }
The $data feeded to the function is a result_array() but can easily be change to use objects instead.
#4

[eluser]joeizang[/eluser]
hey guys,

thought I should chip in here hope this helps,

active record creates multi-dimensional array when you fetch results, and when you want to make a drop down menu in the view it expects a flat array so why not just do your normal pulling of data and say in the model you have

$data = array();
$result = $this->db->get('tablename');
if($result->num_rows()>0)
{
foreach( $result->result_array() as $row)
{
$data[$row['columnname']] = $row['anothercolumnname'];
}
}

this worked for me though so don't know if this is helpful.




Theme © iAndrew 2016 - Forum software by © MyBB