Welcome Guest, Not a member yet? Register   Sign In
Form dropdown from db data
#1

[eluser]abmcr[/eluser]
Code:
/**
* Drop-down Menu FROM database
*
* @access    public
* @param    string
* @param    array
* @param    string
* @param    string
* @return    string
*/

    function form_dropdown_from_db($name = '', $sql, $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"' : '';

        $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
        $query=$CI->db->query($sql);
        if ($query->num_rows() > 0)
        {
           foreach ($query->result_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;
    }

Put it into a MY_Form_helper...
into the view use
Code:
echo form_dropdown_from_db('prova', "SELECT id,c_txtitaliano FROM categorie");
May be useful .....
#2

[eluser]Référencement Google[/eluser]
Your idea of having a form dropdown helper from DB results is good, but I don't like much that it needs to have SQL commands in a view. What better solution can we think of?
#3

[eluser]Iverson[/eluser]
[quote author="Too Pixel" date="1226822978"]Your idea of having a form dropdown helper from DB results is good, but I don't like much that it needs to have SQL commands in a view. What better solution can we think of?[/quote]

I agree. I was thinking the same thing. I too like the idea though so this might be a little better implementation. I separated the actual data manipulation from the display part. In your controller (I would probably run this in a model), you would run...

Code:
$form_data = form_dropdown_data('prova', "SELECT id,c_txtitaliano FROM categorie")

Then you could send your form to your view...
Code:
$form_display = form_dropdown_display($form_data);

if($form_display)
{
   echo $form_display;
}


Helper File
Code:
function form_dropdown_data($name = '', $sql, $selected = array())
{
    $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"' : '';
    $query=$CI->db->query($sql);
    return $query->result_array();
}

function form_dropdown_display($data, $extra = '')
{
    // Only do the work if we have at least one result
    if (count($data) > 0)
    {
        $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
        foreach ($data 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;
    }
    else
    {
        return FALSE;
    }
}
#4

[eluser]Référencement Google[/eluser]
That make sense now, thanks for sharing.
#5

[eluser]Nicholai[/eluser]
I'm pretty new to CodeIgniter (and PHP in general) and receive multiple errors trying to use this helper. However, the drop-down appears below the errors and is pulling data normally, though the "name" parameter of the option list is blank. From what I can tell, my controller follows the function as it should:
Code:
function addnew()
{
$data['title'] = "Create A New Task";
$data['dropdown'] = form_dropdown_data('tskType', "SELECT ID, ttName from tasktypes");
$this->load->view('add_new_task_view',$data);
}

And my view calls it correctly:
Code:
&lt;?php echo form_open('tasks/insert_task'); ?&gt;
<table>
<tr><td>Type: </td><td>&lt;?php echo form_dropdown_display($dropdown); ?&gt;</td></tr>
<tr><td>Location: </td><td>&lt;input type="text" name="tskLocation" /&gt;&lt;/td></tr>
<tr><td>Comments: </td><td>&lt;input type="text" name="tskComments" /&gt;&lt;/td></tr>
<tr><td>Status: </td><td>&lt;input type="text" name="tskStatus" /&gt;&lt;/td></tr>
<tr><td>&lt;input type="submit" value="Create Task" /&gt;&lt;/td></tr>
&lt;/form&gt;

When loading the view, I get these errors.
Undefined variable: extra
Undefined variable: name
Undefined variable: multiple
Undefined variable: selected
in_array() [function.in-array]: Wrong datatype for second argument

Do you have any advice for what I may be doing wrong? I love this helper!
#6

[eluser]xavieremerson[/eluser]
hi..
I'm very new to codeigniter... This article is very much helped me. But I am changed the sql statements in this. Instead of that I used an array variable..

Here is the code ....

Any way thankz.... for giving idea...


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;
}
#7

[eluser]asppp[/eluser]
Hello, I liked the first method you described abmcr,

It works, but in my view prints out (example):

Code:
<select name="fruits">
    <option value="1">apples</option>
    <option value="2">pears</option>
    <option value="3">bananas</option>
</select>

And I want it to print out the fruitnames in the "value",

like this:

Code:
<select name="fruits">
    <option value="apples">apples</option>
    <option value="pears">pears</option>
    <option value="bananas">bananas</option>
</select>

How do I do that???
#8

[eluser]Unknown[/eluser]
[quote author="asppp" date="1292215231"]Hello, I liked the first method you described abmcr,

It works, but in my view prints out (example):

Code:
<select name="fruits">
    <option value="1">apples</option>
    <option value="2">pears</option>
    <option value="3">bananas</option>
</select>

And I want it to print out the fruitnames in the "value",

like this:

Code:
<select name="fruits">
    <option value="apples">apples</option>
    <option value="pears">pears</option>
    <option value="bananas">bananas</option>
</select>

How do I do that???[/quote]

Change line 49 to the following:
Code:
$form .= '<option value="'.$val.'">'.$val."</option>\n";




Theme © iAndrew 2016 - Forum software by © MyBB