robmonie / jquery-week-calendar

Now actively maintained in the following fork - https://github.com/themouette/jquery-week-calendar
388 stars 388 forks source link

Events from database #29

Open giorgio83 opened 13 years ago

giorgio83 commented 13 years ago

Hi I am starting with jquery and I want to use this calendar but carrying my events from a database, for this demo I put the calendar on a page and then I edited the file by modifying the method getEventData demo.js where I have added a ajax function calling a php file that queries the database and bring the events, the data I see that brings me but from here and do not know how to assign my variable response below to return there instead of Sample loading events that bring mine loaded database. can someone help? this is my code:

function getEventData() { var year = new Date().getFullYear(); var month = new Date().getMonth(); var day = new Date().getDate(); $.ajax({ url: 'consulta.php', type: 'POST', success: function(respuesta){ alert(respuesta); } });

  return {
     events : [
       {
           "id":121,
           "start": new Date(2011, month, 14, 12),
           "end": new Date(year, month, 14, 14, 30),
           "title":"jorgito"
        },
        {
           "id":1,
           "start": new Date(year, month, day, 12),
           "end": new Date(year, month, day, 13, 30),
           "title":"Lunch with Mike"
        },
        {
           "id":2,
           "start": new Date(year, month, day, 14),
           "end": new Date(year, month, day, 14, 45),
           "title":"Dev Meeting"
        },
        {
           "id":3,
           "start": new Date(year, month, day + 1, 17),
           "end": new Date(year, month, day + 1, 17, 45),
           "title":"Hair cut"
        },
        {
           "id":4,
           "start": new Date(year, month, day - 1, 8),
           "end": new Date(year, month, day - 1, 9, 30),
           "title":"Team breakfast"
        },
        {
           "id":5,
           "start": new Date(year, month, day + 1, 14),
           "end": new Date(year, month, day + 1, 15),
           "title":"Product showcase"
        },
        {
           "id":6,
           "start": new Date(year, month, day, 10),
           "end": new Date(year, month, day, 11),
           "title":"I'm read-only",
           readOnly : true
        }

     ]
  };

}

webmaster666 commented 13 years ago

its in your calendar.js

data: function(start, end, callback) { $.getJSON("event.php", { start: start.getTime(), end: end.getTime() }, function(result) { callback(result); }); }


event.php:

include("db.php"); $srg = "SELECT * FROM calendar WHERE calendar.start >= ? AND calendar.end <= ?";

$snc = $dbh->prepare($srg);

$snc->execute(array(substr(date("c", substr($_GET["start"], 0, -3)), 0, 10), substr(date("c", substr($_GET["end"], 0, -3)), 0, 10)));

$first = true; $id = 1; $sData= "[";

foreach ($snc->fetchAll() as $str) { if ($first) $first = false; else $sData.= ','; $sData.= '{ "id":' . $id++ . ', "start": "' . $str["start"] . '", "end": "' . $str["end"] . '", "title":"' . $str["title"] . '", "body":"' . $str["body"] . '" }'; } $snc->closeCursor();

echo $sData. "]";

i hope will u help

jmr74 commented 13 years ago

HI webmaster666 how is the db.php coded, can u show?