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
class customer_list extends MY_Controller { function some_method($customer_id, $subnav_item) { $this->sub_nav_id = $subnav_item; $this->customer_id = $customer_id; $uri_string = get_class($this) . '/some_method/' . $this->customer_id . '/' . $this->sub_nav_id; } } |
when calling the class I would do something like this : http://www.mysite.com/customer_list/some_method/1/2
This would call the method “some_method” and pass 2 parameters ($customer_id = 1 and $subnav_item = 2)
Its that simple
If you are looking for an easy to use CRUD (Create, Read, Update, Delete) framework for PHP, then I can *highly* recommend Rapyd.
Although Rapyd is a MVC (Model/View/Controller) framework http://www.rapyd.com/, I am using the version of Rapyd that integrates with CodeIgniter
The only shortcoming is that the documentation is rather lacking, so I had to figure most of it out myself.
While there are some excellent examples on the Rapyd website, I have decided to add some examples of my own.
<?php class hardware_list extends MY_Controller { function hardware_list() { parent::MY_Controller(); $this->load->library('rapyd'); $this->title = 'Manage hardware'; } //put your code here function edit_data() { $hardware_edit = new dataedit_library(); $hardware_edit->label = $this->title; $hardware_edit->back_url = site_url('hardware_list'); $hardware_edit->source('hardware'); $hardware_edit->field('input','hw_serial','Serial Number'); $hardware_edit->field('input','hw_desc','Description'); $hardware_edit->field('radiogroup','hw_status','Status')->options(array("1"=>"Enabled", "2"=>"Disabled")); $hardware_edit->field('radiogroup','hw_type','type')->options(array("1"=>"Wintec", "2"=>"Stand Alone PC")); $hardware_edit->buttons('modify','save','undo','back','delete'); $hardware_edit->build(); $data['head'] = $this->rapyd->head(); $data['title'] = $this->title; $data['content'] = $hardware_edit->output.'<br />'; $data['current_selected_nav_item'] = 6; $this->load->view('terminals/terminals', $data); } function index() { if ($this->rapyd->qs->value('show|delete|modify|create|insert|update')) { $this->edit_data(); } else { $hardware_grid = new datagrid_library(); $hardware_grid->label = $this->title; $hardware_grid->source('hardware'); $hardware_grid->column('hw_serial','serial', true) ->url(site_url('hardware_list/?show={hw_serial}')) ->rule('required'); $hardware_grid->column('hw_status','Status'); $hardware_grid->column('hw_type','Type'); $hardware_grid->column('hw_desc','Description'); $hardware_grid->column('delete', 'delete')->url(site_url('hardware_list/?delete={hw_serial}')); $hardware_grid->buttons('add'); $hardware_grid->build(); $data['head'] = $this->rapyd->head(); $data['title'] = $this->title; $data['content'] = $hardware_grid->output.'<br />'; $data['current_selected_nav_item'] = 6; $this->load->view('terminals/terminals', $data); } } } ?> |
I want to make a note about the “events” and the order that they happen in, as it took me quite a while to figure it out.
create = before insert – show the form with empty fields
insert = after insert – this happens after create and physically inserts the fields into the db
delete = before delete – show confirmation screen
do_delete = after delete is confirmed – this physically deletes the row from the db
show = show all the info for a single row, but edit it just yet
modify = before data is modified – it shows the edit form with all the fields populated
update = save changes entered in the modify screen
Your “source” can also be an active record result set eg
$grid->label = $this->title; $grid->db->select('user.*,customer.company_name'); $grid->db->from('user'); $grid->db->join('customer', 'user.customer_id = customer.customer_id'); $grid->db->where('user.customer_id = ' . $customer_id); |

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