如何使用ajax/json从数据库检索信息? (jQuery周日历) [英] How to retrieve info from database using ajax/json? (jQuery Week Calendar)

查看:85
本文介绍了如何使用ajax/json从数据库检索信息? (jQuery周日历)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改以下 jQuery日历模块以进行集成它与PHP和MySQL数据库一起使用.我遇到的问题是代码会像这样加载事件:

I'm trying to adapt the following jQuery calendar module to integrate it with PHP and a MySQL database. The problem I'm having is that the code loads events as such:

  return {
     events : [
        {
           "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"
        }
     ]
  };

我想将其更改为一个AJAX查询,该查询从数据库中检索事件.但是我不知道到底该怎么做.到目前为止,我曾想过使用JSON从单独的调用数据的PHP页面获取信息,但是如何处理数据以返回适当的对象呢?

I want to change this to an AJAX query that retrieves the events from the database. But I can't figure out exactly how to do this. So far I thought of using JSON to get the information from a separate PHP page that calls the data, but how can I manipulate the data to return a proper object?

到目前为止,我有这样的事情:

So far I have something like this:

  $.ajax({
        type: 'POST',
        url: 'test.php',
        dataType: 'json',
        cache: false,
        success: function(result) {
            //how can I copy the results to be able to return them?
        }
    });

然后在test.php中,我有类似的东西

And then in test.php I have something like

    $sql = $db->query("SELECT * FROM calendar;");

    $results = array();
    while($row = mysql_fetch_assoc($sql))
    {
       $results[] = array(
          'id' => $row['id'],
          'start' => $row['start'],
          'end' => $row['end'],
          'title' => $row['title']
       );
    }
    echo json_encode($results);

请记住,我还必须通过新的Date()传递开始和结束值.

Keep in mind I also have to pass the start and end values through new Date().

谢谢您的时间.

推荐答案

如果您的php页面运行正常,则应返回事件数组.因此,可变结果将是该数组.然后,您可以创建结构{events:result}并将其传递到您的日历.

if your php page is working properly, it should return an array of events. So the variable result would be that array. You can then create the structure {events: result} and pass it to your calendar.

success(result){
    var myData = {events: result};//now you have your data in correct format.
}

这篇关于如何使用ajax/json从数据库检索信息? (jQuery周日历)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆