| | |
Populate dropdown using mysql date range
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
Hello everyone, I'm having trouble populating a dropdown box with mysql data. I have a table with columns makeid, yearstart, yearend and makename. Sample data would be 1, 1936, 1941, American Bantam. I want the dropdown to list the years a certain brand of car was offered. For instance, a user selects American Bantam and the years 1936, 1937... 1940, 1941 are listed. I've tried a bunch of different things but I can't seem to get the dropdown to populate with anything more than just the start or end dates. I'm relatively new to PHP so I'm sure there is a simple solution.
Any help would be appreciated!
Thanks,
Arthur
Any help would be appreciated!
Thanks,
Arthur
•
•
•
•
For instance, a user selects American Bantam and the years 1936, 1937... 1940, 1941 are listed.
try it...
or post appropriate code...then we can see it for validate....
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
Well, I've actually got two pieces of code. The first one I'll post was given to me by someone else but I've managed to get it to work for my application. Here it is:
The next code is a simple drop down I've been working on. I've tried a lot of different things with it but to no avail.
Thanks,
Arthur
PHP Syntax (Toggle Plain Text)
<?php require "includes/dbconnect.php"; $query = ""; if(isset($_GET['make'])) { if(trim($query2) == "") { $query2 .= " where makename = '" . mysql_real_escape_string(stripslashes($_GET['make'])) . "'"; } else { $query2 .= " and makename = '" . mysql_real_escape_string(stripslashes($_GET['make'])) . "'"; } } $query2 = "select * from makes" . $query2 . " order by makename"; $searchresult = mysql_query($query2) or die(mysql_error());; $query3 = "SELECT yearstart, yearend FROM makes"; $yearresult = mysql_query($query3) or die(mysql_error());; $yeararray = array(); while($row = mysql_fetch_assoc($yearresult)) { $yeararray[$row["yearstart"]][] = $row["yearend"]; } ?> <table cellpadding="0" cellspacing="0"> <? if(mysql_num_rows($searchresult) == 0 || !is_numeric(mysql_num_rows($searchresult))) { ?> <tr> <td colspan="3">No vehicles found under that search criteria.</td> </tr> <? } else { for($i = 0; $i < mysql_num_rows($searchresult); $i++) { ?> <tr> <td><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?make=<? echo urlencode(mysql_result($searchresult, $i, "makename")); ?>"><? echo mysql_result($searchresult, $i, "makename"); ?></a></td> <td> <form action="<? echo basename($_SERVER['PHP_SELF']); ?>" method="get"> <input type="hidden" name="make" value="<? echo mysql_result($searchresult, $i, "makename"); ?>" /> <select name="year" id="year<? echo mysql_result($yearresult, $i, "yearstart"); ?>"> <option value="Choose">Choose</option> <? foreach($yeararray[mysql_result($yearresult, $i, "yearstart")] as $value) { ?> <option value="<? echo $value; ?>"><? echo $value; ?></option> <? } ?> </select>
The next code is a simple drop down I've been working on. I've tried a lot of different things with it but to no avail.
PHP Syntax (Toggle Plain Text)
<?php $query3 = "SELECT yearstart, yearend FROM makes"; $result = mysql_query($query3) or die(mysql_error());; $options=""; while ($row=mysql_fetch_array($result)) { $yearstart=$row["yearstart"]; $yearend=$row["yearend"]; $options.="<OPTION VALUE=\"$yearstart\">".$yearstart.$yearend; /// concatenates your option tags with the value and option at that returned row } ?>
Thanks,
Arthur
•
•
Join Date: May 2008
Posts: 6
Reputation:
Solved Threads: 0
Possible modification for Choice Box
<?php
$query3 = "SELECT yearstart, yearend FROM makes";
$result = mysql_query($query3) or die(mysql_error());;
$options="";
while ($row=mysql_fetch_array($result)) {
$yearstart=$row["yearstart"];
$yearend=$row["yearend"];
for ( $i=$yearstart;$i<=$yearend;$i++) {
$options.="<OPTION VALUE=\"$i\">". $i . "</OPTION>";
}
}
?>
<?php
$query3 = "SELECT yearstart, yearend FROM makes";
$result = mysql_query($query3) or die(mysql_error());;
$options="";
while ($row=mysql_fetch_array($result)) {
$yearstart=$row["yearstart"];
$yearend=$row["yearend"];
for ( $i=$yearstart;$i<=$yearend;$i++) {
$options.="<OPTION VALUE=\"$i\">". $i . "</OPTION>";
}
}
?>
![]() |
Other Threads in the PHP Forum
| Thread Tools | Search this Thread |
apache api array basic beginner binary broken cache cakephp checkbox class cms code computing confirm cron curl customizableitems database date delete display dynamic echo email error external file files filter folder form forms forum function functions gc_maxlifetime google headmethod host howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction memmory memory menu mlm multiple mysql navigation oop parsing paypal pdf php phpmysql problem query question radio random recursion remote script search select server sessions sms snippet source space sql syntax system table thesishelp trouble tutorial update upload url validator variable video web youtube







