Welcome Guest, Not a member yet? Register   Sign In
Form field data pulled from database
#21

[eluser]TheFuzzy0ne[/eluser]
Glad you got it sorted. Smile
#22

[eluser]mdcode[/eluser]
[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?):

Code:
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| customerid   | int(8)       | NO   | PRI | NULL    | auto_increment |
| customerName | varchar(150) | NO   |     |         |                |
+--------------+--------------+------+-----+---------+----------------+
#23

[eluser]jedd[/eluser]
Okay. Here's my CREATE.mysql script for (re-)creating the DB:
Code:
DROP DATABASE customers;
CREATE 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.


Here's my CONTROLLER:
Code:
<?php

class Customer extends Controller {

    function Customer()  {
        parent::Controller();
        $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:
Code:
<?php

class Customer_mdl extends Model {

    function Customer_mdl ()  {
        parent::Model();
        }

    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:
Code:
<html>
<head>
    <title>
        <?php echo $site_title ?>
    </title>
</head>


<body>
<h1>
    &lt;?php echo $site_title ?&gt;
</h1>

&lt;?php
    echo form_dropdown('customer', $customers);
?&gt;

&lt;/body&gt;
&lt;/html&gt;
#24

[eluser]mdcode[/eluser]
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.

::Bookmarked::




Theme © iAndrew 2016 - Forum software by © MyBB