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

share|improve this question

68% accept rate
You're assigning one "object" to event in that javascript loop rather than an index into the array. – Lucas Holt Sep 6 at 19:52
1  
event.push({title: json_obj_birthday[i].title, start: json_obj_birthday[i].start}); And JSON.stringify(event) for converting to JSON – MartyIX Sep 6 at 19:53
event isn't an array. – Ignacio Vazquez-Abrams Sep 6 at 19:53
@IgnacioVazquez-Abrams event = []; If not if Multidimensional array ? – DaHaKa Sep 6 at 19:56
1  
That's nice. Look at what you're doing in the loop. – Ignacio Vazquez-Abrams Sep 6 at 19:57
feedback

4 Answers

up vote 0 down vote accepted

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.

share|improve this answer
-1 This has exactly the same problem as the original. – Eric Sep 6 at 19:58
@Eric I did not tackle his full question. I tackled a problem you brilliant people failed to notice. Now please, give me some time while I work up my magic, ok? – Christian Sep 6 at 19:59
I'd assumed that $birthday_array was initialized. Does PHP really let you append to an variable not initialized to an array!? – Eric Sep 6 at 20:01
@Eric Yes, it does. And no, it wasn't initialized, otherwise there wouldn't be any use for isset(), as you and he did. Quite ironic, isn't it? ;) – Christian Sep 6 at 20:01
@Eric Proof of concept: codepad.viper-7.com/Kb40Ph – Christian Sep 6 at 20:08
show 6 more comments
feedback
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

share|improve this answer
feedback

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
    });
}
share|improve this answer
feedback

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.

share|improve this answer
Understand why doesn't work , but I've tried your example too, it doesn't work as well – DaHaKa Sep 6 at 20:01
@DaHaKa: Doesn't work in what way? I renamed "event" to "events", since that seemed to have tricked you into your previous error. You might want to check you don't refer to it as event elsewhere. – Eric Sep 6 at 20:02
Updated question :) – DaHaKa Sep 6 at 20:06
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.