本文介绍了在PHP中从SQL数据库填充DropDown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从SQL数据库获取数据,以填充几个下拉列表。这是节选,但如果您愿意,我可以发表更多。我没有包含所有内容,因为它多了几行。

I'm attempting to get data from a SQL database in order to populate a couple drop downs. This is an excerpt, but I can post more if you'd like. I didn't include it all because its more than a couple lines.

$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>";

由于某种原因,无论我尝试什么,下拉列表都保持空白。

For some reason, the drop down remains empty no matter what I try.

推荐答案

为什么要使用如此复杂的代码。使用PHP自身与HTML集成的功能。

Why are you using such a complex code. Use the power of php of integrating itself with 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
?>

这篇关于在PHP中从SQL数据库填充DropDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 22:31