I’ve been using the CodeIgniter framework for a few months now, and I am still blow away by its flexibility every day.
CodeIgniter is a MVC (Model/View/Controller) framework that makes your life a breeze when developing PHP applications.
I want to add a form to one of my webpages that allows me to apply a pre-defined pricelist to a customer’s products.
The snippet below generates the form and populates the “select” tag with elements from the database
First, lets load all the pricelists into $query
1 | $query = $this->db->get('pricelist'); |
Then lets put the relevant fields into an array
1 2 3 4 | foreach ($query->result() as $row) { $data[$row->pricelist_id] = $row->pricelist_description; } |
Lastly, lets ask CI to build us a form
1 2 3 4 | $result = form_open(get_class($this) . '/assign_pricelist_to_customer/' . $this->customer_id . '/' . $this->sub_nav_id) . "\n"; $result = $result . form_dropdown('pricelist_id', $data) . "\n"; $result = $result . form_submit('submit', 'Load this pricelist') . "\n"; $result = $result . form_close() . "\n"; |
Here’s the full method
1 2 3 4 5 6 7 8 9 10 11 12 | private function setup_pricelist(){ $query = $this->db->get('pricelist'); foreach ($query->result() as $row) { $data[$row->pricelist_id] = $row->pricelist_description; } $result = form_open(get_class($this) . '/assign_pricelist_to_customer/' . $this->customer_id . '/' . $this->sub_nav_id) . "\n"; $result = $result . form_dropdown('pricelist_id', $data) . "\n"; $result = $result . form_submit('submit', 'Load this pricelist') . "\n"; $result = $result . form_close() . "\n"; return $result; } |
So elswhere in my code, all I do is
1 2 3 4 5 6 7 8 9 | . . . $data['pricelist_select'] = $this->setup_pricelist(); $data['title'] = $this->title; $this->load->view('some_view', $data); . . . |
in my view
1 2 3 4 5 | <div class="content">
<?php echo $pricelist_select?>
<?php echo $content?>
<div class="line"></div>
</div> |
And hey presto, I have a form with a dynamically populated dropdown

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 