CodeIgniter Forums
Dynamic dependant dropdown from database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Dynamic dependant dropdown from database (/showthread.php?tid=63947)



Dynamic dependant dropdown from database - GingerNut - 12-26-2015

Hi All,
I desperately need a dynamic dependant dropdown from database.
This code works brilliantly - http://www.99points.info/2010/12/n-level-dynamic-loading-of-dropdowns-using-ajax-and-php/
But I can't get it to work in Codeigniter.
Can anyone convert it for me please?
Yours Roy
Bee Creative Web Design
www.bcwd.ltd.uk


RE: Dynamic dependant dropdown from database - skunkbad - 12-26-2015

If you share some code, I'm sure you'll get some feedback, but it's hard to help when we don't know what mistakes you are making.


RE: Dynamic dependant dropdown from database - GingerNut - 12-26-2015

It's not so much my mistakes, it's converting the PHP to Codeigniter, I think a lot of the trouble is with the Ajax?
Yours Roy
Bee Creative Web Design
www.bcwd.ltd.uk


RE: Dynamic dependant dropdown from database - skunkbad - 12-26-2015

I recommend reading the CodeIgniter documentation, especially the parts about controllers, database connections, and database queries. Even if you skim those sections, you'll have a better understanding of what to do. You should be aware that the code you've been looking at is very old, and legacy code like that is not optimal, unless you are just learning.


RE: Dynamic dependant dropdown from database - RobertSF - 12-26-2015

(12-26-2015, 08:58 AM)GingerNut Wrote: I desperately need a dynamic dependant dropdown from database.

Hey, there. I just implemented that. Check out my Search Scraper application on GitHub. The basic idea is this. In your controller, load from the database the values that will populate the dropdown, and then pass those values to your view, which will then use them when building the dropdown. In my controller's constructor, I do this:
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
Searches extends CI_Controller {

protected 
$sites = array();
protected 
$searches = array();

    public function 
__construct()
    {
        
parent::__construct();
        
$this->load->helper('url');
        
$this->load->helper('download');
        
$this->load->library('form_validation');
        
$this->load->library('table');
        
$this->load->model('searches_model');
        
$this->sites $this->searches_model->get_sites(); // loading dropdown values from database
        
$this->load->driver('site'array_column($this->sites'class'));
        
$this->searches $this->searches_model->get_searches();
    } 


The call to get_sites() in my model executes this code:
PHP Code:
public function get_sites()
{
 
   return $this->db->query(
 
       'SELECT
            sites.id AS id,
            sites.name AS name
         FROM sites;'
 
   )->result_array();




Now that I have the array $sites holding the sites I have in the database, I pass it to the view:
PHP Code:
$data = array(
    
'subview' => 'search_add_view',
    
'sites' => $this->sites,
    
'searches' => $this->searches
);
$this->load->view('searches_view'$data); 

And then my view goes and does this:

PHP Code:
<select id="sites" name="search[site_id]">
 
   <option value="" selected>Select one</option>
 
   <?php foreach ($sites as $row):?>
        <option value="<?php echo $row['id']?>"><?php echo $row['name']?></option>
    <?php endforeach;?>
</select> 

I hope that's a good example, but if it's not clear, please let me know where it derails. Good Luck!


RE: Dynamic dependant dropdown from database - GingerNut - 12-27-2015

(12-26-2015, 12:03 PM)skunkbad Wrote: I recommend reading the CodeIgniter documentation, especially the parts about controllers, database connections, and database queries. Even if you skim those sections, you'll have a better understanding of what to do. You should be aware that the code you've been looking at is very old, and legacy code like that is not optimal, unless you are just learning.

I'm fine with the CI stuff and I can get the first dropdown working, but something needs doing to the Ajax code (which I'm not good at) to make the second etc dropdowns to work.

That's why I'm asking for help?

Yours Roy
Bee Creative Web Design
www.bcwd.ltd.uk


RE: Dynamic dependant dropdown from database - InsiteFX - 12-27-2015

In jQuery ever element in html needs to have an ID also I like to give each element a Name

For one that jQuery code is using an out dated jQuery build.


RE: Dynamic dependant dropdown from database - GingerNut - 12-27-2015

(12-27-2015, 06:09 AM)InsiteFX Wrote: In jQuery ever element in html needs to have an ID also I like to give each element a Name

For one that jQuery code is using an out dated jQuery build.

I understand this is old code, and I have tested it with - <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> - and it works perfectly Smile

BUT, I don't understand enough about JQuery to get it to work in CI, so I need some help please?

I can't get the second dropdown to work, and I don't know why?

Yours Roy
Bee Creative Web Design
www.bcwd.ltd.uk


RE: Dynamic dependant dropdown from database - RobertSF - 12-27-2015

(12-27-2015, 07:49 AM)GingerNut Wrote: BUT, I don't understand enough about JQuery to get it to work in CI, so I need some help please?

I know only a little Javascript, but I don't think there's anything special to get it working "in CI" because PHP doesn't interact with Javascript. By the time Javascript is ready to run, PHP has already finished. Can you get any Javascript to work in a page generated by CI? Conversely, does the Javascript code you have work ok in a regular HTML page you create by hand with just a few dropdowns?

Edit: I just found this. It may help. http://stackoverflow.com/questions/20483470/javascript-dynamic-drop-down-menu-values


RE: Dynamic dependant dropdown from database - John_Betong - 12-27-2015

I have recently installed an Ajax Search routine and noted in the tutorial the CI problem and solution.

http://www.johns-jokes.com/downloads/sp-e/jb-ajax-search/