-1

Show events in the calendar after the loading screen.

events: {
    JSON.parse(get_data());
},

CONTROLLER all is ok no problem here expept for the JSON.PARSE

$calendar = array();
foreach($data_calendar as $key => $val) {
    $calendar[] = array(
        'id' => intval($val - > id),

        'title' => $val - > title,
        'description' => trim($val - > description),
        'start' => date_format(date_create($val - > start_date), "Y-m-d H:i:s"),
        'end' => date_format(date_create($val - > end_date), "Y-m-d H:i:s"),
        'color' => $val - > color,
    );
}

$data = array();
$data['get_data'] = json_encode($calendar);

$data['telaativa'] = 'agenda'; //retorna agenda penso que manté menu barra esquerda colapsada em agenda 
$data['tela'] = ('/calendario/view_agenda--');
view_agenda--

$this - > load - > view('view_home2', $data);

RESULT id with 1 or "1" in the result is because i declare intval for id so i can get both formats in output.

 'get_data' => string '[{"id":1,
 'get_data' => string '[{"id":"1",
array (size=3)
      'get_data' => string '[{"id":1,"title":"teste evento1","description":"descri\u00e7\u00e3o do evento bla bla bla","start":"2019-05-06 00:00:00","end":"2019-05-07 00:00:00","color":"#0071c5"},{"id":6,"title":"cert soldador 1-1","description":"descrtivo do certificado ser 111 bw t1-25mm s275","start":"2019-05-29 23:00:00","end":"2019-05-30 00:00:00","color":"#40E0D0"},{"id":7,"title":"cert soldador 1-2 soldador nr1","description":"certificado de soldador nr 1 doc 1-2","start":"2019-05-30 00:00:00","end":"2019-05-31 00:00:00","color'... (length=5865)
      'telaativa' => string 'agenda' (length=6)
      'tela' => string '/calendario/view_agenda--' (length=25)

VIEW

$(document).ready(function() {
    $('.date-picker').datepicker();
    $('#calendarIO').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicWeek,basicDay'
        },
        defaultDate: moment().format('YYYY-MM-DD'),
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        selectable: true,
        selectHelper: true,
        select: function(start, end) {
            $('#create_modal input[name=start_date]').val(moment(start).format('YYYY-MM-DD'));
            $('#create_modal input[name=end_date]').val(moment(end).format('YYYY-MM-DD'));
            $('#create_modal').modal('show');
            save();
            $('#calendarIO').fullCalendar('unselect');
        },
        eventDrop: function(event, delta, revertFunc) { // si changement de position
            editDropResize(event);
        },
        eventResize: function(event, dayDelta, minuteDelta, revertFunc) { // si changement de longueur
            editDropResize(event);
        },
        eventClick: function(event, element) {
            deteil(event);
            editData(event);
            deleteData(event);
        },

        events: {

            JSON.parse(get_data());

        },

    });
});

ERROR SyntaxError: missing : after property id

If i delete JSON.parse(get_data()); my calendar will be visible but no events. If I add an event it will be shown until I refresh the page. All function work expect loading data from DB into the calendar.

ADyson
  • 57,178
  • 14
  • 51
  • 63
G Ramos
  • 3
  • 1
  • 7
  • 2
    If get_data is a javascript function that returns a serialized array your code should read `events: JSON.parse(get_data()),` without the brackets. If that doesn't do it, what does your get_data function return? – JasonB May 24 '19 at 13:49
  • SCRIPT1003: Expected ':' – G Ramos May 24 '19 at 13:54
  • JSON.parse(get_data); – G Ramos May 24 '19 at 13:55
  • 1): `events:` needs to be given an array, not an object, so remove the `{` and `}` from the declaration. 2) where is the code of your get_data() function please? That seems to be the crucial part of this question, and yet you have not provided it. We can't really be sure what's happening without seeing that. – ADyson May 24 '19 at 13:58
  • events:JSON.parse(get_data); -> SyntaxError: missing } after property list[Saber mais] agenda:852:43note: { opened at line 822, column 42 – G Ramos May 24 '19 at 13:58
  • Look at controller – G Ramos May 24 '19 at 14:01
  • What do you mean? JavaScript can't call a server-side method like that. Where is the method called `getData()` in your JavaScript? – ADyson May 24 '19 at 14:05
  • I can put the full controller. – G Ramos May 24 '19 at 14:06
  • Why do you think it would help, exactly? Again: The controller code is **server-side** PHP code. JavaScript **cannot call a server-side method**. If you are going to write `get_data()` in **JavaScript** then you must somewhere declare `function get_data() { }` **in JavaScript**. – ADyson May 24 '19 at 14:07
  • get_data is a string returned by the controller wich i want to use in view – G Ramos May 24 '19 at 14:07
  • array (size=3) 'get_data' => string '[{"id":1, (...) This is returned from my controller from the server and get data will contain info to populate calendar for 2 months – G Ramos May 24 '19 at 14:09
  • Ok, I think you are fundamentally misunderstanding how to use CodeIgniter data, and the difference between client and server-side programming. I added an answer below which I think will help you. – ADyson May 24 '19 at 14:10

1 Answers1

0

You seem to be confused about how to use your PHP view data in your view.

I think maybe you should read this: https://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view . It shows you how to inject the PHP data you provided to the view into your page.

What you're doing now is writing pure JavaScript which tries to call a function which doesn't exist. What you need to do is write a PHP snippet to inject the server-side data into the HTML and JavaScript code which the PHP is creating.

I'm not a CodeIgniter expert, but based on that documentation page, what I think you should be writing is:

events: <?php echo $get_data; ?>,

If you inject it like this without quote marks, then it will be automatically treated as JavaScript array literal, so there's no need for JSON.parse(). This works because JSON syntax is a subset of valid JavaScript object syntax. If you use the View Source feature in your browser to view the finished output of your page, you'll see what I mean.

(Note that you also need to remove the { and } from the events: declaration, because fullCalendar expects an array, not an object.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • ok well i followed something alredy made so it can have bad code https://github.com/syifandimulyanto/codeigniter-fullcalendar – G Ramos May 24 '19 at 14:21
  • i have a return array – G Ramos May 24 '19 at 14:23
  • protected '_ci_cached_vars' => array (size=3) 'get_data' => string '[{"id":1,"title":"teste evento1","description":"descri\u00e7\u00e3o do evento bla bla bla","start":"2019-05-06 00:00:00","end":"2019-05-07 – G Ramos May 24 '19 at 14:24
  • events: , – G Ramos May 24 '19 at 14:32
  • IT WORKED THANKS – G Ramos May 24 '19 at 14:33
  • In that github page you mentioned, they actually wrote `JSON.parse(get_data)` (without an extra `()` to make it a function call). So in that code, `get_data` refers to a JavaScript variable. And earlier in the script they wrote `var get_data = '';` to declare that variable and place the PHP data in it (as a string, not a literal). That code is here: https://github.com/syifandimulyanto/codeigniter-fullcalendar/blob/master/application/views/calendar.php You seem to have missed that step out, and also made a typo. So you didn't quite follow it correctly. – ADyson May 24 '19 at 14:42
  • But I'm glad you got it working by my method, which is very similar. – ADyson May 24 '19 at 14:43