Welcome Guest, Not a member yet? Register   Sign In
Dynamic drop down list help
#1

[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">
      &lt;?php echo heading('Search by Continent', 1); ?&gt;
      
      &lt;?php echo form_open('country_search/country_list'); ?&gt;
      
            &lt;?php
                
            echo form_dropdown('continent', $dropdown);
                
            ?&gt;
      
      &lt;?php echo form_submit('submit', 'Show me the countries!'); ?&gt;  
      
      &lt;?php echo form_close(); ?&gt;
    </div>
    
    <div id="navigation">
      
      &lt;?php echo ul(anchor('CodeIgniter_1.7.2','Search by Continent')); ?&gt;
      
    </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>
&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]Nick_MyShuitings[/eluser]
Quote:$data = $this->User_model->get_continents();

I think that you need to call either ->result() if you like objects or ->result_array() if you prefer arrays. Then run a echo
Code:
echo "<pre>"; die(var_dump($data));
so that you can see that you actually have something now that you can run a foreach on.

if you run a var_dump of $data now you prolly get something along the lines of mysql result object gibberish

so.... try this:
Code:
$data = $this->User_model->get_continents()->result();
echo "<pre>";
die(var_dump($data));




Theme © iAndrew 2016 - Forum software by © MyBB