Order number in form

shaq

New Member
Hi,

I have two lists : employeurs list and salaries list. Each list has several elements.
I made a form using a combined two lists

Now I need to add a ready-only element to employeur list to do an unique order number everytime a new form is created (like counter).
The numerotation will be like 20170000001 for the first form created, and 20170000002 for the second form, etc....

I can edit and modify the form without changing the numerotation number.

How I can do. Little help please.

Thanks.
 
Several questions.

1) Does the order number need to be visible on the form when it is first created. In other words, does it need to be calculated when the form is rendered, or can it be done when the form is submitted.

2) Does it have to start at 1. The best way to do this is usually to use the PK (primary key) as the "number" part, so "2017" + the PK value padded with 0's. But that might mean it doesn't start at 1 (depending on your existing table data). And of course it can't be displayed when first creating the form, as the PK won't exist until after the form is submitted and the data written out to the database.

Using the PK value is by far the preferred method, as it handles all the potential issues with duplicate numbering if you try and code your own solution. That's what PK's are - sequential, unique numbers identifying rows in a table. Managing your own system of unique, sequential numbers is re-inventing the wheel.

If using the PK value is OK, then you'll need a form submission script (a PHP form plugin) running 'onAfterProcess', which does this:

Code:
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery->update('employeur')
   ->set('order_number = ' . $myDb->quote(sprintf('%s%08d', date('Y'), $formModel->formData['rowid']))
   ->where('id = ' . $myDb->quote($formModel->formData['rowid']));
$myDb->setQuery($myQuery);
$myDb->execute();

Modify the table and field names to suit.

Of course, if you want to start the number part over for each new year, that raises a whole new level of complexity.

-- hugh
 
Last edited:
Thanks for your answer;

1/ The order number can be created at form submission
2/ Lets say that we need the number to start like 20171 (so forget about 0's)
3/ what kind of element do I need to create (field, calc...etc) in employeur list or no need to create an element
 
Create a field element. Set the ACL for 'form view' to Super Admins (or whatever). Call it whatever you want, but change the query I gave you to use the same name (I used 'order_number' in my example).

If you don't want leading zero's, just change the sprintf to

sprintf('%s%d', ... etc ...)

eg. remove the 08 from the format string.

-- hugh
 
Well, I guess if you don't want to do padding, you don't need the sprintf at all, just do ...

Code:
->set('order_number = ' . $myDb->quote(date('Y') . $formModel->formData['rowid']))

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top