S
S
Sama Samsonov2018-04-25 08:30:33
PHP
Sama Samsonov, 2018-04-25 08:30:33

How to convert json from php array?

How to display data correctly
displayed data from the database and passed it to the template
in the body of the template wrote a script

$(document).ready(function() {
        $('#calendar').fullCalendar({
            events : [
                <?php
//	            $datesquerysss = json_encode($datesquerysss); // не работает
              	foreach($datesquerysss as $task){
              	title : '$task->start_time';  //  json_encode($task->start_time) еше один вариант не работает
              	start : '$task->stop_time';	            
              	}
                ?>
            ]
        })
    });

how to parse in json

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yan-s, 2018-04-25
@samuser

Horses mixed up in a bunch, people ...
In the controller, iterate over the array into a new structure that matches the required format, such as:

$events = [
  'events' => [
    ['title' => '...', 'start ' => '...'],
    ['title' => '...', 'start ' => '...'],
    ['title' => '...', 'start ' => '...'],
    ['title' => '...', 'start ' => '...'],
  ]
];

in the view, pass the result through json_encode, you get:
{
  "events": [
    {
      "title": "...",
      "start ": "..."
    },
    {
      "title": "...",
      "start ": "..."
    },
    {
      "title": "...",
      "start ": "..."
    },
    {
      "title": "...",
      "start ": "..."
    }
  ]
}

and output with echo or the appropriate construct of your template engine:
var caledarEvents = <?php echo json_encode($events); ?>;
$(document).ready(function() {
        $('#calendar').fullCalendar(caledarEvents)
});

K
Kalombyr, 2018-04-25
@Kalombyr

I think you should start learning from the basics.
You have mixed everything together. First, for some reason, you convert a string to Json, then you try to parse it in a loop ...
If you need data from php to javascript, then you can do this, for example:
index.html

<?php
    $datesquerysss = ....
?>
<script>
   var jsonstring = ' <?php  echo json_encode($datesquerysss);  ?> ';
    ...... 
    var jsonobject =  JSON.parse(jsonstring );
   console.log(jsonobject);
</script>

In json, you can not convert at all, but immediately collect the js object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question