CodeIgniter Forums
Form field data pulled from database - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Form field data pulled from database (/thread-16796.html)

Pages: 1 2 3

Form field data pulled from database - El Forum - 03-18-2009

Glad you got it sorted. Smile

Form field data pulled from database - El Forum - 03-18-2009

[quote author="jedd" date="1237443039"]Actually, on that note, do you mind if I ... ?

Can you please post your database schema? I'll rattle up some comparison code, using a different approach.[/quote]

Please do... Here's the customers table (I'm guessing that's the only one you wanted?):

| Field        | Type         | Null | Key | Default | Extra          |
| customerid   | int(8)       | NO   | PRI | NULL    | auto_increment |
| customerName | varchar(150) | NO   |     |         |                |

Form field data pulled from database - El Forum - 03-18-2009

Okay. Here's my CREATE.mysql script for (re-)creating the DB:
DROP DATABASE customers;
USE customers;

CREATE TABLE customers  (
    id        SERIAL,
    name    char(150) UNIQUE,
    INDEX (name),
    PRIMARY KEY (id)

INSERT INTO customers (name)
    VALUES ("Customer the first");
INSERT INTO customers (name)
    VALUES ("Customer the second");
INSERT INTO customers (name)
    VALUES ("Customer the third");

INSERT INTO customers (name)
    VALUES ("Customer the fourth");

Note I changed 'customerid' to 'id' - as context makes it clear, and it's cumbersome to have the tablename as part of column names. That's what the table.column syntax is for.

The next three files are complete, and tested.

This is just another way of doing things, and almost certainly not the best way. I am not happy with the conversion of a flat array out of the database, into an associative array as required by form_dropdown() - I am confident there's a better way of doing this data massage.


class Customer extends Controller {

    function Customer()  {
        $this->load->model ('Customer_mdl');
        } // end-function View()

    function add()  {
        $data['site_title'] = $this->config->item('site_title');

        // Grab customers- will be in flat array, by record.  If there's no results, don't proceed.
        if ($raw_customers = $this->Customer_mdl->get_all_customers())  {
            // Convert flat ([0] id=X, name=xyz) array to associative ( $id => $name )
            foreach ($raw_customers as $key=>$value)
                $data['customers'][$value['id']] = $value['name'];

            // Proceed to view
            $this->load->view ('default/admin/projects_add', $data);

        } // end-function add()

    } // end-class View()

Here's my MODEL:

class Customer_mdl extends Model {

    function Customer_mdl ()  {

    function get_all_customers ()  {
        $query = $this->db->query ('SELECT *
                                    FROM customers');

        return ($query->num_rows) ? $query->result_array() : FALSE;
        }  // end-function get_all_customers()

    }  // end-class Customer_mdl

And here's my VIEW:
        <?php echo $site_title ?>

    &lt;?php echo $site_title ?&gt;

    echo form_dropdown('customer', $customers);


Form field data pulled from database - El Forum - 03-18-2009

Wow, thanks jedd. The code in yours looks a bit cleaner than mine and there's less of it. If I get a chance with this I'll revisit it to improve the foundation of what's there now, especially the clean up of the tables and the column names.