[eluser]Unknown[/eluser]
Hey folks,
I'm very new to code igniter, and I'm just working my way through a simple dynamic drop down list, but I can't figure out exactly how to get er to work. I've been all over the forums and I can't seem to find anything that will make this happen.
So I have my controller all set up, connecting to my database and loading my model, but I'm a litle confused on exactly how to pass the data between model and controller and view, also how to generate the list, do I put the for loop in the view, or in the model then pass the data to the controller, etc.
Here's what I have so far, be brutally honest if possible, I apologize if it doesn't make much sense, I've been getting desperate and trying anything really so it's a bit of a mess.
Controller
Code:
<?php
class Country_search extends Controller {
function Country_search()
{
parent::Controller();
}
function index()
{
$config['hostname'] = "localhost";
$config['username'] = "root";
$config['password'] = "";
$config['database'] = "world";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$this->load->database($config);
$this->load->model('User_model');
$data = $this->User_model->get_continents();
$dropdown = array();
//foreach ($data)
//{
// $dropdown[$data->id] = $data->title;
// }
$this->load->view('index', array('dropdown' => $dropdown));
}
}
Model
Code:
<?php
class User_model extends Model {
function User_model()
{
parent::Model();
}
function get_continents()
{
$query = $this->db->query('SELECT DISTINCT continent FROM country');
return $query;
}
}
?>
View
Code:
<?php echo doctype('xhtml1-trans') ?>
<html>
<head>
<?php
$link = array(
'href' => 'CodeIgniter_1.7.2/stuff/assign-2/assets/style.css',
'rel' => 'stylesheet',
'type' => 'text/css'
);
echo link_tag($link);
?>
<title>Country Search</title>
</head>
<body>
<div id="wrapper">
<div id="content">
<?php echo heading('Search by Continent', 1); ?>
<?php echo form_open('country_search/country_list'); ?>
<?php
echo form_dropdown('continent', $dropdown);
?>
<?php echo form_submit('submit', 'Show me the countries!'); ?>
<?php echo form_close(); ?>
</div>
<div id="navigation">
<?php echo ul(anchor('CodeIgniter_1.7.2','Search by Continent')); ?>
</div>
<div id="header">
<p class="site-name">ISSD 21 CodeIgniter Country Search</p>
<p class="site-slogan">Ryan Bend</p>
</div>
<div id="footer">
<p>Footer</p>
</div>
</div>
</body>
</html>