Adding php data into javascript object

Discuss coding issues, and scripts related to PHP and MySQL.
mlucicom
Posts:37

Adding php data into javascript object

Hello! I want to add some php code in this javascript section

Code: Select all

 <script>
            $(document).ready(function() {
            $('#calendar').fullCalendar({

            <!--Header Section Including Previous,Next and Today-->
            header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicWeek,basicDay'
            },

            <!--Default Date-->
            defaultDate: '2018-01-01',
            editable: true,

            <!--Event Section-->
            eventLimit: true, // allow "more" link when too many events
            events: [
            {
            title: 'All Day Event',
            start: '2015-02-01'
            },
            {
            title: 'Long Event',
            start: '2015-02-07',
            end: '2015-02-10'
            },
            {
            id: 999,
            title: 'Repeating Event',
            start: '2015-02-09T16:00:00'
            },
            {
            id: 999,
            title: 'Repeating Event',
            start: '2015-02-16T16:00:00'
            },
            {
            title: 'Conference',
            start: '2015-02-11',
            end: '2015-02-13'
            },
          
            {
            title: 'Meeting',
            start: '2015-02-12T10:30:00',
            end: '2015-02-12T12:30:00'
            },
            {
            title: 'Lunch',
            start: '2015-02-12T12:00:00'
            },
            {
            title: 'Meeting',
            start: '2015-02-12T14:30:00'
            },
            {
            title: 'Happy Hour',
            start: '2015-02-12T17:30:00'
            },
            {
            title: 'Dinner',
            start: '2015-02-12T20:00:00'
            },
            {
            title: 'Birthday Party',
            start: '2015-02-13T07:00:00'
            },
            
            {
            title: 'Click for Google',
            url: 'http://google.com/',
            start: '2015-02-28'
            }
            ]
            });
            });
</script>
i want something like this

Code: Select all

{<?php 
 <--php code for select results from database and foreach this-->
  title: '<$databasevalue1;>',
  url: '$databasevalue2;',
  start: '$databasevalue3;'
}

Admin Posts:805
Hello
Try test and apply the code from this example:

Code: Select all

//array with data from database
$arr =[
 [
  'title'=>'Title 1',
  'url'=>'URL 1',
  'start'=>'Start 1'
 ],
 [
  'title'=>'Title 2',
  'url'=>'URL 2',
  'start'=>'Start 2'
 ]
];

$jsob =[];
foreach($arr as $k=>$v){
  //adds data from $arr in $jsob to be transformed in object for JavaScript
  $jsob[] =[
    'title'=>$v['title'],
    'url'=>$v['url'],
    'start'=>$v['start']
  ];
}

//transform $jsob in JSON object because it can be used directly in JavaScript
$jsob = json_encode($jsob);

//makes a JS script with $jsob
echo '<script>
var jsob ='. $jsob .';

//test
alert(jsob[0]["title"]);
</script>';
- The ideea is to add the php values into an array in php, like you want to be in javascript; then, apply json_encode() to convert that array into a JSON object which can be added directly in javascript code.

mlucicom Posts:37
i Tried to integrate yout code but don't work
you have an idea where I'm wrong

Code: Select all

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Bootstrap Calendar</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!--Sylesheets and JavaScript files to be included-->
        <link href='fullcalendar.css' rel='stylesheet' />
        <link href='fullcalendar.print.css' rel='stylesheet' media='print' />
        <script src='moment.min.js'></script>
        <script src='jquery.min.js'></script>
        <script src='fullcalendar.min.js'></script>
        <?php 
$arr =[
 [
  'title'=>'Title 1',
  'url'=>'URL 1',
  'start'=>'2018-01-01'
 ],
 [
  'title'=>'Title 2',
  'url'=>'URL 2',
  'start'=>'2018-01-02'
 ]
];

$jsob =[];
foreach($arr as $k=>$v){
  //adds data from $arr in $jsob to be transformed in object for JavaScript
  $jsob[] =[
    'title'=>$v['title'],
    'url'=>$v['url'],
    'start'=>$v['start']
  ];
}

//transform $jsob in JSON object because it can be used directly in JavaScript
$jsob = json_encode($jsob);

//makes a JS script with $jsob
echo '<script>
var jsob ='. $jsob .';

//test
alert(jsob[0]["title"]);
</script>';
        ?>
        <script>
            $(document).ready(function() {
            $('#calendar').fullCalendar({

            <!--Header Section Including Previous,Next and Today-->
            header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicWeek,basicDay'
            },

            <!--Default Date-->
            defaultDate: '2018-01-01',
            editable: true,

            
            <!--Event Section-->

            eventLimit: true, // allow "more" link when too many events
            events: [
            {
            title: "<?php echo 'ok';?>",
            start: '2018-01-01'
            },
            {
            title: 'Long Event',
            start: '2015-02-07',
            end: '2015-02-10'
            },
            {
            id: 999,
            title: 'Repeating Event',
            start: '2015-02-09T16:00:00'
            },
            {
            id: 999,
            title: 'Repeating Event',
            start: '2015-02-16T16:00:00'
            },
            {
            title: 'Conference',
            start: '2015-02-11',
            end: '2015-02-13'
            },
          
            {
            title: 'Meeting',
            start: '2015-02-12T10:30:00',
            end: '2015-02-12T12:30:00'
            },
            {
            title: 'Lunch',
            start: '2015-02-12T12:00:00'
            },
            {
            title: 'Meeting',
            start: '2015-02-12T14:30:00'
            },
            {
            title: 'Happy Hour',
            start: '2015-02-12T17:30:00'
            },
            {
            title: 'Dinner',
            start: '2015-02-12T20:00:00'
            },
            {
            title: 'Birthday Party',
            start: '2015-02-13T07:00:00'
            },
            
            {
            title: 'Click for Google',
            url: 'http://google.com/',
            start: '2015-02-28'
            }
            ]
            });
            });
        </script>
        <!--Styling for calendar-->
        <style>
            body {
            margin: 40px 10px;
            padding: 0;
            font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
            font-size: 14px;
            }
            #calendar {
            max-width: 900px;
            margin: 0 auto;
            }
        </style>
    </head>
    <body>
        <div id='calendar'></div>
    </body>
</html>


Admin Posts:805
if what you want is to add js objects from php in the 'events' array in javascript, try this:

Code: Select all

<?php
$arr =[
  [
  'title'=>'Title 1',
  'url'=>'URL 1',
  'start'=>'2018-01-01'
  ],
  [
  'title'=>'Title 2',
  'url'=>'URL 2',
  'start'=>'2018-01-02'
  ]
];

$jsob =[];
foreach($arr as $k=>$v){
  //adds data from $arr in $jsob to be transformed in object for JavaScript
  $jsob[] =[
  'title'=>$v['title'],
  'url'=>$v['url'],
  'start'=>$v['start']
  ];
}
//transform $jsob in JSON object because it can be used directly in JavaScript
$jsob = json_encode($jsob);
?>
<script>
$(document).ready(function() {
  $('#calendar').fullCalendar({
  <!--Header Section Including Previous,Next and Today-->
    header: {
    left: 'prev,next today',
    center: 'title',
    right: 'month,basicWeek,basicDay'
    },
    <!--Default Date-->
    defaultDate: '2018-01-01',
    editable: true,
    <!--Event Section-->
    eventLimit: true, // allow "more" link when too many events
    events: [
    {
    title: "<?php echo 'ok';?>",
    start: '2018-01-01'
    },
    {
    title: 'Long Event',
    start: '2015-02-07',
    end: '2015-02-10'
    },
    {
    id: 999,
    title: 'Repeating Event',
    start: '2015-02-09T16:00:00'
    },
    {
    id: 999,
    title: 'Repeating Event',
    start: '2015-02-16T16:00:00'
    },
    {
    title: 'Conference',
    start: '2015-02-11',
    end: '2015-02-13'
    },
    {
    title: 'Meeting',
    start: '2015-02-12T10:30:00',
    end: '2015-02-12T12:30:00'
    },
    {
    title: 'Lunch',
    start: '2015-02-12T12:00:00'
    },
    {
    title: 'Meeting',
    start: '2015-02-12T14:30:00'
    },
    {
    title: 'Happy Hour',
    start: '2015-02-12T17:30:00'
    },
    {
    title: 'Dinner',
    start: '2015-02-12T20:00:00'
    },
    {
    title: 'Birthday Party',
    start: '2015-02-13T07:00:00'
    },
    {
    title: 'Click for Google',
    url: 'http://google.com/',
    start: '2015-02-28'
    },
<?php echo str_replace(['[',']'], '', $jsob); ?>
    ]
  });
});
</script>

Similar Topics