View fullcalendar codeigniter events

Asked

Viewed 566 times

1

Hello! I want to retrieve the events from the database and display them in the calendar. Can you help me to do this? So far I have the following codes:

Model:

Public function obter_noticias()
{
$sql = "SELECT * FROM noticias WHERE noticias.id";
return $this->db->query($sql, array($_GET['start'], $_GET['end']))->result();
}

controller

Public function obter_noticias()
{
    $result=$this->Noticias_model->obter_noticias();
    echo json_encode($result);
    echo $this->db->last_query();            
    die();
}

In the.js file, I am using the following code

var base_url='http://localhost/ci_adminlte/admin';
$(document).ready(function(){
    $('#calendar').fullCalendar({
    //alert('Teste de carregamento FullCalendar');
    header: {
            left: 'prev,next today',
            center: 'title',
            //right: 'month,basicWeek,basicDay'
            right: 'month,agendaWeek,agendaDay,listWeek,'
        },        
        editable: false,
    navLinks: true,
    //selectable: true,
    selectHelper: true,
    events: base_url+'noticias/obter_noticias'
    });
});

Below, follow my table

id INT(11) NOT NULL AUTO_INCREMENT,
 data DATETIME NOT NULL,
 titulo TEXT NULL DEFAULT NULL,
 noticia TEXT NULL DEFAULT NULL,
 usuario_cadastro VARCHAR(40) NOT NULL,
 usuario_alteracao VARCHAR(40) NOT NULL,
 dt_cadastro DATETIME NOT NULL,
 dt_alteracao DATETIME NULL DEFAULT NULL,
  • Why echo $this->db->last_query();?

  • To test the queries.

  • And how about the answer to JavaScript? Truncated?

  • Truncated, yes!!

  • But then it won’t work anyway, champ.

  • But every function that renders the fullcalendar is in this file. So I believe Events also has to be in this code.

  • What’s it like? The function that renders the fullcalendar is in the controller? This is not correct. The controller should only return the object json. The library fullcalendar will only use the object generated by controller in his events.

  • It is not in the controller, It is in a file . js, called through the view.

  • And where is the VIEW? You could save this object in a variable and pass to VIEW click on JavaScript. Have you tried that?

  • I tried, The way I have in the example above, I put some Events manually and so it appears, but when I try to popular through the database, I can’t

Show 5 more comments

1 answer

1


Model method that captures db data:

function obter_noticias() {
        $sql = "SELECT * FROM noticias ORDER BY noticias.data ASC";
        $query = $this->db->query($sql);
        $array = $query->result_array();
        return $array;
    }

Method of controller that returns the object json

function Test_Function(){
  $noticias = $this->model->obter_noticias();
  $obj = NULL;
  //The cat's leap: criar o objeto para a variavel 'events' 
  foreach ($noticias as $i) {
    $obj[] = [
        'title' => $i['titulo'],
        'start' => $i['data']
    ];
   }
   echo json_encode($obj);
   exit();
}

javascript:

$(document).ready(function(){
  $('#calendar').fullCalendar({
  //alert('Teste de carregamento FullCalendar');
  header: {
    left: 'prev,next today',
    center: 'title',
    right: 'month,agendaWeek,agendaDay,listWeek,'
  },
  editable: false,
  navLinks: true,
  selectHelper: true,
  events: {
    url: 'dashboard/Test_Function',
    type: 'POST',
    data: {
     value1: 'aaa',
     value2: 'bbb'
    },
    success: function(data) {
     console.log(data);
    },
    error: function() {
     alert('Erro ao carregar eventos!');
    },
   }
  });
});
  • Hello, sorry for the delay, I was trying to apply the way you did. I couldn’t. I believe that the structure of my files does not allow it to be done this way. Example: I am not calling the fullcalendary through <scritp> in the view, I’m calling through an external javascript file. And this one doesn’t handle PHP, because it works on client-server. So I’m trying to use JSON, and I’m trying to call the controller and its method through javascript.

  • So in this javascript file, I have this: events: {&#xA; url: 'dashboard/Test_Function',&#xA; type: 'POST',&#xA; data: {&#xA; value1: 'aaa',&#xA; value2: 'bbb'&#xA; },&#xA; success: function(data) {&#xA; console.log(data);&#xA; },&#xA; error: function() {&#xA; alert('Erro ao carregar eventos!');&#xA; }, &#xA; }

  • and inside the Dashboard controller, I have this code: function Test_Function()&#xA; { &#xA; $data = array&#xA; (&#xA; array&#xA; (&#xA; 'id' => 1234, &#xA; 'title' => "Nome: Wagner",&#xA; 'start' => "2017-01-13"&#xA; ),&#xA; ); &#xA; echo json_encode($data); &#xA; } But in console.log, obey this: POST http://localhost/ci_adminlte/dashboard/Test_Function net::ERR_CONTENT_DECODING_FAILED

  • I did so because you did not show the method that carries the VIEW, just showed the method that returns the object json. You can use a helper that will instantiate the object in a global way, but I need to know how you are loading the calendar.

  • I managed to load successfully. needed to set $config['compress_output'] = FALSE;in /config/config.php codeigniter. My function: function Test_Function()&#xA; {&#xA; $data = array&#xA; (&#xA; array&#xA; (&#xA; 'id' => 1234, &#xA; 'title' => "Wagner Fillio",&#xA; 'start' => "2017-01-13"&#xA; ),&#xA; ); &#xA; echo json_encode($data);&#xA; }

  • Now, can you help me with a function to get the database data? Something like: <?php &#xA;$notices = $this->db->get('noticias')->result_array();&#xA; foreach($notices as $row):&#xA;?>&#xA;{&#xA; title: "<?php echo $row['titulo'];?>",&#xA; start: new Date(<?php echo date('Y',$row['data']);?>, <?php echo date('m',$row['data'])-1;?>, <?php echo date('d',$row['data']);?>)&#xA;},&#xA;<?php &#xA; endforeach&#xA;?>

  • Pera... Compression? Solved what with response compression? Managed to load what? Where? Confused. You are using one method to form the object and another to load the calendar, right?

  • Loading database data is no problem. The answer already has an example you can use in model. The problem is to pass the object json to the javascript to fill the calendar.

  • Come on: Baggy... First mistake was: net::ERR_CONTENT_DECODING_FAILED. So I decided to change the codeigniter configuration config/config.phpconfig/config.php for false

  • 1

    Perfect your help! Thank you so much... Worked perfectly..

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.