Welcome Guest, Not a member yet? Register   Sign In
Dropdown Form sampel needed
#1

[eluser]reneschaub[/eluser]
Hi all. Im looking for a Dropdown Menue Sampel where gets the Data from a Database.
I saw in autoload its load the form_helper but i don`t understand if the forms get from form helper or in view files?
#2

[eluser]Kyle Johnson[/eluser]
Code:
// The query goes in the controller and gets passed to the view
// Controller Code
$query = $this->db->query('SELECT id, name FROM table');

// Convert the id and name to a proper array for the dropdown helper.
foreach($query->result_object() as $val)
{
$data['form'][$val->id] = $val->name;
}

$this->load->view('myview',$data); // Pass data to view

// View Code
echo form_open();
echo form_select('mydropdownname', $form); // load array data into dropdown
echo form_close();

I haven't tested this, but I'm pretty sure that's what you need?
#3

[eluser]reneschaub[/eluser]
Thx alot!
I will try out today Smile
#4

[eluser]The Wizard[/eluser]
Code:
function Custom_dropdown_city( $name, $country_code, $selected_id = '', $reload = FALSE )
    {

        $this->load->database('c88v2');

        $this->db->select('id, name, name_local');
        $this->db->from('c88v2_city');

        $this->db->where('country_code', $country_code );

        $this->db->order_by('display_order', 'desc');
        $this->db->order_by('name', 'asc');

        $query = $this->db->get();

        $num_rows = $query->num_rows();

        $result_array = array();

        if ( $num_rows <= 0)
        {
            //$result_array['-1'] = '-';
            $result_array['0'] = ' - IF EMPTY, SELECT THIS - ';
        }
        else
        {

            $result_array['-1'] = ' - Select - ';

            foreach ($query->result_array() as $row)
            {
                $result_array[ $row['id'] ] = $row['name'];
            }

        }

        $javascript = $reload == TRUE ? 'onchange="this.form.submit();"' : '';

        $this->load->helper('form');

        return form_dropdown( $name, $result_array, $selected_id, $javascript );

    }

hope it helps Smile
#5

[eluser]reneschaub[/eluser]
Hmmm it looks well but i don`t understand where i must insert the code?

I think this comes in system\helpers\form_helper.php
--------------------------------------------------------------------------------------
function Custom_dropdown_city( $name, $country_code, $selected_id = '', $reload = FALSE )
{

$this->load->database('c88v2');

$this->db->select('id, name, name_local');
$this->db->from('c88v2_city');

$this->db->where('country_code', $country_code );

$this->db->order_by('display_order', 'desc');
$this->db->order_by('name', 'asc');

$query = $this->db->get();

$num_rows = $query->num_rows();

$result_array = array();

if ( $num_rows <= 0)
{
//$result_array['-1'] = '-';
$result_array['0'] = ' - IF EMPTY, SELECT THIS - ';
}
else
{

$result_array['-1'] = ' - Select - ';

foreach ($query->result_array() as $row)
{
$result_array[ $row['id'] ] = $row['name'];
}

}

--------------------------------------------------------------------------------------

And i think this in application\views\form_view.php

$javascript = $reload == TRUE ? 'onchange="this.form.submit();"' : '';

$this->load->helper('form');

return form_dropdown( $name, $result_array, $selected_id, $javascript );

}

--------------------------------------------------------------------------------------

Right?
#6

[eluser]The Wizard[/eluser]
no no,
its meant to be put into the model and called like

$this->model_x->Custom_dropdown_city( 'dropdown_city', 'TR', '', TRUE );

this would return all the cities from the country code TR for example istanbul ankara antalya & etc.

if you would specify a parameter for the select_id it would let it come as selected.

the TRUE / FALSE at the end indicates that for example if you chose a city from the dropdown,
the form will submit (reload).



now if you chose something and the form submits, its wise that you submit the selected id so after
you re populate the form in validation, you get the city selected.

its like the hotmail registration page, you chose a country and the form reloads & so on.. Smile

hope it helps.
#7

[eluser]The Wizard[/eluser]
ist sehr hilfreich dieses stuck codeschen Smile
#8

[eluser]reneschaub[/eluser]
Ok danke. Na dann hoffe ich das es heute abend klappt und ich es endlich schnalle Smile
#9

[eluser]reneschaub[/eluser]
Hmm erhalte folgenden fehler:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$model_form

Filename: views/form_view.php

Line Number: 20

Fatal error: Call to a member function Custom_dropdown_city() on a non-object in C:\wamp\www\regform\system\application\views\form_view.php on line 20
#10

[eluser]The Wizard[/eluser]
also jut Smile

das stuckchen code das soll in das model rein.
das schickt nen query zu der datenbank und liest da die stadte ab.
da wir ein array haben,
Code:
$result_array[ $row['id'] ] = $row['name'];

haben wir am ende ein VOLLES array was wir dann mit

Code:
return form_dropdown( $name, $result_array, $selected_id, $javascript );
in ein volles dropdown menu verwandeln konnen.

das konnen wir dann zb. in eine variable tuhen so in der art von

$data['staedte'] = $this->model_x->Custom_dropdown_city( .. );

und dann in die view mit

&lt;?= $staedte ?&gt; einsetzen.

bei dir scheint ubrigens das model nicht geladen zu sein Smile
muss heute noch japanisch uben, werde morgen wieder versuchen zu helfen.

bis denne Smile




Theme © iAndrew 2016 - Forum software by © MyBB