Welcome Guest, Not a member yet? Register   Sign In
from result_array() to form_dropdown()
#11

[eluser]helmutbjorg[/eluser]
geez man... just build it. This forum isn't your personal development team.
#12

[eluser]B A B U[/eluser]
[quote author="helmutbjorg" date="1249722277"]geez man... just build it. This forum isn't your personal development team.[/quote]
Cool buddy. I already posted my code on top. I am asking whether my way of coding is correct or any improvement required.
Here is full code. Please let me know if any improvements r required.
Code:
<?php
    class Global_model extends Model
    {
        /*
        // Uncomment this if you want to excute some code after creation of object
        // NOTE: Place code in PHP 5 __construct, not in PHP 4 construct
        function __construct()
        {
            parent::Model();
        }
        
        // For PHP 4
        function Global_model()
        {
            $this->__construct();
        }
        */
        
        private $blank_option = array( NULL => '--' ); // Blank option to add for select


/*-----------------------------------------------------------------------------
Getting all company names & IDs from companies table into dropdown list
------------------------------------------------------------------------------*/

        function company_list()
        {
            $this->db->select('company_id,company_name');
            $result_obj = $this->db->get('companies');
            $result = $result_obj->result_array();
            
            // Converting MDA to SDA options
            $company_list = $this->result_to_select($result);
            
            // Adding blank option
            $company_list = $this->add_blank_option($company_list);
            
            // Generating dropdown
            $this->load->helper('form');    
            $attributes = 'id="contact_company"';
            $company_list = form_dropdown('contact_company', $company_list, '', $attributes);
            
            return $company_list;
        }

/*-----------------------------------------------------------------------------
Add blank option to the input options array
@ $options - options array reference
@ $blank_option - blank option display text or array
NOTE: You can change the blank option value which is located on top of this page
------------------------------------------------------------------------------*/        

        function add_blank_option($options, $blank_option = '')
        {
            if (is_array($options) && is_string($blank_option))
            {
                if (empty($blank_option))
                {
                    $blank_option = $this->blank_option;
                }
                else
                {
                    $blank_option = array( NULL => $blank_option );
                }
                $options = $blank_option + $options;
                
                return $options;
            }
            
            else
            {
                show_error("Wrong options array passed");
            }
        }
        
/*-----------------------------------------------------------------------------
Converting the two columns query result into single dimentional array
@ $options - options array that having 2 columns & first column should be option value
------------------------------------------------------------------------------*/        

        function result_to_select($result)
        {
            if (is_array($result))
            {
                $options = $keys = array();
                
                foreach ($result AS $row)
                {
                    if (count($row)!==2)
                    {
                        show_error("Array having more than 2 or less columns");
                    }
            
                    // Getting column names into array
                    foreach ($row AS $key => $value)
                    {
                        $keys[] = $key;
                    }
                    
                    // Creating select options array using columns array
                    for($i=0 ; $i < count($keys) ; $i++)
                    {
                        $options[$row[$keys[0]]] = $row[$keys[1]];
                    }
                }
                return $options;
            }
            show_error("Passed wrong array options parameter");
        }        
        
    }

/* End of file global_modle.php */
/* Location: ./system/application/controllers/global_modle.php */
#13

[eluser]helmutbjorg[/eluser]
Okay... I guess your code looks awfully complicated for a relatively simple process.

You don't really need to extend the model to do all this. Put Phils function into a general helper perhaps with a boolean option of whether to include a blank option at the start.

Code:
function array_to_select($results, $value = 'id', $key = 'title', $add_blank=false)
{
    // Converts objects to arrays
    if(is_object($results)) $results = get_object_vars($results);

    $options = array();

    if(!empty($add_blank)) $options = array(null=>$add_blank);

    // Will only run if results is an array, not a string, int, etc.
    if(is_array($results))
    {
        foreach($results as $result)
        {
            // Get the two rows specified
            $options[$result[$value]] = $result[$key];
        }
    }

    return $options;
}

Usage
Code:
$options = array_to_select($result, 'company_id', 'company_name', '--');
$company_list = form_dropdown('contact_company', $options, '', $attributes);

This way you can re-use the code in the future. This is not tested it is just to give you the idea.
#14

[eluser]B A B U[/eluser]
Thanks for the info. But result_to_options and add_blank_options are in helper only. To show here i grouped all the things.

I think it's better to write a function to add blank option, thus i can reuse.

What if i dont know column names?

My helper

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*============================================================================
All the common function should be defined here
============================================================================*/

/*-----------------------------------------------------------------------------
Add blank option to the input options array
@ $options - options array reference
@ $blank_option - blank option display text or array
NOTE: You can change the blank option value which is located on top of this page
------------------------------------------------------------------------------*/        

    if ( ! function_exists('add_blank_option'))
    {
        function add_blank_option($options, $blank_option = '')
        {
            if (is_array($options) && is_string($blank_option))
            {
                if (empty($blank_option))
                {
                    $blank_option = array( NULL => '--' );
                }
                else
                {
                    $blank_option = array( NULL => $blank_option );
                }
                $options = $blank_option + $options;
                
                return $options;
            }
            
            else
            {
                show_error("Wrong options array passed");
            }
        }
    }
        
/*-----------------------------------------------------------------------------
Converting the two columns query result into single dimentional array
@ $options - options array that having 2 columns & first column should be option value
NOTE: The result should be array with 2 columns
------------------------------------------------------------------------------*/        

    if ( ! function_exists('result_to_select'))
    {        
        function result_to_select($result)
        {
            if (is_array($result))
            {
                $options = $keys = array();
                
                foreach ($result AS $row)
                {
                    if (count($row)!==2)
                    {
                        show_error("Array having more than 2 or less columns");
                    }
            
                    // Getting column names into array
                    foreach ($row AS $key => $value)
                    {
                        $keys[] = $key;
                    }
                    
                    // Creating select options array using columns array
                    for($i=0 ; $i < count($keys) ; $i++)
                    {
                        $options[$row[$keys[0]]] = $row[$keys[1]];
                    }
                }
                return $options;
            }
            show_error("Passed wrong array options parameter");
        }        
    }

/* End of file global_helper.php */
/* Location: ./system/application/helpers/global_helper.php */

My Model
Code:
&lt;?php
    class Global_model extends Model
    {
        // Uncomment this if you want to excute some code after creation of object
        // NOTE: Place code in PHP 5 __construct, not in PHP 4 construct
        function __construct()
        {
            parent::Model();
            $this->load->helper('global_helper');
            $this->load->helper('form');
        }
        
        // For PHP 4
        function Global_model()
        {
            $this->__construct();
        }

/*-----------------------------------------------------------------------------
Getting all company names & IDs from companies table into dropdown list
------------------------------------------------------------------------------*/

        function company_list()
        {
            $this->db->select('company_id,company_name');
            $result_obj = $this->db->get('companies');
            $result = $result_obj->result_array();
            
            // Converting MDA to SDA options
            $company_list = result_to_select($result);
            
            // Adding blank option
            $company_list = add_blank_option($company_list);
            
            // Generating dropdown
            $attributes = 'id="contact_company"';
            $company_list = form_dropdown('contact_company', $company_list, '', $attributes);
            
            return $company_list;
        }
        
    } // Global_model class END


/* End of file global_modle.php */
/* Location: ./system/application/controllers/global_modle.php */




Theme © iAndrew 2016 - Forum software by © MyBB