从其他地方的while循环打印MySQL数据

时间:2012-06-11 14:00:33

标签: php mysql while-loop

我目前有两个下拉菜单和一个div。我的目标是能够使用在div内第二个下拉列表的选定<option>中打印的变量。如图所示,我试图用数组做到这一点。

这是我到目前为止所做的:

$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];
}

1 个答案:

答案 0 :(得分:0)

变化:

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

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

然后在你的内心

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

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

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

例如:

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

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