json中每日标题栏的标题

时间:2013-01-22 09:58:33

标签: php javascript jquery mysql json

抱歉笨拙的问题, 我不太确定它是否可能,我用json正确地从Mysql db中读取数据 并将它们显示为非表格的元素 在数据库中我有一些约会日期,将每周显示给客户组,并有下一个链接到其他一周。 有一些代码,我很抱歉。 这是HTML代码:

<div class="timesviewport">
    <div class="selector">
        <div class="title-collection" style="">
           <!--time add here-->
        </div>
        <div class="selector-header">
            <div class="header-title-collection" style="">
              <!--date add here--> 
            </div>
            <div class="control">
                <div class="prevlink">
                   <div class="nextprevloading"></div>
                       <a class="prevweeklink" href="#" rel="nofollow">before</a>
                </div>
                <div class="nextlink">
                   <div class="nextprevloading"></div>
                       <a class="nextweeklink" href="#" rel="nofollow" data-test="search-next-week-link">next</a>
                </div>
            </div>
        </div>                                    
    </div>
</div>

这里是javascript

function getresDetails(id)
{
    var new_res_date = '' ;
    var new_res_time = '' ;

    new_res_time += '<div class="title"><div class="title-row-collection">';

    daily = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
    j=0;

    var id_val = id.value;
    var url = "table.php?id="+id_val;

    $.getJSON(url, function(json) {
        new_res_date +='<div class="header-title"><div class="header-title-date-collection" id="adddate">';
        new_res_time += '<div class="title-row"><div class="title-row-date-collection">' ;

        $.each(json, function(i, value) {
            if(i==0)
            {
                new_res_date += '<div class="header-title-date"><div class="header-title-date-name">'+daily[j%7]+ '</div><div class="header-title-date-value">'+json[i].res_date+ '</div></div>';
                new_res_time +=  '<div class="title-row-date"><div class="appointment-collection"><div class="appointment-collection-first"><div><a class="appointment-time" href="#">'+json[i].res_time+'</a></div>' ;
                //alert("first :" +i);
                j++;
            }
            else if (i>0 && json[i-1].res_date==json[i].res_date)
            {
                new_res_time += '<div><a class="appointment-time" href="#">'+json[i].res_time+ '</a></div>';
                //alert("second :" +i);
            }
            else
            {
                new_res_date +=  '<div class="header-title-date"><div class="header-title-date-name">'+daily[j%7]+ '</div><div class="header-title-date-value">'+json[i].res_date+ '</div></div>';
                new_res_time +=  '</div></div></div><div class="title-row-date"><div class="appointment-collection"><div class="appointment-collection-first"><div><a class="appointment-time" href="#">'+json[i].res_time+ '</a></div>';
                //alert("third :" +i);
                j++;
            }
        });
        j=0;
        new_res_date += '</div></div>' ;
        new_res_time += '</div></div></div></div></div></div></div>' ;

        $(new_res_time).appendTo(".title-row-collection");
        $(new_res_date).appendTo(".header-title-collection");
    });
}

和从数据库读取数据的其他页面在这里

table.php

<?php

include 'configure.php';

$doc_id = $_GET["id"];

$qr = "SELECT `res_date`,`res_time` FROM `reserve_tbl` WHERE `doc_id` = '".$doc_id."'";

$res= mysql_query($qr);


$i=0;

while($row = mysql_fetch_array($res))
{
    $res_arr[$i]["res_date"] = $row["res_date"]; 
    $res_arr[$i]["res_time"] = $row["res_time"];
    $i++;
}

header('Content-type: application/json');
echo json_encode($res_arr);
?>

profinf.php

<?php

include 'configure.php';

$doc_id = $_GET["id"];

$qr = "SELECT * FROM `doc_tbl`  WHERE `doc_id`  = '".$doc_id."'";

$res= mysql_query($qr)  or die("Error: " . mysql_error() . "<br />In Query: " . $qr);;
$i=0;
while($row = mysql_fetch_array($res))
{
    $doc_arr[$i]["doc_name"] = $row["doc_name"];
    $doc_arr[$i]["doc_family"] = $row["doc_family"];
    $doc_arr[$i]["doc_grade"] = $row["doc_grade"];
    $doc_arr[$i]["doc_specialy"]= $row["doc_specialy"];
    $doc_arr[$i]["doc_adress"] = $row["doc_adress"];
    $i++;
}
header('Content-type: application/json');
echo json_encode($doc_arr);
?>

最后这里是连接configure.php

<?php
$cfg_server = "localhost";
$cfg_database   = "db_name";
$cfg_username   = "root";
$cfg_password   = "";

$conn = mysql_connect($cfg_server, $cfg_username, $cfg_password);
if($conn)
{
    if (!mysql_select_db($cfg_database))
    {
        die('Could not select Database: '.mysql_error());
    }
}
if (!$conn)
{
    die('Could not connect to Database: '.mysql_error());
}
?>

以前有人有同样的问题吗?我需要按每周元素显示日期列组,而不是表,如果我使用表更容易?但我不知道如何在表格中显示列周列组!我可以通过SQL查询按数据分组吗?

感谢。

1 个答案:

答案 0 :(得分:1)

mySql有一个周功能。

如果您将查询更改为: -

SELECT `res_date`,`res_time`, WEEK(res_date) AS `res_week` FROM `reserve_tbl` WHERE `doc_id` = '".$doc_id."'";

您可以使用res_week字段按周编号跟踪每条记录,从而显示上周和下周