0

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.

2
  • You don't select DateTime in your SQL Query. How are you getting that value? Commented May 25, 2013 at 4:07
  • 1
    Learn to use a debugger. 95% of your questions can be answered that way. (And stop using the deprecated mysql_ functions.) Commented May 25, 2013 at 4:17

2 Answers 2

6

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
?>
Sign up to request clarification or add additional context in comments.

Comments

0

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

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.