• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create multidimensional array from two database tables for form_dropdown()

#5
[eluser]suntrop[/eluser]
I think I get it. With a Little Help from My Friends :-) Thanks cpass78
Code:
$this->db->select( 'p.id AS project_id, p.title AS project_title, c.name AS customer_name' );
    $this->db->from( 'projects AS p' );
    $this->db->join( 'customers AS c', 'p.customer_id = c.id', 'left' );
    $this->db->order_by( "c.name" );
    $query = $this->db->get();

    if ( $query->num_rows() > 0 )
    {
       $projects = $query->result_array();
    }
    
    foreach ($projects as $row) {
        $p[$row['customer_name']][$row['project_id']] = $row['project_title'];
    }

    
    echo form_dropdown('projects_array', $p);
The $p array looks a bit ugly, but it works.

It produces …
Code:
<select name="projects">
<optgroup label="Customer A">
<option value="2">Project Name</option>
</optgroup>
<optgroup label="Customer B">
<option value="1">Project Name 2</option>
<option value="4">Project Name 3</option>
</optgroup>
<optgroup label="Customer C">
<option value="3">Project Name 4</option>
</optgroup>
</select>


Messages In This Thread
Create multidimensional array from two database tables for form_dropdown() - by El Forum - 07-24-2011, 12:03 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.