Welcome Guest, Not a member yet? Register   Sign In
Dynamically fill an array from Mysql using Form Helper for dropdown list.
#1

[eluser]datguru[/eluser]
Hello guys first of all thanks for taking the time to read.

Right on to the problem......

I am trying to create a drop-down from a mysql query and fill the array in the controller by using a for each loop, then load the view and just fill the drop-down using a variable so to keep all the logic in the controller.

Heres what I have so far...

The Controller:

Code:
function new_raw_materials()
    {
        $this->load->model(');
        
        $suppliers = $this->mdl_supplier->get();
        //Generate Supplier Name List
        foreach($suppliers as $row)
        {
        $data['options'] = array( $row->supplier_id => $row->supplier_name);
        }
        //Dynamicaly Generate the Base URL so we know the location of the css file.
        $baseURL = $this->config->item('base_url');

        $data["baseURL"] = $baseURL;
        
        $this->load->view("admin/new_raw_materials",$data);
    }

The View:
Code:
<tr>
<td>Raw Materials Supplier Name:</td> <td>&lt;?=form_dropdown('supplier_id',$options, '1'); ?&gt;</td>
</tr>

I am pretty sure the problem lies with the way I am filling the array but I am not sure on how to create dynamic expanding array without putting the logic inside the array which I don't think you can do.


Thanks for your time

Regards
#2

[eluser]Mike Ryan[/eluser]
Hi Datguru,

Are you only seeing one supplier in the dropdown menu? If so, it is because of this line:

Code:
$data['options'] = array( $row->supplier_id => $row->supplier_name);

$data['options'] is being overwritten each time the loop executes, so only the last pair will show in the dropdown. Try this:


Code:
foreach($suppliers as $row)
        {
        $tmp[] = array( $row->supplier_id => $row->supplier_name);
        }
    $data['options'] = $tmp;

I only tested this with print_r, but it should work with the dropdown. Also, if mld_supplier_get returns an array consisting only of ID and Name, you could just do:

Code:
$suppliers = $this->mdl_supplier->get();
    $data['options'] = $suppliers;

Regards,

Mike
#3

[eluser]datguru[/eluser]
Hey thanks for the reply Mike, that has indeed solved the problem of populating the array. The only issue now is that it is giving me an array of arrays, so when I go to fill the form_dropdown with the options it gives me the options of Array, Array, Array.... Is there a better way to dynamically fill the form_dropdown()?

Thanks again

Edit: Found a solution....

What I did was the following...

Code:
$tmp2 = array() //Needs to be an array type otherwise the first merge will not work
        foreach($suppliers as $row)
        {
        $tmp = array( $row->supplier_id => $row->supplier_name);
        $tmp2 = array_merge($tmp,$tmp);
        }
$data['options'] = $tmp2;

Thanks again Mike without your help would not of got there.

Cheers

Chris
#4

[eluser]Erwin Setiawan[/eluser]
Hey why your code not working for me?
i had the same problem to..
and try to get solution, but while I try your solution, I can't get all dropdown value displayed.

I try to get another solution and finally i found it.

this is my dynamically dropdown script for my application.


Code:
$cat_id="";
$category="";
$query=$this->dn_categories_model->get_all();
foreach($query as $row):
        $cat_id .= "$row->cat_id";
    $cat_id .= ",";
    $category .= "$row->category";
    $category .= ",";
endforeach;
$comma=",";//separate by commas
$arr_cat_id=split($comma,$cat_id);
$arr_category=split($comma,$category);
$options=array_combine($arr_cat_id,$arr_category);
            
$data['input_category'] = $options;


And work clearly^^
#5

[eluser]DCZ[/eluser]
[quote author="Erwin Setiawan" date="1227540155"]Hey why your code not working for me?
i had the same problem to..
and try to get solution, but while I try your solution, I can't get all dropdown value displayed.
[/quote]

That is because this line

Code:
$tmp2 = array_merge($tmp,$tmp);

Should be

Code:
$tmp2 = array_merge($tmp,$tmp2);

And this
Code:
foreach($suppliers as $row)

Should maybe be this
Code:
foreach($suppliers->result() as $row)

Greetz
#6

[eluser]Tamer Solieman[/eluser]
Thanks DCZ it works fine for me :>
#7

[eluser]DCZ[/eluser]
I might have an alternative for this function
(Remark the flexibility of the query could be better)

In your model or helper

Code:
function get_dropdown_array($key, $value, $from){
        $result = array();
        $array_keys_values = $this->db->query('select '.$key.', '.$value.' from '.$from.' order by id asc');
       foreach ($array_keys_values->result() as $row)
        {
            $result[$row->$key]= $row->$value;
        }
        return $result;
    }

In your controler :
Let's request some countries....

Code:
$this->data['content_countries'] = $this->my_model->get_dropdown_array('id', 'country','countries');


In your view :

Code:
&lt;?=form_dropdown('country_id', $content_countries,$my_country_id);?&gt;

Remark "$my_country_id" is the "selected" value on load.

We can update that last line some more.

Code:
&lt;?=form_dropdown('country_id', $content_countries,set_value('country_id', $my_country_id))?&gt;

So now it will select $my_country_id on first load and then it will remember it value on a failed submit.

Greetz.
#8

[eluser]Tamer Solieman[/eluser]
Thanks DCZ This was very helpful for me
#9

[eluser]pasquattro[/eluser]
Hi, im very new to this and would like to use DCZ's code. Could someone explain the code for the model so I can try and implement it.

Code:
function get_dropdown_array($key, $value, $from){
        $result = array();
        $array_keys_values = $this->db->query('select '.$key.', '.$value.' from '.$from.' order by id asc');
       foreach ($array_keys_values->result() as $row)
        {
            $result[$row->$key]= $row->$value;
        }
        return $result;
    }

thanks.
#10

[eluser]DCZ[/eluser]
Hi pasquattro,

My main goal was to fill a dropdown object with the result of a mysql query. CI has a nice library that you can use to query the database, but the problem is that the array that is return is not in the correct shape to send it to a dropdown object.

Therefor I wrote a function to turn the array around.

First I create an empty array

Code:
$result = array();

Then I launch a sql statement using the CI db library and store it into a variable (type : array)

Code:
$array_keys_values = $this->db->query('select '.$key.', '.$value.' from '.$from.' order by id asc');

Next I loop thru all the values inside my $array_keys_values array and each result is then put in the variable $row.

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

Now I take my new and empty array called $results. And I will put the right values in the right place. Making my new array $results the perfect object to give to the dropdown function in the view (form_dropdown)

Code:
$result[$row->$key]= $row->$value;


For a better understanding what is going on, print $result and $array_keys_values and you will see the difference.




Theme © iAndrew 2016 - Forum software by © MyBB