*PHP Unit Association* Please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adiosamigos
Forum Newbie
Posts: 8
Joined: Sat Jun 27, 2009 3:52 am

*PHP Unit Association* Please help

Post by adiosamigos »

Hi,

I am creating a website with a form. The form has three fields; ingredients, quantity and unit.

What I would like to achieve:

When the user enters the ingredient, the unit (unit of measurement) will change to something appropriate, for example

The user will begin entering 'jam' and when the form recognises the word (probably when the 'm' is typed" the unit field will refresh and display 'jar(s)'

Perhaps it would be faster to process or easier if the unit field refreshes when the user clicks on the quantity field.

I need this so that when the user submits the data I will have a database of ingredients with universal units of measurement for the quantities, so it's not displayed as:

Jam 12 Teaspoons
Jam 0.5 Jars

I will create a file hosted on the server with all the associated units, I don't yet know which format to use and was thinking mySQL, cvs or txt but I'm unsure which will be best. I'd rather something lightweight like cvs though.

If anyone could point me in the right direction or give me some idea of where to start I would really appreciate it.

Thanks a lot,
Tom :)
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: *PHP Unit Association* Please help

Post by Eric! »

You'll probably need to use javascript and xmlhttprequest. Here is an example using a form and inserting city and state based on zipcode
http://www.webpasties.com/xmlHttpReques ... ial_1.html

You could also use ajax.

For the associations, I would put them in mysql. Easier to sort, search and update, but that's a personal preference.
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: *PHP Unit Association* Please help

Post by juma929 »

Hello,

To get the nice functionality you mentioned such as the units automatically updating depending on the item selected I would suggest AJAX, you can do this in plain Javascript without AJAX but you would need to either code all of the information in the javascript file or hidden in the actual page that is being viewed which can be problematic for updating, preventing others stealing your hard work and future expansion of your system/website.

1. Display Page.
2. User selects item.
3. AJAX calls a PHP page which takes an item and pulls the corresponding unit from any of the data sources you suggest.
4. Javascript updates the page with the new unit.

Personally, I would use MySQL if the list of items is large, large enough to require future sorting/searching and more advanced techniques. CSV is very nice until you start wanting to handle larger amounts of data and perform advanced tasks.

If you need anything else please just let me know, I can even knock you up a basic AJAX system to provide this functionality.

Thanks,
Justin
adiosamigos
Forum Newbie
Posts: 8
Joined: Sat Jun 27, 2009 3:52 am

Re: *PHP Unit Association* Please help

Post by adiosamigos »

Thanks both of you.

Justin, your solution seems to accomplish everything I am trying to achieve although the user does not select the ingredient, as in a drop down menu, but rather types it in a text field. Then when they select/click on the quantity text field the third text field will be populated with the appropriate unit of measurement. After considering the functionality I've now decided it might be better to have a drop down menu for the third column (unit of measurement) so the user can select which unit of measurement they would prefer to use eg grams, ounces, teaspoons, slice or whatever is most appropriate.

Is this still possible to do using ajax?

How easy will this be to write? I appreciate your offer and may consider it although I will try and write it myself first.

Thanks a lot, Tom :)
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: *PHP Unit Association* Please help

Post by juma929 »

Hello,

That's fine in ajax no problem but I wouldn't personally let the users type in the ingredients due to spelling mistakes etc

Thanks,
Justin
adiosamigos
Forum Newbie
Posts: 8
Joined: Sat Jun 27, 2009 3:52 am

Re: *PHP Unit Association* Please help

Post by adiosamigos »

Thanks for the help it's much appreciated.

I will post back in a few weeks when I have wrote it and got it running or sooner if i have any problems :)

Thanks again,
Tom
adiosamigos
Forum Newbie
Posts: 8
Joined: Sat Jun 27, 2009 3:52 am

Re: *PHP Unit Association* Please help

Post by adiosamigos »

Hi, I have looked into it a lot deeper now and have a better understanding of what I need to do.

I was wondering if you would recommend:

A. start by putting some test material in the mySQL database, then writing the PHP, then the javascript/ajax etc

B. start by creating the form with the ajax/javascript, the write the PHP and then do the database.

Thanks again,
Tom :)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: *PHP Unit Association* Please help

Post by califdon »

A.
adiosamigos
Forum Newbie
Posts: 8
Joined: Sat Jun 27, 2009 3:52 am

Re: *PHP Unit Association* Please help

Post by adiosamigos »

Thanks.

Also, does anyone have an idea of how long this would take to load because I don't want the user to have to wait.

Cheers,
Tom
adiosamigos
Forum Newbie
Posts: 8
Joined: Sat Jun 27, 2009 3:52 am

Re: *PHP Unit Association* Please help

Post by adiosamigos »

Hi guys. Finished the engine and it works perfectly.

Thanks, you saved me a lot of time and I've learnt a lot in the execution of your advice.

Thanks again,
Tom :D
Post Reply