-1

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

4
  • You're assigning one "object" to event in that javascript loop rather than an index into the array. Commented Sep 6, 2012 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 Commented Sep 6, 2012 at 19:53
  • @IgnacioVazquez-Abrams event = []; If not if Multidimensional array ? Commented Sep 6, 2012 at 19:56
  • 1
    That's nice. Look at what you're doing in the loop. Commented Sep 6, 2012 at 19:57

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Understand why doesn't work , but I've tried your example too, it doesn't work as well
@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.
1

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
    });
}

Comments

0

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.

8 Comments

-1 This has exactly the same problem as the original.
@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?
I'd assumed that $birthday_array was initialized. Does PHP really let you append to an variable not initialized to an array!?
@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? ;)
@DaHaKa "It doesn't work" is a broad statement. What didn't work?
|
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.