0

这是建立在上一个问题的基础上的: https ://stackoverflow.com/questions/15534272/interpreting-the-day-of-a-datetime-and-putting-it-into-a-relevant-div

本质上我有这张表:

Events Table
EventID | Start               | End                 |  Details
1       | 2013-03-18 09:00:00 | 2013-03-18 10:00:00 |  Event is in room 6
2       | 2013-03-18 10:00:00 | 2013-03-18 11:00:00 |  Event is in room 3

和这个专栏:

<div class="timetable-column-first">
    <div class="timetable-row-start">Time</div>
    <div class="timetable-row">9:00am</div>
    <div class="timetable-row">10:00am</div>
</div>

<div class="timetable-column">
    <div class="timetable-row-start">Monday</div>
    <div class="timetable-row" id="mon-9"></div>
    <div class="timetable-row" id="mon-10"></div>
</div>

我已经构建了这个 php 查询来获取我需要的信息:

function getEvents($weekNumStart, $weekNumEnd){
    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    if (!$mysqli) {
        die('There was a problem connecting to the database.');
    }
    else {
        if ($weekNumEnd == '') {
            $weekNumEnd = $weekNumStart;
        }
        $query = $mysqli->prepare("SELECT EventID, DAYOFWEEK(Start) AS wday, Events.Start, HOUR(Start) AS sHour, HOUR(End) AS eHour, Events.End, Details, StartName FROM Events, Week WHERE StartName >= '$weekNumStart' AND StartName <= '$weekNumEnd' AND Events.Start >= StartWeek AND Events.Start <=EndWeek;");
        $query->execute();
        $query->bind_result($eventID, $dayofweek, $startDateTime, $startHour, $endHour, $endDateTime, $weekName);
        while ($query->fetch()){
            echo $eventID, $dayofweek, $startDateTime, $startHour, $endHour, $endDateTime, $weekName;
        }
    }
    $mysqli->close();
}

问题是这个查询是在附加的 php 文件 /functions.php 中完成的,该文件包含在标题中。

我不知道如何更改此行:

echo $eventID, $dayofweek, $startDateTime, $startHour, $endHour, $endDateTime, $weekName;

这样它要么将每个数组返回到原始 index.php,然后我就可以使用 jquery 将它插入到我的 div id 的列和行中。因此,例如,细节在 div 中会像这样结束:

<div class="timetable-column">
    <div class="timetable-row-start">Monday</div>
    <div class="timetable-row" id="mon-9">1 - Event is in room 6</div>
    <div class="timetable-row" id="mon-10">2 - Event is in room 3</div>
</div>

就目前而言,我的功能有效,从服务器回显所有单独的数据。

在 index.php 页面上,我只是这样称呼它:

getEvents($weekNumStart, $weekNumEnd);

我考虑过返回数组而不是回显,但这不会只返回很多行吗?因此,当我在带有列表的 index.php 页面上选择它时,($the data here) = getEvents($weekNumStart, $weekNumEnd);我只会将第一个事件发回?

4

1 回答 1

1

我做这样的事情的典型模型是:

function getEvents($weekNumStart, $weekNumEnd){
    // I'm moving the SQL up here just so it's out of the way for a minute;
    $sql_str = "SELECT EventID, DAYOFWEEK(Start) AS wday, Events.Start, HOUR(Start) AS sHour, HOUR(End) AS eHour, Events.End, Details, StartName FROM Events, Week WHERE StartName >= '$weekNumStart' AND StartName <= '$weekNumEnd' AND Events.Start >= StartWeek AND Events.Start <=EndWeek";


    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    if (!$mysqli) {
        die('There was a problem connecting to the database.');
    }
    else {
        if ($weekNumEnd == '') {
            $weekNumEnd = $weekNumStart;
        }
        $query = $mysqli->prepare($sql_str); // I moved the string above just for clarity
        $query->execute();
        $query->bind_result($eventID, $dayofweek, $startDateTime, $startHour, $endHour, $endDateTime, $weekName);

        // LINE ADDED:
        $data_arr = array();
        while ($query->fetch()){
            // I'm creating an associative array to sort each result by day of
            // the week
            $data_arr[$dayofweek] = array(
                $eventID, $dayofweek, $startDateTime, 
                $startHour, $endHour, $endDateTime, $weekName
            );
            /* Example of what the object would look like with your events in it.
            ['Monday' => [[
                   1, "Monday", "2013-03-18 09:00:00", 
                   "09", "10", "2013-03-18 10:00:00",
                   "Event is in room 6"
            ], [
                   2, "Monday", "2013-03-18 10:00:00", 
                   "10", "11", "2013-03-18 11:00:00",
                   "Event is in room 3"
            ]]];
             */
        }
        $mysqli->close(); // a little redundant, but meh
        return $data_arr; // returns each of your results as an array within an array
    }
    $mysqli->close();

}

然后从您调用 getEvents() 函数的任何地方,执行以下操作:

$data = getEvents($weekNumStart, $weekNumEnd);

$html = '';
$firstCol = '';
$secondCol = '';

// for each day in the data array:
foreach($data as $day) {
    $firstCol .= '<div class="timetable-column-first">'
           . '<div class="timetable-row-start">Time</div>';
    $secondCol .= '<div class="timetable-column">'
           . '<div class="timetable-row-start">' . $day . '</div>';

    $timeStartRow = '';
    $detailsRow = '';
    for($i = 0; $i < count($data[$day]); $i++) {
        $startHour = $data[$day][$i][3];
        $details = $data[$day][$i][6];

        // build the content rows within the first column
        $timeStartRow .= '<div class="timetable-row">' 
                      . $startHour                         // append each start hour
                      . '</div>';

        // build the content rows within the second column
        $detailsRow .= '<div class="timetable-row" id="' 
                    . $day . '-' . $starHour . '">'        // create the id
                    . $details                             // deats!
                    . '</div>';
    }
    $timeStartRow .= '</div>'; // close off .timetable-column-first
    $detailsRow .= '</div>'; // close off .timetable-column

    $html .= $firstCol . $timeStartRow . $secondCol . $detailsRow;
    $firstCol = '';
    $secondCol = '';
} 

echo $html; // I can't guarantee it will work, but it'll be 90% there :]

希望这有帮助。如果不出意外,请扫描它以了解我想要的整体想法。祝你好运!

于 2013-03-20T23:55:26.660 回答