Show one Dropdown that's populated from from_year and to_year fields in mysql

Hi, I’m not familiar with php language etc yet so apologies if there is a better way of asking this but…I am putting together a set of chained dropdowns (the second set of options is determined by what is selected in the first dropdown). My website is about motorbikes and the user will select make, model, year etc which will then display the required page.

My data has the year range information split between two fields “from_year” and “to_year”
Multiple rows could have a common year in the range covered by from_year/to_year fields.
I want the website user to be able to input the year of their motorbike and get php/Mysql to check the ranges covered by from_year / to_year

Is this possible?

Thanks

Yes! EVERYTHING is possible in programming… If you spend enough time to work it all out…

Now, on multiple drop-down’s based on previous selections, there are basically two or three ways to handle this.
One is to population all of the possibilities and use Javascript to show the user just the ones that are selected.
Another is to have the selection make the page post the results and use PHP to load the second or third option
to show the user just the ones that are needed. A third way would be to use JQuery and AJAX to load the next
drop-down with the correct data based on a selection. All of these will work. Some are harder to program than
others.

The easiest for a beginner is to force a post (form submit) when the user changes a selection. The posted
values of all of the drop-downs are read in the PHP code and used to load the next drop-down data in the
sequence. Since forms are very easy to use and easy to explain, this is easy to process.

The other two are tricky. The first one is to load all possible combinations and show them as selected, but, this
is hard to work out the logic. The last way is quite easy if you understand how to use JQuery and AJAX. This
version would dynamically load the new values of drop-downs when previous ones change. It is not really that
hard to learn, but, not needed if you use the more standard form submit method.

So, you have a lot to think about. If you decide on one way or another, we can help you with it… Good luck!

Thanks for the reply, I think I’ll give the easy option a try first as it’s not just php I’m new to!

I can’t seem to find any information about how to populate the values into the drop down box for the year, might be because I’m not using the correct phrases and terms in my search.

First off, I’m trying to understand the process for the year dropdown.

So, assuming my previous selections return the following rows to be used to populate the year dropdown. Each row has from_year and to_year fields and the values are

2000/2006
1989/1999
2008/2015

So, the range covers years from 1989 to 2015 but misses out on 2007.

The dropdown presented to the website user should show all years in the range 1989, 1990, 1991…2006, 2008… 2015

I’d appreciate if you could let me know if php functions cover this or even just the logic to use.

Thanks

Well, it is very easy to load up a clause with values. Here is an example of how to do it.
It is just loosely written to show you an example. You will also need to make the drop-down (SELECT) cause
a submit of the form. I will show you how to do that too. (It is the Javascript inside the drop-down that does
this for you. Hope this helps…

[php]

<?php $dbconn = mysqli_connect('hostname', 'username', 'password', 'database-name'); $sql = "SELECT * FROM tablename"; $result = mysqli_query($dbconn, $sql); echo ""; echo "Select something here..."; while ($row = mysqli_fetch_array($dbconn, $result)) { echo "" . $row['field_date'] . ""; } echo ""; ?>

[/php]
Notes: See in the clause that I added the on-change option so that it posts the form. This would be
used the first drop-down that you want to use the changed value to reload the second one with. The second
one would not need this as you would want the user to press the submit button instead. And, as you see, you
will need to load the correct field names, table names, etc where needed. Like the filed name for your dates.
You would need to do the above for each drop down that you want to make others to be altered. Let’s say
name the first like this: , you would need to load the value of it
and test if they had selected one. To do that, you use a line that reads the value of the drop-down and then
use it in the second drop down to read data from the database based on it. Loosely like this one:
$first_drop_down=$_POST[“first_drop_down”];
If the value is empty, then the user did not select a value and your code should handle it as needed. If they
selected a value, then you would load the second drop-down based on that value using the “WHERE” clause in
the query.

Well, that should help you get started… As a newbie, you might want to read up on all of these functions. You
can review just about any command you might need in the PHP.net site, or even better use the link below.
You can select the language you want to read about at the top, such as HTML, CSS, PHP and then select a
subject under each on the left such as database, functions, etc… Hope this helps…
http://www.w3schools.com/php/php_intro.asp
(Just press the green Next-Chapter button when you are in a tutorial…)

And, when you get stuck, ask us and someone here will help you. Remember to place your code inside the
PHP tags which helps us a lot…

Dear Bruthas N Sistas, let us bow our heads in a moment of silence :-X as we vow to stop echo’ing HTML.

Allow me…

[php]<?php
$dbconn = mysqli_connect(‘hostname’, ‘username’, ‘password’, ‘database-name’);
$sql = “SELECT * FROM tablename”;
$result = mysqli_query($dbconn, $sql);
?>

Select something here... <?php while ($row = mysqli_fetch_array($dbconn, $result)) { echo "{$row['field_date']}"; } ?> [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service