0

我目前有两个下拉菜单和一个 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];
}
4

1 回答 1

0

改变:

echo "<option>" . $name . " (" . getDateString($day) . ")</option>";

$dropdown .= "<option>" . $name . " (" . getDateString($day) . ")</option>";

然后在你的下

<select><?=$dropdown;?></select>

您正在按照正在打印的 HTML 的顺序在代码中回显选项标签。意思是,您在选择标签之外呼应它。通过使用 .= 您将 $dropdown 变量与循环中的其余选项连接起来。

在一天结束时,您的下拉选项将存在于 $dropdown 中,您可以在页面上的任何位置使用它,无论您放置它。

前任:

<div><?=$dropdown;?></div>
<select><?=$dropdown;?></select>

但请注意每个项目周围的标签。

于 2012-06-11T14:01:50.740 回答