View events fullcalendar codeigniter

1

Hello! I want to retrieve the events from the database and display them in the calendar. Can you help me do that? 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'm 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 is 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,
    
asked by anonymous 11.01.2017 / 17:18

1 answer

1

Modeling 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 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!');
    },
   }
  });
});
    
11.01.2017 / 21:59