Robert Ingrum
Robert Ingrum

Reputation: 234

Populating a DropDown from SQL database in PHP

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.

Upvotes: 0

Views: 6908

Answers (2)

sAnS
sAnS

Reputation: 1163

You may try like this

<?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>';

?>

Upvotes: 0

Veer Shrivastav
Veer Shrivastav

Reputation: 5496

Why are you using such a complex code. Use the power of php of integrating itself with HTML.

Try this Style.

And check if you have established a connection with the database or not.

   <?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
?>

Upvotes: 6

Related Questions