Part of the EllisLab Network
   
 
Dynamically fill an array from Mysql using Form Helper for dropdown list.
Posted: 19 October 2008 08:42 AM   [ Ignore ]  
Summer Student
Total Posts:  23
Joined  04-27-2008

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:

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:

<tr>
<
td>Raw Materials Supplier Name:</td> <td><?=form_dropdown('supplier_id',$options'1'); ?></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

Profile
 
 
Posted: 19 October 2008 09:36 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  115
Joined  10-19-2008

Hi Datguru,

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

$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:

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:

$suppliers $this->mdl_supplier->get();
    
$data['options'$suppliers

Regards,

Mike

Profile
 
 
Posted: 19 October 2008 10:06 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  23
Joined  04-27-2008

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…

$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

Profile
 
 
Posted: 24 November 2008 05:22 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  10
Joined  10-25-2008

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.

$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^^

 Signature 

Visit me

http://einchi.com

Profile
 
 
Posted: 05 May 2009 10:15 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  8
Joined  04-28-2009
Erwin Setiawan - 24 November 2008 10:22 AM

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.

That is because this line

$tmp2 array_merge($tmp,$tmp); 

Should be

$tmp2 array_merge($tmp,$tmp2); 

And this

foreach($suppliers as $row

Should maybe be this

foreach($suppliers->result() as $row

Greetz

Profile
 
 
Posted: 26 May 2009 03:59 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Avatar
Total Posts:  8
Joined  10-29-2008

Thanks DCZ it works fine for me :>

 Signature 

—————————-
Major

Profile
 
 
Posted: 27 May 2009 04:24 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  8
Joined  04-28-2009

I might have an alternative for this function
(Remark the flexibility of the query could be better)

In your model or helper

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….

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


In your view :

<?=form_dropdown('country_id'$content_countries,$my_country_id);?> 

Remark “$my_country_id” is the “selected” value on load.

We can update that last line some more.

<?=form_dropdown('country_id'$content_countries,set_value('country_id'$my_country_id))?> 

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

Greetz.

Profile
 
 
Posted: 01 June 2009 06:38 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Avatar
Total Posts:  8
Joined  10-29-2008

Thanks DCZ This was very helpful for me

 Signature 

—————————-
Major

Profile
 
 
Posted: 31 August 2009 02:52 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  2
Joined  08-31-2009

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.

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.

Profile
 
 
Posted: 01 September 2009 03:25 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  8
Joined  04-28-2009

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

$result = array(); 

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

$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.

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)

$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.

Profile
 
 
Posted: 01 September 2009 02:49 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  2
Joined  08-31-2009

Thanks a lot DCZ, for both the code and explanation, I really appreciate it.

Profile
 
 
Posted: 05 March 2010 12:02 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  7
Joined  02-23-2009

Great!
Solved my problem on populating drop down form

Profile
 
 
Posted: 02 March 2011 04:30 PM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  2
Joined  02-28-2011

hi dcz,
i know this post quite old but i have some problem.i am new to CI infact 2 days old.

i want to populate data in dropdown. i have used your later function .
thisi s what i did so far
in model..

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

in controller

function dropdown()
{     
$this
->load->model('Dropdown_model');
  
$this->data['drop'$this->Dropdown_model->get_dropdown_array('uid''name','users');
    

in view

<?=form_dropdown('uid'$drop,'uid');?> 

but i am getting error like

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: drop

Filename: views/signup.php

Line Number: 34
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 310

Profile
 
 
Posted: 19 April 2011 06:03 AM   [ Ignore ]   [ # 13 ]  
Summer Student
Total Posts:  1
Joined  03-28-2011
Tamer Solieman - 26 May 2009 07:59 PM

Thanks DCZ it works fine for me :>

I’m beginner and newbie in Code Igniter, i have tried this…

i was added :

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;
    

on my model

then i load this result using

<?php  $options1=$this->Guest_model->get_dropdown_array('id''driver_name','guest_driver');
        
        
?>

<?
=form_dropdown('driver'$options1set_value('driver',isset($default['driver']) ? $default['driver'''));?> 

and it’s work!..great thanks

Profile