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

Recommended Answers

All 7 Replies

can you post your code for us to know what you want it would be?

im a newbie too .. perhaps i could help too:)

enim

For instance, a user selects American Bantam and the years 1936, 1937... 1940, 1941 are listed.

If user selects name, then based on that id you can get two dates from your database table, then make a dropdown list by incrementing the first date, put if condition greater than end date...
try it...
or post appropriate code...then we can see it for validate....

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:

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

$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

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>";
}
}
?>

Well, that's kinda working. The dropdown now populates with all the years for all the Makes. I only want it to display the relevent years for each car the user clicks on. Any ideas?

It seems to me that the reason you are getting all the makes for the year range you choossis because you are not explicitly choosing a make in that query. You need to have WHERE makename = 'something' in your SQL query.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.