这是在这里建立一个先前的问题: 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 div的列和行中。因此,例如详细信息最终将在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);
我想过返回数组而不是echo,但是这不会只返回一行吗?
所以当我在index.php页面上用列表($the data here) = getEvents($weekNumStart, $weekNumEnd);
选择它时,我只会将第一个事件发回去?
答案 0 :(得分: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 :]
希望这有帮助。如果没有别的,请扫描它以寻找我想要的整体想法。祝你好运!