- Chained selects with dynamic content
- Posted by Andy Jacobs on January 11th, 2006
Hi all,
I've written a little system so that my Missus can do invoices and keep
records of customers, etc, using PHP and MySQL.
I have a table with customers and a table with contacts so that one customer
can have several contacts. When you create an invoice, there are two drop
downs - one listing all the customers and one listing all the contacts -
with the information being taken from the 2 MySQL tables.
Each contact is associated with a customer so choosing a contact would
automatically select the correct customer but she won't necessarily know
that John Smith is from XYZ ltd so it needs to have both.
What I want to do is use chained selects so that choosing a customer will
populate the contacts drop down with only those contacts from that customer.
I've done some googling but maybe I'm not searching for the right thing as I
can't seem to find what I need.
Has anyone done this who could point me at some examples or something?
Cheers
Andy
- Posted by Geoff Berrow on January 11th, 2006
Message-ID: <BFEA8918.A1CE%andy@redcatmedia.net> from Andy Jacobs
contained the following:
Unless I'm missing something...
Presumably you have your customer ID as a foreign key in the contacts
table.
So say you have a customer ID stored in $customer_id
$sql= "SELECT * from contacts WHERE contacts.customer_id =
$customer_id";
Run the query and loop through the results to populate the drop down.
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
- Posted by Jon Ribbens on January 11th, 2006
In article <BFEA8918.A1CE%andy@redcatmedia.net>, Andy Jacobs wrote:
If you want that to happen dynamically on the one page (i.e. not
having it so the user selects the company and then clicks 'Next'
or something to go to another page) then you will have to rely on
JavaScript. If, as it sounds, this application is for internal use
by a single person then that's presumably fine.
Depending on how large your database is and how large you think it
might become, you could either simply make the code which is
downloaded with the page list every single company and contact in the
database, or if that would be too large you'll need to look into
XMLHttpRequest/Msxml2.XMLHTTP/Microsoft.XMLHTTP to dynamically request
contact lists to update the drop-downs.
Either way you want to look at the Document Object Model (DOM) for how
to dynamically update the document contents in the browser:
http://www.w3.org/DOM/DOMTR
- Posted by Andy Jacobs on January 12th, 2006
On 11/1/06 5:02 pm, in article slrndsaeh3.njs.jon+usenet@snowy.squish.net,
"Jon Ribbens" <jon+usenet@unequivocal.co.uk> wrote:
Yep on a single user and yep on Javascript. Found exactly what I needed on
the Zend site in one of their tutorials:
http://www.zend.com/zend/tut/drop-down.php
Took a little while to get straight as there are a couple of bugs in their
code but it works perfectly for what I need.
Andy


