如何在codeigniter中传递id数据库到jquery


How to pass id database into jquery in codeigniter?

嗨,我是一个新的代码编码器,我想通过ID数据库进入jquery,但我不能这样做,我尝试搜索谷歌youtube,但我仍然不能这个代码我已经卡了几天了,谁能帮帮我呢?

这是原始代码:

     <style>
  @import url(http://fonts.googleapis.com  /css?family=Open+Sans:400,300,600);  
    html {
      border-top: 5px solid #fff;
      background: #58DDAF;
      color: #2a2a2a;
    }
    html, body {
      margin: 0;
      padding: 0;
      font-family: 'Open Sans';
    }
    h1 {
      color: #fff;
      text-align: center;
      font-weight: 300;
    }
    .slider {
      position: relative;
      overflow: hidden;
      margin: 20px auto 0 auto;
      border-radius: 4px;
    }
    .slider ul {
      position: relative;
      margin: 0;
      padding: 0;
      height: 200px;
      list-style: none;
    }
    .slider ul li {
      position: relative;
      display: block;
      float: left;
      margin: 0;
      padding: 0;
      width: 500px;
      height: 300px;
      background: #ccc;
      text-align: center;
      line-height: 300px;
    }
    .slider ul li img{
      width: 100%;
    }
    a#control_prev, a#control_next {
      position: absolute;
      top: 40%;
      z-index: 999;
      display: block;
      padding: 4% 3%;
      width: auto;
      height: auto;
      background: #2a2a2a;
      color: #fff;
      text-decoration: none;
      font-weight: 600;
      font-size: 18px;
      opacity: 0.8;
      cursor: pointer;
    }
    a#control_prev:hover, a#control_next:hover {
      opacity: 1;
      -webkit-transition: all 0.2s ease;
    } 
    a#control_prev {
      border-radius: 0 2px 2px 0;
    }
    a#control_next {
      right: 0;
      border-radius: 2px 0 0 2px;
    }
    .slider_option {
      position: relative;
      margin: 10px auto;
      width: 160px;
      font-size: 18px;
    }
      </style>
      <script src="<?php echo base_url().'./style/jquery.js'; ?>"></script>
    </head>
    <body>
        <script>
          jQuery(document).ready(function ($) { 
          var slideCount = $('#slider2 ul li').length;
          var slideWidth = $('#slider2 ul li').width();
          var slideHeight = $('#slider2 ul li').height();
          var sliderUlWidth = slideCount * slideWidth;
          $('#slider2').css({ width: slideWidth, height: slideHeight });
          $('#slider2 ul').css({ width: sliderUlWidth, marginLeft: - slideWidth  });
            $('#slider2 ul li:last-child').prependTo('#slider2 ul');
            function moveLeft() {
                $('#slider2 ul').animate({
                    left: + slideWidth
                }, 200, function () {
                    $('#slider2 ul li:last-child').prependTo('#slider2 ul');
                    $('#slider2 ul').css('left', '');
                });
            };
            function moveRight() {
                $('#slider2 ul').animate({
                    left: - slideWidth
                }, 200, function () {
                    $('#slider2 ul li:first-child').appendTo('#slider2 ul');
                    $('#slider2 ul').css('left', '');
                });
            };
            $('a.control_prev1').click(function () {
                moveLeft();
            });
            $('a.control_next1').click(function () {
                moveRight();
            });
        }); 
        </script>
        <div id="slider2" class="slider">
          <a href="#" id="control_next" class="control_next1">>></a>
          <a href="#" id="control_prev" class="control_prev1"><</a>
          <ul>
            <li><img src="<?php echo base_url().'./images/events/ll.png' ?>"></li>
            <li style="background: #aaa;"><img src="<?php echo base_url().'./images/events/blue.jpg' ?>"></li>
            <li style="background: #aaa;"><img src="<?php echo base_url().'./images/events/red.jpg' ?>"></li>
          </ul>  
        </div>
        </body>
        </html>

我试着传递ID,但它不工作,请帮助我:

My database: events
           id image image2 image3
           1  1a.jpg 2a.jpg  3a.jpg
           2  1b.jpg 2b.jpg  3b.jpg
           3  1b.jpg 2c.jpg  3c.jpg

     <?php $query = $this->db->get('events'); ?>
<?php foreach($query->result() as $val): ?>
<script>
  jQuery(document).ready(function ($) { 
  var id = "#slider<?php echo $val->id; ?>";
  var ida = ".control_next<?php echo $val->id; ?>";
  var idb = ".control_prev<?php echo $val->id; ?>";

  var slideCount = $(id + ' ul li').length;
  var slideWidth = $(id + ' ul li').width();
  var slideHeight = $(id + ' ul li').height();
  var sliderUlWidth = slideCount * slideWidth;
  $(id).css({ width: slideWidth, height: slideHeight });
  $(id + ' ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });
    $(id + ' ul li:last-child').prependTo(id + ' ul');
    function moveLeft() {
        $(id + ' ul').animate({
            left: + slideWidth
        }, 200, function () {
            $(id + ' ul li:last-child').prependTo(id + ' ul');
            $(id + ' ul').css('left', '');
        });
    };
    function moveRight() {
        $(id + ' ul').animate({
            left: - slideWidth
        }, 200, function () {
            $(id + ' ul li:first-child').appendTo(id + ' ul');
            $(id + ' ul').css('left', '');
        });
    };
    $('a.control_prev ' + idb).click(function () {
        moveLeft();
    });
    $('a.control_next ' + ida).click(function () {
        moveRight();
    });
}); 
</script>
<div id="slider<?php echo $val->id; ?>" class="slider">
  <a href="#" id="control_next" class="control_next<?php echo $val->id; ?>">>></a>
  <a href="#" id="control_prev" class="control_prev<?php echo $val->id; ?>"><</a>
  <ul>
    <li><img src="<?php echo base_url().'./images/events/'.$val->image; ?>"></li>
    <li style="background: #aaa;"><img src="<?php echo base_url().'./images/events/'.$val->image2; ?>"></li>
    <li style="background: #aaa;"><img src="<?php echo base_url().'./images/events/'.$val->image3; ?>"></li>
  </ul>  
</div>
<?php endforeach; ?>

无论何时使用id,都应该使用连接操作符。同时去掉id的括号。没必要。

var slideCount = $(id + ' ul li').length;
                      ^  ^// add a space here.
var slideWidth = $(id + 'ul li').width();
var slideHeight = $(id + 'ul li').height();

我只给了3行,请改变它在所有需要的地方在你的jquery

$('a.' + idb).click(function () {
    moveLeft();
});
$('a.' + ida).click(function () {
    moveRight();
});