|
Posted by frothpoker on 09/01/06 12:40
PHP is not really the tool to do this with. Because PHP is a server
side scripting tool and is used to return HTML code, you can only
really do one list at a time.
Eg.
Lets say you want to drop down firstly a list of Car Makes (Ford,
Jaguar, Ferrari) and then from the selection made show a second list
that shows all the models for only the selected manufacturer.
you would request a list of makes from the database "Select * from
Makes" and build the html so that it looks like
<option value=ford>Ford</option>
<option value=jaguar>Jaguar</option>
<option value=ferrari>Ferrari</option>
In order to build the second list, you would need to do another trip to
the server "select * from Models where Make = 'ford' " and build
another list of option values.
To do this without returning to the server would require you to build
an array of all models "Select * from models" and then run Javascript
client side to do an inline replace of the options list. This is fine
when you have limited records, but what do you do if you for example,
wanted to list every variant of every model of every manufacturer.
That would be a huge array and the delivery time would be awful because
all that data needs to be sent to the web page in the array so that the
javascript can sort it out.
The two best ways I have found to do this are:-
run a query that selects all records from top table
for each row, run a second query that selects all sub records
append each to a single drop down list but set the value of top records
to be an error trap and the sub records indented somehow.
you would get an option list that looks like:
<option value=invalid>Ford</option>
<option value=mondeo>---Mondeo</option>
<option value=galaxy>---Galaxy</option>
<option value=maverick>Maveric</option>
<option value=invalid>Jaguar</option>
<option value=etype>---Etype</option>
<option value=xkr>---XKR</option>
<option value=invalid>Ferrari</option>
<option value=612>---612 Scalati</option>
<option value=550>---550 Maranelo</option>
<option value=360b>---360 Barchetta</option>
This way, if the user selects a manufacturer rather than a model, the
validation script will throw an error.
this is suitable for limited lists and limited levels. If you look at
Ebay, this is how they manage their catagories in drop down lists
the other way is to have self referencing pages with a hidden value in
the submitted form that indicates how far through the process you are.
The PHP server then delivers a different piece of HTML depending on
which part of the page you are at.
You will find lots of examples of self referring pages if you look
Obiron
cpptutor2000@yahoo.com wrote:
> Could some PHP guru please help me? I looking for a good working
> example of chained dropdown list. Any help would be greatly
> appreciated.
Navigation:
[Reply to this message]
|