我目前有两个下拉菜单和一个 div。我的目标是能够使用<option>
在 div 内的第二个下拉列表中选择打印的变量。如图所示,我试图用一个数组来做到这一点。
这是我到目前为止所拥有的:
$items = array();
// converting day into the date of the next time that day occurs
function getDateString($day) {
if (date("l") == $day) {
return date("D j M");
}
else {
$date = strtotime("next $day");
return date("D j M",$date);
}
}
// populating the second dropdown
function getClubs() {
connect();
date_default_timezone_set('Europe/London');
$day = date("l");
$time = date("G");
if ($time >= 21) {
$day = date('l', strtotime($day .' +1 day'));
}
if ($day == Monday) {
$query = "SELECT * FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')";
}
else if ($day == Tuesday) {
$query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY')";
}
else if ($day == Wednesday) {
$query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY')";
}
else if ($day == Thursday) {
$query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY')";
}
else if ($day == Friday) {
$query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY')";
}
else if ($day == Saturday) {
$query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY')";
}
else if ($day == Sunday) {
$query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY')";
}
$result = mysql_query($query);
$items = array();
if($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
// $items[] = array($row['name']);
$name = $row['name'];
$day = $row['day'];
// add these two to an array
array_push($items, $name, $day);
echo "<option>" . $name . " (" . getDateString($day) . ")</option>";
}
}
}
// populating div
function printDetails() {
// echo $name (as an example) from the array above
echo $items[0];
}