If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

How to show column B value based on column A value

Hi,

I need to create a form, with 2 columns (A & B). By default, column A will show all vendors in a drop-down box and only upon user selection, then the list of products that belong to that vendor will appear in column B drop-down box for user to select.
- Column A contains vendor's name (eg. Vendor1, Vendor2, Vendor3 etc)
- Column B contains product name for each vendor.

Of course there are many (and frankly, better) ways to do it, but this is a way that is similar to yours. As for the hideAllFields function, don't you dare do the same mistake again and write the hiding command out for every vendor. An easy way would be assigning a css class to every div and then find all divs with that class and hide them. Alternatively, wrap all these divs into a parent div and then hide all direct descendants. Whatever you do, just stay flexible – no hardcoding!

As for the other thing: If you really have a lot of vendors, your PHP/MySQL code is, sorry, terribly slow. The simple answer is to create the divs for each vendor in a loop – this will at least save you having to write out the code for every vendor. However, your code will still be slow because you're still querying your database way more often than you have to.

The better way is to improve your SQL statements. The two most important ones being:
1) Do NOT use the * selector. Select only the fields you actually need.
2) Do NOT query the database again and again for every vendor. Select all products at once, sorting them by the vendor and then display the result accordingly. A hint: The sorting can already be done in MySQL.

Keep in mind that the above assumes that vendor names are unique. If they are not, you need to use an abstraction layer and assign unique IDs to each vendor with which you can work then.

I have tried re-writing the codes, but I have no idea how to code inside the showfield part. Not really sure if I am going on the right track

By default, product field will not be shown and when user click a vendor name, only all the products belong to that vendor will be shown.

Can someone please kindly advise? Thanks.

Below are the database design and the codes.

Database:
I created 1 database with 2 tables:
1) Table named "vendor" with fields - id (primary key, auto-increment) and vendorname
2) Table named "product" with fields - id (primary key, auto-increment), productname, vendorid (this will be the foreign key that link to the id in vendor table to identify the vendor that the product belongs to)