Welcome Guest, Not a member yet? Register   Sign In
Help with dropdown
#7

[eluser]danmontgomery[/eluser]
I have a helper that I use to generate select fields from database tables. It will generate a select from the first two columns in a table, optionally you can pass it some SQL (which should return at least 2 columns, the first two will be the value and displayed option), or an array which it passes to the native form_dropdown() function.

Code:
echo make_select("my_field", "table_name", set_value("my_field"));
echo make_select("my_field", array("apple" => "red", "banana" => "yellow"), set_value("my_field"));
echo make_select("my_field", false, set_value("my_field"), array("default_value" => FALSE, "sql" => "SELECT id, CONCAT_WS(' ', firstname, lastname) FROM people"));

Params:
$name: Name of field
$table: Table in DB (optionally, an assoc array as value => display)
$selected_value: Option selected by default
$options: Array of options:
default_value: Default option at the top of the list of options, has no value ("Select One..." by default, FALSE to disable)
params: Attributes to append to the <select> tag, given as an assoc. array
sql: Optional SQL statement to construct the select from

So, in your case, assuming there are only 2 columns (CategoryID and Name) in the categories table (or they are the first 2 columns), you could just use:

Code:
echo make_select("category", "categories", set_value("category"));

otherwise,

Code:
echo make_select("category", false, set_value("category"), array("sql" => "SELECT `CategoryID`, `Name` FROM `categories` ORDER BY `Name` ASC"));

Hope this helps.

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// ------------------------------------------------------------------------

/**
* Form Helper Extension
*/

// --------------------------------------------------------------------

/**
* make_select
*
* Construct a single or multiple select dropdown by giving a table name
*
* @access   public
* @param    string
* @param    mixed
* @param    mixed
* @param    array
*
* @return   string
*/

function make_select($name, $table, $selected_value = false, $options = array() ){

    $ret = array();
    
    if(!isset($options['default_value']) || $options['default_value'] === NULL)
        $options['default_value'] = "Select One...";
  
    if($options['default_value'] !== false)
        $ret[''] = $options['default_value'];

    // If an array, just pass parameters to form_dropdown() helper function
    if(is_array($table)){
        foreach($table as $key => $value){
            $ret[$key] = $value;
        }
    }else{
        if($ci =& get_instance()){
            $ci->load->database();
    
            if(isset($options['sql']) && strlen($options['sql']))
                $query = $ci->db->query($options['sql']);
            else
                $query = $ci->db->get($table);
            
            if($query->num_rows() > 0){
                foreach($query->result() as $row){
                    $row = (array)$row;
                    // Must have at least 2 columns
                    if(count($row) < 2){
                        show_error("Insufficient number of columns passed to make" . ( $options['multi'] ? "_multi" : "" ) . "_select()");
                        return false;
                    }
    
                    $ret[array_shift($row)] = array_shift($row);
                }
            }
        }else{
            show_error("Unable to access CI instance");
            return false;
        }
    }
    $opt_str = "";
    
    if(isset($options['multi']) && $options['multi'] === true){
        $options['params']['class'] = ( isset($options['params']['class']) ? $options['params']['class'] . " " : "" ) . "multiselect";

        if(!isset($options['params']['id']))
            $options['params']['id'] = preg_replace(array("/\s/", "/[^A-Za-z0-9-]/"), array("-", ""), $name);
    }
    
    if(isset($options['params']) && is_array($options['params']) && count($options['params'])){
        $arr = array();
        foreach($options['params'] as $key => $value){
            $arr[] = $key . '="' . addslashes($value) . '"';
        }
        $opt_str = implode(" ", $arr);
    }
    
    return ( isset($options['multi']) && $options['multi'] === true ? form_multiselect($name, $ret, $selected_value, $opt_str) : form_dropdown($name, $ret, $selected_value, $opt_str) );
}

// --------------------------------------------------------------------

/**
* make_multi_select
*
* Construct a multiple select dropdown by giving a table name
* Disables selected_value and sets multi = true by default, passes information to make_select().
*
* @access   public
* @param    string
* @param    mixed
* @param    mixed
* @param    array
*
* @return   string
*/
function make_multi_select($name, $table, $selected_value = false, $options = array()){
    $options['multi'] = true;
    $options['default_value'] = false;
    return make_select($name, $table, $selected_value, $options);
}


Messages In This Thread
Help with dropdown - by El Forum - 01-24-2010, 01:03 PM
Help with dropdown - by El Forum - 01-24-2010, 01:34 PM
Help with dropdown - by El Forum - 01-24-2010, 04:32 PM
Help with dropdown - by El Forum - 01-24-2010, 08:19 PM
Help with dropdown - by El Forum - 01-25-2010, 01:55 AM
Help with dropdown - by El Forum - 01-25-2010, 09:28 AM
Help with dropdown - by El Forum - 01-25-2010, 10:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB