简体   繁体   中英

json encoded php variable into javascript

I'm retrieving data from MySql with php like this =>

while ($birthday_row = $birthday_r->fetch_row()){
    $birthday_array[] = array(
    'title' => $birthday_row[0],
    'start' => $birthday_row[1] . "-" . $birthday_row[2]
    );
}

JavaScript:

var json_obj_birthday = <?php if (isset($birthday_array)){echo json_encode($birthday_array);} ?>;
var json_obj_birthday_len = json_obj_birthday.length;
var d = new Date();
var event = [];
for (var i=0;i<json_obj_birthday_len;i++){ // adding manually year
    json_obj_birthday[i].start = d.getFullYear() + "-" + json_obj_birthday[i].start;
    event = {
        'title' : json_obj_birthday[i].title,
        'start' : json_obj_birthday[i].start
    };
}

In above JavaScript code into event array is stored only for first data , it only stores for json_obj_birthday[0] , how can I store all information into event array ? thanks

PS. I want that array event be json encoded with full information

UPDATE

now I've tried like this

var events = <?php if (isset($birthday_array)){echo json_encode($birthday_array);} ?>;
        var d = new Date();
        for (var i = 0; i < events.length; i++){
            events[i].start = d.getFullYear() + "-" + events[i].start;
        }

and doesn't get desire result as well

Second Update

I need what I'm trying to do because want to pass this event variable into jQuery full calendar, here is script =>

jQuery("#calendar").fullCalendar({ // initialize full calendar
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicWeek,basicDay'
        },
        events: [
            event
        ] // here was my fault , I must to have `events: event,`
    });

After tying your code guys , information into calendar doesn't inserted , That's what I mean doesn't work

PS. If someone interested more deeply what I am doing and why , pls take a look at my previous question too add birthday events into jQuery full calendar each year

Why not assign the json directly to events , then correct the start entries?

var events = <?php echo isset($birthday_array) ? json_encode($birthday_array) : "[]" ?>;
var d = new Date();
for (var i = 0; i < events.length; i++){
    events[i].start = d.getFullYear() + "-" + events[i].start;
}

Note that I added a clause to the PHP so that if $birthday_array is not set, javascript recieves an empty array, rather than a syntax error.


Your code doesn't work because the line event = { ... } replaces the array stored in event with a single javascript object.

PHP:

while ($birthday_row = $birthday_r->fetch_row()){
    $birthday_array[] = array(
    'title' => $birthday_row[0],
    'start' => $birthday_row[1] . "-" . $birthday_row[2]
    );
}

JavaScript:

var json_obj_birthday = <?php if (isset($birthday_array)){echo json_encode($birthday_array);} ?>;
var json_obj_birthday_len = json_obj_birthday.length;
var d = new Date();
var event = [];
for (var i=0;i<json_obj_birthday_len;i++){ // adding manually year
    json_obj_birthday[i].start = d.getFullYear() + "-" + json_obj_birthday[i].start;
    event.push({
        'title' : json_obj_birthday[i].title,
        'start' : json_obj_birthday[i].start
    });
}
var json_obj_birthday = <?php if (isset($birthday_array)){echo json_encode($birthday_array);} ?>;
var json_obj_birthday_len = json_obj_birthday.length;
var d = new Date();
var event = [];
for (var i=0;i<json_obj_birthday_len;i++){ // adding manually year
    json_obj_birthday[i].start = d.getFullYear() + "-" + json_obj_birthday[i].start;
  var  event2 = {
        'title' : json_obj_birthday[i].title,
        'start' : json_obj_birthday[i].start
    };
event.push(event2);
}

try this in javascript

I think your whole approach here is wrong. Here's how I would do it (additionally, it won't show up warnings/notices):

<?php

    $birthday_array = array();

    while ($birthday_row = $birthday_r->fetch_row()){
        $birthday_array[] = array(
            'title' => $birthday_row[0],
            'start' => $birthday_row[1] . "-" . $birthday_row[2],
        );
    }

?><script type="text/javascript">

    var json_obj_birthday = <?php echo json_encode($birthday_array); ?>;
    var json_obj_birthday_len = json_obj_birthday.length;
    var d = new Date();
    var event = [];

    for (var i=0; i < json_obj_birthday_len; i++){ // adding manually year
        json_obj_birthday[i].start = d.getFullYear() + "-" + json_obj_birthday[i].start;
        event.push({
            'title' : json_obj_birthday[i].title,
            'start' : json_obj_birthday[i].start
        });
    }

</script>

My advice here is; always declare variables ... never just append/increment/etc to imaginary variables.

Finally, while there's nothing wrong with offloading some processing to client-side javascript, you might want to consider putting it inside the PHP / server-side. Just a small note, not really critical.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM