| | |
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 |
.htaccess alerts apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database dataentry date display duplicates dynamic echo email emptydisplayvalue error execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include insert ip javasciptvalidation javascript joomla keywords limit link login mail matching menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect remote script search securephp server sessions shot sms source space sql subscription syntax system table tutorial tutorials update upload url validator variable video web youtube







