Welcome Guest, Not a member yet? Register   Sign In
Dynamic dependant dropdown from database
#1

Hi All,
I desperately need a dynamic dependant dropdown from database.
This code works brilliantly - http://www.99points.info/2010/12/n-level...x-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
Reply
#2

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.
Reply
#3

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
Reply
#4

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.
Reply
#5

(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!
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#6

(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
Reply
#7

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

(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
Reply
#9

(This post was last modified: 12-27-2015, 02:21 PM by RobertSF.)

(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/20483...enu-values
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#10

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-...ax-search/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB