Retrieving events from mysql database

1,326 views
Skip to first unread message

Gixer

unread,
Oct 24, 2010, 2:33:58 PM10/24/10
to jquery week calendar
Hi,

I'm no web developer and know the basics of programming so maybe
jumped in at the deep end. I've been stuck with this for a while now
and read so many googled pages that I'm confused.

I'm intending to use the excellent weekcalendar as a rota and have
multiple users added to it. I've used the theme roller and message
display as per the demo's available.

My events are being written to a mysql database.

The structure of what is saved;

id = auto increments
title
body
start
end
editable
userId

Date and time is formatted so mysql accepts it and looks like;
2010-10-20 09:30:00.

I see that the calendar 'data' function is called before the calendar
is rendered on screen.

My data function looks like this;

data: function(start, end, callback) {
$.getJSON("http://localhost/app/views/layouts/events/feed.php", {
start: start.getTime(),
end: end.getTime() },
function(result) {
//alert(result[0].start);
callback(result);
});
}
});

The alert shows what is stored in the array when enabled.

My feed.php page looks like this;

....
$query = "SELECT * FROM events";

$result = mysql_query($query) or die(mysql_error());

$events = array();
while ($row = mysql_fetch_assoc($result)) {
$eventArray['id'] = $row['id'];
$eventArray['title'] = $row['title'];
$eventArray['body'] = $row['body'];
$eventArray['start'] = $row['start'];
$eventArray['end'] = $row['end'];
$eventArray['userId'] = $row['userID'];
$eventArray['editable'] = $row['editable'];
$events[] = $eventArray;
}
echo json_encode($events);
?>

Firebug shows the json;

[{"id":"27","title":"Off Desk","body":"ssssswsdw","start":"2010-10-20
09:30:00","end":"2010-10-20 16:00:00","userId":"3","editable":"1"}]

Validated okay at http://www.jsonlint.com/

Only nothing shows on my calendar. I thought it would be the date
format but the event hard coded is fine. Am I missing something in
the callback?

If you can get me going on the right track it is much appreciated.
I've read so much and spent many hours trying to resolve.

Thanks,
Jay


Julien MUETTON

unread,
Oct 24, 2010, 3:18:54 PM10/24/10
to jquery-wee...@googlegroups.com
Hi Jay,

I guess this might come from your calendar configuration, get a closer look to 
  • your "users" array
  • your "getEventUserId" callback
  • your "getUserId" callback
In fact, to have the event displayed it is mandatory that the getEventUserId and the getUserId returns the same value (same type as the comparison is === in the $.inArray function).

to have a better understanding of the process, have a look to _findWeekDayForEvent internal.

let me know if it solve your issue or not.

Julien.

Gixer

unread,
Oct 24, 2010, 4:05:07 PM10/24/10
to jquery week calendar
Hi Julien,

Thank you so much for the prompt reply.

I haven't made use of getEventUserId and getUserId so will
investigate. I've just captured calEvent.userId and put that into
mysql and then returned it in json.

Thanks again,
Jay

Julien MUETTON

unread,
Oct 24, 2010, 4:11:24 PM10/24/10
to jquery-wee...@googlegroups.com
So, the only problem should be the int / string conversion.

maybe if you convert your SQL userId field into an integer that would be ok.
be careful with the default implementation, the id used for your user is the index in the users array, so when you change it it will put your events on different users.

Cheers,

Julien.

Gixer

unread,
Oct 30, 2010, 12:44:50 PM10/30/10
to jquery week calendar
Thanks again Julien.

You were right I need to review ;

- "getEventUserId" callback
- "getUserId" callback

When I remove the users array I can display an event on the page so
it's something to do with this.

Best regards,
Jay


Gixer

unread,
Oct 30, 2010, 8:05:51 PM10/30/10
to jquery week calendar
I'm struggling with multiple users and not finding examples of it
being used other than demo 3.

Default code:-

getUserId : function(user, index, calendar) {
return index;
},

// This returns the full array on each day?

getUserName : function(user, index, calendar) {
//alert(user);
return user;
},

//Again this returns the full array on each day?

getEventUserId : function(calEvent, calendar) {
alert(calEvent.userId);
return calEvent.userId;
},

//This returns the correct UserId for the event stored in the database
and output json.

setEventUserId : function(userId, calEvent, calendar) {
calEvent.userId = userId;
return calEvent;
},

So getEventUserId and getUserId do not return the same values as
getUserId repeats the array. I put getUserId in my json output with
the same value as getEventUserId but this did not appear as an
event.

With the user array commented out the event displays. I'm obviously
misunderstanding and missing the procedure to display multiple user
events.

Gixer

unread,
Oct 31, 2010, 11:20:38 AM10/31/10
to jquery week calendar
okay fixed it after re-reading Julien's response :-)

demo 3 has an event like this;

[{"id":5, "start": new Date(year, month, day+1, 14), "end": new
Date(year, month, day+1, 15, 00), "title": "Product showcase", userId:
0}]

My output takes this form;

[{"id":"27","title":"Off Desk","body":"ssssswsdw","start":"2010-10-20
09:30:00","end":"2010-10-20 16:00:00","userId":"3","editable":"1"}]

Note that my userId value is inside double quotes. If I put the demo
3 userId inside double quotes the event does not display either.

Change my code to not use this and it displays just fine.

Gixer

unread,
Oct 31, 2010, 11:51:59 AM10/31/10
to jquery week calendar
Should anyone else be struggling with the same...

MySQL userId field was set to integer but ended up as a string in the
json output.

So I had to parse the data.

New data function;

data: function(start, end, callback) {
$.getJSON("http://localhost/app/views/layouts/events/
feed.php", {
start: start.getTime(),
end: end.getTime() },
function(result) {
if (result != null)
{
for (i in result) {
var calEvent = result[i];
calEvent.userId = parseInt(calEvent.userId);
}
}
var calevents = result;
callback(calevents);
});
}


Mahiboobu M

unread,
Apr 5, 2013, 6:59:29 AM4/5/13
to jquery-wee...@googlegroups.com
Dear Sir,
 I am Storing Events in database but i am unable to retrieve the events from database, i dont know how to push to events:[{}] i tried so many times and so many ways but still i didn't get it, below is my code go through, please do needfulll.


  
 function getEventData() {
          var year = new Date().getFullYear();
          var month = new Date().getMonth();
          var day = new Date().getDate();
          var result=[];
          var sss=[];
          alert(new Date(year, month, day, 12));
          /* $.getJSON("AjaxRequest.do?method=getSaveEventsData", function (data) {
              alert(data);
                $.each(data, function (index, ddddd) {
                    $.each(ddddd, function (key, value) {
                       alert(value);
                       alert(value[0]);
                       alert(value["id"]+" jjjjaaaa");
                    });
                });
            });
             */
            $.ajax( {
                   type : "POST",
                   url : "AjaxRequest.do?method=getSaveEventsData",
                   data: "{}",
                   contentType: "application/json; charset=utf-8",
                dataType: "json",
                   success : function(result) {
                       var myData = {events: result};
                       alert(myData+"myData");
                   alert(result+" wwwww");
                var receivedData = [];
                $.each(result.jsonArray, function(index) {
                    alert(index+" asasdasasas");
                    $.each(result.jsonArray[index], function(key, value) {
                        var point = [];
                            point.push(value);
                            receivedData.push(point);
                            alert(receivedData+" receivedData");
                            alert(point+" point");
                            getKumar(receivedData);
                        });
                   
                });
                alert(receivedData+" receivedData aaaaa");
             //   sss.push(result);
              //  getKumar(result);
              //  alert(sss+" wwwwwtttttttttttttt");
               // alert(arr2+" iiiiiiiiii +getEventData");
        },
      
        error: function (x, e) {
            alert("The call to the server side failed.");
        }
            
                    });
         
          alert(result +"fddfdfdfdf");
          var arrrr=[{
              "id":1,
             "start": new Date(year, month, day, 12),
             "end": new Date(year, month, day, 13, 30),
             "title":"Lunch with Mike"
     }
     ];
          alert(arr2 +"fddfdfdfdf what erfe");
          alert(arrrr+" events ddd");
          return {
             events : arr2
          };
       }  

Thanks in advance,
Mehaboob.
Reply all
Reply to author
Forward
0 new messages