在PHP中填充SQL数据库中的DropDown

时间:2013-05-25 04:00:50

标签: php sql database arrays

我正在尝试从SQL数据库中获取数据,以便填充几个下拉列表。这是摘录,但如果您愿意,我可以发布更多内容。我没有把它全部包括在内,因为它超过了几行。

$queryData = mysql_query("SELECT DISTINCT DateTime AS DateTime FROM 'historicaldata' ORDER BY YEAR(DateTime), DAYOFYEAR(DateTime)");
$queryGroups = mysql_query("SELECT DISTINCT histgroupname AS GroupName FROM 'historicalgroups' WHERE `histgroupID` < 10 ORDER BY `histgroupname`");

$tracker = 0;
$dataArray = array();
$groupsArray = array();
$DateFormat1 = array();
$DateFormat2 = array();
$DayNumber = array();
$Month = array();
$Year = array();

while ($row = mysql_fetch_array($queryData)) {
    $dataArray[$tracker] = $row['DateTime'];
    $tracker++;
}

$tracker = 0;
while ($row = mysql_fetch_array($queryGroups)) {
    $groupsArray[$tracker] = $row['GroupName'];
    $tracker++;
}

$tracker = 0;
foreach ($dataArray as $l) {
    $p = strtotime($l);
    $x = getdate($p);
    $DateFormat1[$tracker] = date("D M d, Y", $x);
    $DateFormat2[$tracker] = date("M Y", $x);
    $DayNumber[$tracker] = date("z", $x);
    $Month[$tracker] = date("n", $x);
    $Year[$tracker] = date("Y", $x);
    $tracker++;
}

echo "<div id='Period1'> <span class='regblue'>Start</span><select name='startdate'><option value=''></option>";

foreach($DateFormat1 as $x)
    echo "<option selected value='$x'>$x</option>";

echo "</select> </div>";

出于某种原因,无论我尝试什么,下拉都是空的。

2 个答案:

答案 0 :(得分:5)

为什么使用这么复杂的代码?使用PHP的强大功能与HTML集成。

试试这个风格。

并检查您是否与数据库建立了连接。

   <?php

    require_once('connection.php'); //establish the connection with the database on this page.

$queryData = mysql_query("SELECT DISTINCT DateTime AS DateTime FROM 'historicaldata' ORDER BY YEAR(DateTime), DAYOFYEAR(DateTime)");
$queryGroups = mysql_query("SELECT DISTINCT histgroupname AS GroupName FROM 'historicalgroups' WHERE `histgroupID` < 10 ORDER BY `histgroupname`");

$result = mysql_fetch_array(mysql_query($queryData));   //$result now has database tables
$resultGroups = mysql_fetch_array(mysql_query($qrueryGroups)); //$resultGroups has now database tables

?>
<select name='Date'>
<?php
while($row = mysql_fetch_array($result))
{
    ?>
        <option values=<?php echo($row['DateTime']); ?><?php echo($row['DateTime']); ?></option>
    <?php
}
?>
</select>
<?php
?>

答案 1 :(得分:0)

您可以尝试这样

<?php

require_once('db_connect.php'); //connect with the database.

$queryData = mysql_query("SELECT DISTINCT DateTime AS DateTime FROM 'historicaldata' ORDER BY YEAR(DateTime), DAYOFYEAR(DateTime)");
$queryGroups = mysql_query("SELECT DISTINCT histgroupname AS GroupName FROM 'historicalgroups' WHERE `histgroupID` < 10 ORDER BY `histgroupname`");

$result = mysql_fetch_array(mysql_query($queryData));   //$result now has database tables
$resultGroups = mysql_fetch_array(mysql_query($qrueryGroups)); //$resultGroups has now database tables

echo '<select name="Date" id="Date">';

while($row = mysql_fetch_assoc($result))
{

    echo '<option values=' . $row["DateTime"] . '>' . $row["DateTime"] . '</option>';

}
echo '</select>';

?>