CodeIgniter-来自数据库的动态依赖下拉列表

罗伊·戈林(Roy Goring)

有人可以帮忙吗?

我正在尝试将此代码转换为CI,但是我对JQuery遇到问题,因为我不太了解它。

原始代码来自-http ://www.99points.info/2010/12/n-level-dynamic-loading-of-dropdowns-using-ajax-and-php/-可在CI外部完美运行。

我可以看到第一个下拉列表。

然后,当它在“ index2.php”的第15行上加载“ get_chid_categories.php”时,我得到一个错误-我理解为什么(尝试从“根”中的文件读取数据库),但是我不知道如何解决它。

我的看法-index2.php

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

    <script type="text/javascript" src="/jquery.livequery.js"></script>
    <script type="text/javascript">

      $(document).ready(function() {

        //$('#loader').hide();

         $('.parent').livequery('change', function() {

          $(this).nextAll('.parent').remove();
          $(this).nextAll('label').remove();

          $('#show_sub_categories').append('</application/views/img src="loader.gif" style="float:left; margin-top:7px;" id="loader" alt="" />');

           $.post("/get_chid_categories.php", {
            parent_id: $(this).val(),
        }, function(response){

            setTimeout("finishAjax('show_sub_categories', '"+escape(response)+"')", 400);
        });

        return false;
    });
});

     function finishAjax(id, response){
     $('#loader').remove();

     $('#'+id).append(unescape(response));
    } 

</script>
<style>
    .both h4{ font-family:Arial, Helvetica, sans-serif; margin:0px; font-size:14px;}
     #search_category_id{ padding:3px; width:200px;}

     .parent{ padding:3px; width:150px; float:left; margin-right:12px;}
     .both{ float:left; margin:0 0px 0 0; padding:0px;}
</style>
</head>
<body>
    <div id="show_sub_categories">
        <select name="search_category" class="parent">
           <option value="" selected="selected">-- Categories --</option>
        <?php
           foreach ($view->result() as $row)
          {
          ?>
             <option value="<?php echo $row->id;?>"><?php echo $row->category;?></option>
          <?php
        }
        ?>
        </select>   

      </div>
       <br clear="all" /><br clear="all" />
</body>
</html>

控制器-Swim.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Swim extends CI_Controller {

    public function __construct()
        {
            parent::__construct();
                $this->datestring = '%d/%m/%Y';
                $this->datewrite = 'Y-d-m';
                $this->load->model('Swim_model');
                $this->load->helper('date');
                $data['not_logged_in'] = 'You are not logged in';
                $this->session->set_userdata($data);
        }

    public function index2()
        {
            $data['main_view'] = 'index2';
            $data['view'] = $this->Swim_model->CRUD_categories();
            $this->load->view('load_view',$data);
        }
}

模型-Swim_model.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Swim_model extends CI_Model {

    public function __construct()
        {
            // Call the CI_Model constructor
            parent::__construct();
        }

    public function CRUD_categories()
        {
        return $this->db->select('*')
        ->from('categories')
        ->where('pid', '0')
        ->get();
        }
}

get_child_categories.php-如果将其放在“根目录”中,则只能使其包含此文件

<?php

if($_REQUEST)
{
    $id     = $_REQUEST['parent_id'];
        $query = $this->db->select('*')
        ->from('categories')
        ->where('pid', $id)
        ->get();
        ?>
        <select name="sub_category" class="parent">
        <option value="" selected="selected">-- Sub Category --</option>
        <?php
    foreach ($query->result() as $row)
{
        ?>
            <option value="<?php echo $row->id;?>"><?php echo $row->category;?></option>
        <?php
        }?>
        </select>   
    <?php   
    }
?>

jquery.livequery.js-也在'root'中

/*! Copyright (c) 2008 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version: 1.0.3
 * Requires jQuery 1.1.3+
 * Docs: http://docs.jquery.com/Plugins/livequery
 */

(function($) {

$.extend($.fn, {
    livequery: function(type, fn, fn2) {
        var self = this, q;

        // Handle different call patterns
        if ($.isFunction(type))
            fn2 = fn, fn = type, type = undefined;

        // See if Live Query already exists
        $.each( $.livequery.queries, function(i, query) {
            if ( self.selector == query.selector && self.context == query.context &&
                type == query.type && (!fn || fn.$lqguid == query.fn.$lqguid) && (!fn2 || fn2.$lqguid == query.fn2.$lqguid) )
                    // Found the query, exit the each loop
                    return (q = query) && false;
        });

        // Create new Live Query if it wasn't found
        q = q || new $.livequery(this.selector, this.context, type, fn, fn2);

        // Make sure it is running
        q.stopped = false;

        // Run it immediately for the first time
        q.run();

        // Contnue the chain
        return this;
    },

    expire: function(type, fn, fn2) {
        var self = this;

        // Handle different call patterns
        if ($.isFunction(type))
            fn2 = fn, fn = type, type = undefined;

        // Find the Live Query based on arguments and stop it
        $.each( $.livequery.queries, function(i, query) {
            if ( self.selector == query.selector && self.context == query.context && 
                (!type || type == query.type) && (!fn || fn.$lqguid == query.fn.$lqguid) && (!fn2 || fn2.$lqguid == query.fn2.$lqguid) && !this.stopped )
                    $.livequery.stop(query.id);
        });

        // Continue the chain
        return this;
    }
});

$.livequery = function(selector, context, type, fn, fn2) {
    this.selector = selector;
    this.context  = context || document;
    this.type     = type;
    this.fn       = fn;
    this.fn2      = fn2;
    this.elements = [];
    this.stopped  = false;

    // The id is the index of the Live Query in $.livequery.queries
    this.id = $.livequery.queries.push(this)-1;

    // Mark the functions for matching later on
    fn.$lqguid = fn.$lqguid || $.livequery.guid++;
    if (fn2) fn2.$lqguid = fn2.$lqguid || $.livequery.guid++;

    // Return the Live Query
    return this;
};

$.livequery.prototype = {
    stop: function() {
        var query = this;

        if ( this.type )
            // Unbind all bound events
            this.elements.unbind(this.type, this.fn);
        else if (this.fn2)
            // Call the second function for all matched elements
            this.elements.each(function(i, el) {
                query.fn2.apply(el);
            });

        // Clear out matched elements
        this.elements = [];

        // Stop the Live Query from running until restarted
        this.stopped = true;
    },

    run: function() {
        // Short-circuit if stopped
        if ( this.stopped ) return;
        var query = this;

        var oEls = this.elements,
            els  = $(this.selector, this.context),
            nEls = els.not(oEls);

        // Set elements to the latest set of matched elements
        this.elements = els;

        if (this.type) {
            // Bind events to newly matched elements
            nEls.bind(this.type, this.fn);

            // Unbind events to elements no longer matched
            if (oEls.length > 0)
                $.each(oEls, function(i, el) {
                    if ( $.inArray(el, els) < 0 )
                        $.event.remove(el, query.type, query.fn);
                });
        }
        else {
            // Call the first function for newly matched elements
            nEls.each(function() {
                query.fn.apply(this);
            });

            // Call the second function for elements no longer matched
            if ( this.fn2 && oEls.length > 0 )
                $.each(oEls, function(i, el) {
                    if ( $.inArray(el, els) < 0 )
                        query.fn2.apply(el);
                });
        }
    }
};

$.extend($.livequery, {
    guid: 0,
    queries: [],
    queue: [],
    running: false,
    timeout: null,

    checkQueue: function() {
        if ( $.livequery.running && $.livequery.queue.length ) {
            var length = $.livequery.queue.length;
            // Run each Live Query currently in the queue
            while ( length-- )
                $.livequery.queries[ $.livequery.queue.shift() ].run();
        }
    },

    pause: function() {
        // Don't run anymore Live Queries until restarted
        $.livequery.running = false;
    },

    play: function() {
        // Restart Live Queries
        $.livequery.running = true;
        // Request a run of the Live Queries
        $.livequery.run();
    },

    registerPlugin: function() {
        $.each( arguments, function(i,n) {
            // Short-circuit if the method doesn't exist
            if (!$.fn[n]) return;

            // Save a reference to the original method
            var old = $.fn[n];

            // Create a new method
            $.fn[n] = function() {
                // Call the original method
                var r = old.apply(this, arguments);

                // Request a run of the Live Queries
                $.livequery.run();

                // Return the original methods result
                return r;
            }
        });
    },

    run: function(id) {
        if (id != undefined) {
            // Put the particular Live Query in the queue if it doesn't already exist
            if ( $.inArray(id, $.livequery.queue) < 0 )
                $.livequery.queue.push( id );
        }
        else
            // Put each Live Query in the queue if it doesn't already exist
            $.each( $.livequery.queries, function(id) {
                if ( $.inArray(id, $.livequery.queue) < 0 )
                    $.livequery.queue.push( id );
            });

        // Clear timeout if it already exists
        if ($.livequery.timeout) clearTimeout($.livequery.timeout);
        // Create a timeout to check the queue and actually run the Live Queries
        $.livequery.timeout = setTimeout($.livequery.checkQueue, 20);
    },

    stop: function(id) {
        if (id != undefined)
            // Stop are particular Live Query
            $.livequery.queries[ id ].stop();
        else
            // Stop all Live Queries
            $.each( $.livequery.queries, function(id) {
                $.livequery.queries[ id ].stop();
            });
    }
});

// Register core DOM manipulation methods
$.livequery.registerPlugin('append', 'prepend', 'after', 'before', 'wrap', 'attr', 'removeAttr', 'addClass', 'removeClass', 'toggleClass', 'empty', 'remove');

// Run Live Queries when the Document is ready
$(function() { $.livequery.play(); });


// Save a reference to the original init method
var init = $.prototype.init;

// Create a new init method that exposes two new properties: selector and context
$.prototype.init = function(a,c) {
    // Call the original init and save the result
    var r = init.apply(this, arguments);

    // Copy over properties if they exist already
    if (a && a.selector)
        r.context = a.context, r.selector = a.selector;

    // Set properties
    if ( typeof a == 'string' )
        r.context = c || document, r.selector = a;

    // Return the result
    return r;
};

// Give the init function the jQuery prototype for later instantiation (needed after Rev 4091)
$.prototype.init.prototype = $.prototype;

})(jQuery);

此文件会将类别加载到MYSQL数据库中

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL auto_increment,
  `category` varchar(50) NOT NULL,
  `pid` int(11),
  PRIMARY KEY  (`id`)
);

--
-- Dumping data for table `categories`
--

INSERT INTO `categories` (`id`, `category`, `pid`) VALUES
(1, 'Barncroft',0),
(2, 'Mary Rose',0),
(3, 'Petersfield',0),
(4, 'Redwood',0),
(5, 'Springfield',0),
(6, 'Mon',1),
(7, 'Tue',1),
(8, 'Wed',1),
(9, 'Thu',1),
(10, 'Fri',1),
(11, 'Sat',1),
(12, 'Sun',1),
(13, 'Mon',2),
(14, 'Tue',2),
(15, 'Wed',2),
(16, 'Thu',2),
(17, 'Fri',2),
(18, 'Sat',2),
(19, 'Sun',2),
(20, 'Mon',3),
(21, 'Tue',3),
(22, 'Wed',3),
(23, 'Thu',3),
(24, 'Fri',3),
(25, 'Sat',3),
(26, 'Sun',3),
(27, 'Mon',4),
(28, 'Tue',4),
(29, 'Wed',4),
(30, 'Thu',4),
(31, 'Fri',4),
(32, 'Sat',4),
(33, 'Sun',4),
(34, 'Mon',5),
(35, 'Tue',5),
(36, 'Wed',5),
(37, 'Thu',5),
(38, 'Fri',5),
(39, 'Sat',5),
(40, 'Sun',5),
(41, '07:00',6),
(42, '07:30',6),
(43, '08:00',6),
(44, '08:30',6),
(45, '09:00',6),
(46, '09:30',6),
(47, '10:00',6),
(48, '10:30',6),
(49, '11:00',6);
泥鲨

我会尝试将get_child_categories添加到您的Swim控制器作为一种方法:

function get_child_categories()
{

    $id = $_REQUEST['parent_id'];
     $query = $this->db->select('*')->from('categories')->where('pid', $id)->get();

    $html = '<select name="sub_category" class="parent">';
    $html .= '<option value="" selected="selected">-- Sub Category --</option>';

    foreach ($query->result() as $row){
        $html .= '<option value="'.$row->id.'">'.$row->category.'</option>';
    }

    $html .= '</select>';

    echo $html;

}

在您看来,您可以这样称呼它:

$.post("swim/get_child_categories", { //etc

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Codeigniter中使用数据库创建下拉列表

来自分类Dev

来自具有相同值的数据库的 PHP Codeigniter 下拉值

来自分类Dev

Codeigniter 中的动态数据库

来自分类Dev

带有 AJAX 动态依赖下拉列表的 Codeigniter

来自分类Dev

使用Codeigniter将数据从数据库检索到下拉列表

来自分类Dev

如何从Codeigniter中的多个选择下拉列表中将数据插入数据库?

来自分类Dev

Codeigniter从数据库制作列表

来自分类Dev

Codeigniter更新数据库记录列表

来自分类Dev

从数据库显示数据到下拉菜单CodeIgniter

来自分类Dev

codeigniter中的动态依赖下拉选择

来自分类Dev

在Codeigniter中动态加载数据库连接数据

来自分类Dev

来自Codeigniter中数据库的多级菜单

来自分类Dev

来自数据库限制的Codeigniter分页,偏移量

来自分类Dev

来自Codeigniter中数据库的多级菜单

来自分类Dev

如何在数据库上获取数据并在视图的“选择”下拉列表中显示(Codeigniter)

来自分类Dev

来自多个表的CodeIgniter下拉列表

来自分类Dev

在codeigniter中动态添加数据库

来自分类Dev

通过URL的CodeIgniter动态数据库连接

来自分类Dev

CodeIgniter如何动态设置自动加载的数据库

来自分类Dev

CodeIgniter更新数据库

来自分类Dev

Codeigniter数据库错误

来自分类Dev

Codeigniter 数据库配置

来自分类Dev

如何使用codeigniter和bootstrap显示从数据库中选择的值并显示在下拉列表中

来自分类Dev

Codeigniter - 在表/列表中显示数据库记录

来自分类Dev

从Codeigniter中的其他数据库切换动态数据库

来自分类Dev

jQuery CODEIGNITER的先前依赖的依赖下拉列表不会消失

来自分类Dev

Codeigniter / JS:从数据库获取数据

来自分类Dev

CodeIgniter:无法从数据库获取数据

来自分类Dev

基于带有 codeigniter 的单表数据值的依赖下拉列表

Related 相关文章

  1. 1

    在Codeigniter中使用数据库创建下拉列表

  2. 2

    来自具有相同值的数据库的 PHP Codeigniter 下拉值

  3. 3

    Codeigniter 中的动态数据库

  4. 4

    带有 AJAX 动态依赖下拉列表的 Codeigniter

  5. 5

    使用Codeigniter将数据从数据库检索到下拉列表

  6. 6

    如何从Codeigniter中的多个选择下拉列表中将数据插入数据库?

  7. 7

    Codeigniter从数据库制作列表

  8. 8

    Codeigniter更新数据库记录列表

  9. 9

    从数据库显示数据到下拉菜单CodeIgniter

  10. 10

    codeigniter中的动态依赖下拉选择

  11. 11

    在Codeigniter中动态加载数据库连接数据

  12. 12

    来自Codeigniter中数据库的多级菜单

  13. 13

    来自数据库限制的Codeigniter分页,偏移量

  14. 14

    来自Codeigniter中数据库的多级菜单

  15. 15

    如何在数据库上获取数据并在视图的“选择”下拉列表中显示(Codeigniter)

  16. 16

    来自多个表的CodeIgniter下拉列表

  17. 17

    在codeigniter中动态添加数据库

  18. 18

    通过URL的CodeIgniter动态数据库连接

  19. 19

    CodeIgniter如何动态设置自动加载的数据库

  20. 20

    CodeIgniter更新数据库

  21. 21

    Codeigniter数据库错误

  22. 22

    Codeigniter 数据库配置

  23. 23

    如何使用codeigniter和bootstrap显示从数据库中选择的值并显示在下拉列表中

  24. 24

    Codeigniter - 在表/列表中显示数据库记录

  25. 25

    从Codeigniter中的其他数据库切换动态数据库

  26. 26

    jQuery CODEIGNITER的先前依赖的依赖下拉列表不会消失

  27. 27

    Codeigniter / JS:从数据库获取数据

  28. 28

    CodeIgniter:无法从数据库获取数据

  29. 29

    基于带有 codeigniter 的单表数据值的依赖下拉列表

热门标签

归档