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

[eluser]Bainzy[/eluser]
Hi everyone

I'm really stuck ATM with dropdown boxes all I am trying to do is have the dropdown box show the category name and have the category ID as the value but it's not working its just filling the dropdown with all the information from my categorys table Sad

how would I go about doing this ?
#2

[eluser]JHackamack[/eluser]
Do you have an example of your code that you're trying to create? This would help in debugging.
#3

[eluser]Bainzy[/eluser]
Right ok the code I have at the momen is as follows

Foreach ($this->ForumModel->fetchAllRows('category') as $row)
{
$options[$row->CategoryID] = $row->Name;
}
$data['options'] = $options;

then is send the data to the view file which is as follows

<?php echo form_dropdown('category', $options); ?>
#4

[eluser]JHackamack[/eluser]
What does $this->ForumModel->fetchAllRows('category') return?
#5

[eluser]Bainzy[/eluser]
That line returns all the rows from the 'Categorys' database so that i can select what data i require from it.

Chris
#6

[eluser]JHackamack[/eluser]
My Question was referring to How CI returns rows.

In order to do a foreach loop it needs to read

Code:
foreach($rows->result() as $row)

but your model has to be returning this:

Code:
$rows = $this->db->get('table');
return $rows;
#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);
}




Theme © iAndrew 2016 - Forum software by © MyBB