Hi There -

This is something I can sorta figure out in regular PHP but understanding how to approach this with Drupal has me stumped. I've been thinking about node forms and hook form alter and really have hit a wall.

I need a form that does the following:

Metal            cost/oz            oz            cost ($)
steel             .0348           ______           0
iron              .0572              3             0.1716
copper            .0283           ______           0
tin               .0027             18             0.486
______            _____           ______           0     <----------------------- customer can write in their own   
_________________________________________________________                         values and add another 
TOTAL                                             $0.6576                         line if they want 
                                                                                  up to infinity...

- The customer will fill in under the oz column, and upon save the costs in the right hand column will update and a total will show up.
- When someone views after the form is saved, they need to see just the list of what got ordered, not the entire list of a couple hundred metals, the cost for each, and the total. This can be in version two.
- The customer is allowed to edit the list and it should update with new costs and totals.
...

I'm not really sure how to approach this with the form api as I know it. Basically, it's vertical and this is horizontal and I'm not wrapping my mind around how to get it to fit in this instance. The brief is to copy the functionality of an existing Excel spreadsheet that works with formulas for the cost and totals field, using Drupal.

How would you approach this problem? ANY pointing in the right direction would be gratefully accepted.

Thanks!

Comments

zbricoleur’s picture

I would start by installing the matrix field module (http://drupal.org/project/matrix).

Once I had the form matrix set up, I would use jQuery to update the cost cells on the fly. That way the customer could see what they were ordering without having to submit first. (Of course, you'd want to be sure to check the numbers again once they hit submit, to make sure they hadn't tinkered with the amounts. I use jQuery to make the updated field inactive on focus, which is enough to dissuade most people, but better to validate it after submission anyway.)

joecanti’s picture

hi newbstah,

Maybe if this was an ordering system you could see the oz as units of quantity and have an ecommerce system like ubercart set up - so when someone wants to buy 5oz of steel they by 5 x 1oz of steel? You could probably theme this quite nicely in Ubercart by adding extra product options.

Your 'form' would then essentially become your shopping cart which they could update and change as required, and use to only view products which they have added.

There is another thought I had - there is a module - http://drupal.org/project/computed_field - which is a CCK field which adds up/multiplies other fields together. You could have one CCK field called unit price, and another CCK field of type number where people put in how much they want. The you could use computed field to calculate the final price and output it in a new field. This may then give you something more concrete to theme around to get it looking as you would like, and I think it can work on the fly (ajax or something) but not sure about that.

Joe

goofus’s picture

Hi,
Isn't this just a normal form that might be formatted with an html table? You described standard form processing.

When the form is initialized you go to the database and get your display values (e.g. metals and price per ounce). You build the form, each metal has a corresponding text field for the number of ounces.

The user clicks submit. You calculate the cost values, store the results in the database and then display the results in an html table.

The default element for the Drupal FAPI is a plain html element. You can use the prefix and suffix attributes to build the html rows and columns.

The process you described is standard. You don't need any special spreadsheet controls. You said the calculation occurs on submission. That's standard form processing.

jaypan’s picture

Goofus told you how to take care of the processing. As for the visual layout of the form, you run the form through a theme function, which you will define in hook_theme(). Then you can output the display in an HTML table.

Search for 'theming tables' (or 'themeing tables') and see what you find. There are threads out there on the matter.

Contact me to contract me for D7 -> D10/11 migrations.