17 Nov 2009 @ 1:37 PM 

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

Posted By: Zayin
Last Edit: 17 Nov 2009 @ 01:40 PM

EmailPermalink
Tags
Tags: ,
Categories: php


 

Responses to this post » (None)

 
Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.


 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 38
  • Comments » 56
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Links



    No Child Pages.

Portfolio



    No Child Pages.