marilena6
marilena6

Reputation: 97

Codeigniter query result display

My view

 <?php echo $calendar; ?>
 <div id="event_area"></div>
 <script type="text/javascript">
 $(document).ready(function(){      

    $('.calendar .day').click(function(){

      var day_data= $(this).find('.content').html();

      if($(this).find('.content').html()!=null){

        $.ajax({
            url: "<?php echo base_url('calendar/pass_name')?>/",
            method:"POST",
            data:{'day_data':day_data},
            success:function(data){


                 $('#event_area').html(data);


            }


        })

My controller:

function pass_name(){

    $name=$this->input->post('day_data');

    $result=$this->load->model('calendar_model')->get_description($name);
    $data['calendar'] = $result;

    echo json_encode($data);

}

My model:

function get_description($name){
    $query = $this->db
  ->select('description')
  ->from('calendar')
  ->where('name',$name)
  ->get();

   return $query->result();

}

This is an example of the result I am getting in my view. {"calendar":[{"description":"test description\r\nwww.google.com</a>"}]} How can I make it better, for instance displaying "test description.." without the characters?

Thanks in advance.

Upvotes: 0

Views: 325

Answers (2)

user5139148
user5139148

Reputation:

In your ajax success function replace the code as follow

success:function(data){
    var obj = $.parseJSON(data);
    var content = '';
    $.each(obj.calendar, function(i,v){
        content+= '<p>'+v.description+'</p>'; //--> here you can add any tag, class
    });
    $("#event_area").html(content);
}

Hope this code may solve your problem. If you need any further clarification, I'm happy to help you.

Upvotes: 1

Shihas
Shihas

Reputation: 814

You have to add dataType:'json' in your ajax code:

$.ajax({
        url: "<?php echo base_url('calendar/pass_name')?>/",
        method:"POST",
        dataType:'json',
        data:{'day_data':day_data},
        success: function(data){
             $('#event_area').html(data);
        }
});

Upvotes: 0

Related Questions