[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>
<?php echo $site_title ?>
</h1>
<?php
echo form_dropdown('customer', $customers);
?>
</body>
</html>