• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get JSON data in table - Codeigniter

#1
[url=https://stackoverflow.com/questions/52217079/how-to-get-json-data-in-table-codeigniter/52217327?noredirect=1#][/url]
Hello I want to display data on click without refresh page in codeigniter, I got data in JSON but how to display in table view, so here is my code so please help me.

Here Is My Controller:
Code:
public function get_event_date() {
   $this->load->model('main_model');
   $start_event = $this->input->POST("start_event");
   $end_event = $this->input->POST("end_event");
   $events = $this->main_model->getallevent($start_event,$end_event);
   $data_events = array();
   foreach ($events->result() as $r) {
       $data_events[] = array(
           "id" => $r->id,
           "title" => $r->title,
           "start_event" => $r->start_event,
           "end_event" => $r->end_event,
       );
   }
   echo json_encode(array("events" => $data_events));
}

Here Is My Model:
Code:
public function getallevent($start_event,$end_event)
{
   return $this->db->where("start_event >=",$start_event)->where("end_event <=", $end_event)->get("events");
}
Here My ajax:

Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
   $(document).ready(function () {
// code to get all records from table via select box
$("#getdata").click(function () {

   var event_type = $('#event_type').val();
   var start_event = $('#start_event').val();
   var end_event = $('#end_event').val();

            //debugger;        
            $.ajax({

               url: "<?php echo base_url('welcome/get_event_date'); ?>",
               type: "POST",
               dataType: "json",
               data: {"event_type": event_type, "start_event": start_event, "end_event": end_event},
               success: function (data) {
                   alert(data);
               },
               error: function (data) {
                   // do something
                   alert(data);
               }
           });
        });
});
</script>

Here is My View:
<table id="example1" class="table table-bordered table-striped">
       <thead>
        <tr>
           <th>Date</th>
           <th>Event Type</th>
           <th>Event Name</th>

       </tr>
   </thead>
   <?php
   foreach ($allevents as $get) {
       ?>                                                  
       <tbody>
           <tr>

               <td><?php echo $get->start_event; ?></td>
               <td><?php echo $get->event_type; ?></td>
               <td><?php echo $get->title; ?></td>
           </tr>
       </tbody>
       <?php
   }
   ?>

</table>
Reply

#2
Use JSON.parse(data) in the success part of AJAX:
Code:
records = JSON.parse(data);

A few remarks on php styling.
If you mix php and html, the alternative syntax for control structures can be very helpful:
PHP Code:
<?php foreach($records as $record) : ?>

  <?php if ($t == 'blabla') : ?>
     <p>You said blabla</p>
  <?php else : ?>
     <p>You said something else…</p>
  <?php endif; ?>
  
<?php endforeach; ?>

In your page, you have the <tbody> inside the foreach structure. That's not necessary. Place it before the foreach, and the </tbody> after it.

In stead of "<?php echo" you can use "<?=" in views. Makes it more compact and better to read.
Reply

#3
(09-07-2018, 03:36 AM)Wouter60 Wrote: Use JSON.parse(data) in the success part of AJAX:
Code:
records = JSON.parse(data);

A few remarks on php styling.
If you mix php and html, the alternative syntax for control structures can be very helpful:
PHP Code:
<?php foreach($records as $record) : ?>

  <?php if ($t == 'blabla') : ?>
     <p>You said blabla</p>
  <?php else : ?>
     <p>You said something else…</p>
  <?php endif; ?>
  
<?php endforeach; ?>
Reply

#4
(09-07-2018, 03:53 AM)jaydevvara Wrote:
(09-07-2018, 03:36 AM)Wouter60 Wrote: Use JSON.parse(data) in the success part of AJAX:
Code:
records = JSON.parse(data);

A few remarks on php styling.
If you mix php and html, the alternative syntax for control structures can be very helpful:
PHP Code:
<?php foreach($records as $record) : ?>

  <?php if ($t == 'blabla') : ?>
     <p>You said blabla</p>
  <?php else : ?>
     <p>You said something else…</p>
  <?php endif; ?>
  
<?php endforeach; ?>

i already added json in success part but i did not get in response in success part
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.