如何将json数据发送到codeigniter视图 [英] How to send json data to codeigniter view

查看:94
本文介绍了如何将json数据发送到codeigniter视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有一个问题,基于我将如何用我的控制器返回的数据替换我的网页部分。我有一个页面直接从我的数据库显示电影的细节,我想当用户选择一个特定的类型,然后只显示该特定类别的电影。我已经设法从选择框中获取每个类型的名称并将其发送到控制器,我将使用它从数据库中提取数据。在我的HTML中我使用foreach循环来加载 ALL 数据,我的问题是如何将它们显示回视图并操纵我的php / html代码以显示它们(实际上替换了现有的php / html与new作为ajax请求)。我知道我必须使用json_encode()将它们作为对象发回,但我不知道如何显示它们或迭代php / html标签中的数据。

Hi i have a question based on how i am going to replace my web page section with the returned data from my controller. I have a page that displays the details of the movies directly from my database and i want when a user selects a specific genre then only the movies of that certain category wil be displayed. I have managed to get the name of each genre from the select box and send it to the controller were i will use it to extract the data from the database. In my html i am using a foreach loop to load ALL the data and my problem is how i will display them back to the view and manipulate my php/html code in order to display them(actually replace the existing php/html with the new as an ajax request). I know i have to use json_encode() to send them back as an object but i don't know how to display them or iterate the data in the php/html tags.

Javascript

Javascript

$(".select__sort").change(function()
    {

        var genre = $(".select__sort").val();

        $.ajax({
            type: "POST",
            url: "http://localhost:8888/Cinema_Seat_Booking_System/movie_list_full/order_by_genre",
            data: {'cat':genre},
            dataType:"json",
            cache: false,
        success: function (data) {

        }
      });
    });

HTML

  <div class="select-area">

                <form class="select select--film-category" method='get'>
                      <select name="select_item" class="select__sort" tabindex="0">
                        <option value="default" selected="selected">All</option>
                          <?php foreach($genres as $category): ?>
                          <option value="<?php echo $category->genre ?>" selected="selected"> <?php echo $category->genre ?></option>
                          <?php endforeach ?>
                    </select>
                </form>

        </div>

HTML foreach循环电影数据

HTML foreach loop movie data

<!-- Movie preview item -->
            <?php foreach($movies_data as $movie_data) :?>
            <div class="movie movie--preview movie--full release">
                 <div class="col-sm-3 col-md-2 col-lg-2">
                        <div class="movie__images">
                            <img alt='' src=<?php echo $movie_data->path ?>>
                        </div>

                </div>

                <div class="col-sm-9 col-md-10 col-lg-10 movie__about">
                        <a href='view_movie_page_full.php' class="movie__title link--huge"><?php echo $movie_data->title ?></a>

                        <p class="movie__time"><?php echo $movie_data->duration_min ?></p>

                        <p class="movie__option"><strong>Country: </strong><a href="#"><?php echo $movie_data->country ?></a href="#"></p>
                        <p class="movie__option"><strong>Category: </strong><a href="#"><?php echo $movie_data->genre ?></a></p>
                        <p class="movie__option"><strong>Director: </strong><a href="#"><?php echo $movie_data->director ?></a></p>
                        <p class="movie__option"><strong>Actors: </strong><a href="#"><?php echo $movie_data->actors ?></a></p>

                        <div class="movie__btns">
                            <a href="<?php echo site_url('book1')?>" class="btn btn-md btn--warning">book a ticket <span class="hidden-sm">for this movie</span></a>
                        </div>

                        <div class="preview-footer">
                            <div class="movie__rate"><div class="score"></div><span class="movie__rate-number"></span> </div>


                        </div>
                </div>

                <div class="clearfix"></div>



            </div>
            <?php endforeach ?>
            <!-- end movie preview item -->

控制器

 public function order_by_genre()
{
    $this->load->model('movie_list_full_model');

    $ajax_request = $this->input->post('cat');
    $query_data['genres'] = $this->movie_list_full_model->get_all_genres($ajax_request);


    $this->load->view('templates/header');
    $this->load->view('home/view_movie_list_full', $query_data);
    $this->load->view('templates/footer_movie_list_full');

}

ps我希望页面在不重新加载的情况下更改数据它,这就是我用ajax尝试它的原因,但我对此很新,我被卡住了。有什么帮助吗?

ps I want the page to change the data with out reloading it, that's the reason i am trying it with ajax but i am fairly new to this and i am stuck. Any help?

推荐答案

你可以检查ajax使用post来获取你的控制器然后返回json编码

You can check if ajax used post to get to your controller and then return json encode

public function order_by_genre(){

$this->load->model('movie_list_full_model');
$is_post = $this->input->server('REQUEST_METHOD') == 'POST';

if($is_post){
     $ajax_request = $this->input->post('cat');

     echo json_encode( $this->movie_list_full_model->get_all_genres($ajax_request));
  }
else {
      $this->load->view('templates/header');
      $this->load->view('home/view_movie_list_full', $query_data);
      $this->load->view('templates/footer_movie_list_full');
     }
}

然后在javascript中你可以使用$(this) .val()它获取更改元素的值(你可以使用它很多)然后如果ajax调用在div中成功,你显示你的电影只是附加每个电影与forEach循环

And then in javascript you can use $(this).val() it gets the value of changed element ( you can use this a lot ) and then if ajax call is success in div where you show your movies just append every movie with forEach loop

$(".select__sort").change(function()
{

    var genre = $(this).val();

    $.ajax({
        type: "POST",
        url: "http://localhost:8888/Cinema_Seat_Booking_System/movie_list_full/order_by_genre",
        data: {'cat':genre},
        dataType:"json",
        cache: false,
        success: function (data) {
          $("#movies").html(" "); //clear everything from movie div

            data.forEach(function(entry) {   
             $("#movies").append("<div>"+entry.movie+"</div>");
               //just add everything here
             });
        }
    });
});

这篇关于如何将json数据发送到codeigniter视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆