Auto Generate numbers

loudy

Member
Hello,

I would like to create a simple table, that has a field:
Offline Student ID# [Auto generate with 4 digits number like: 0001]

Any tips. thanks,
 
Do you want to store this as a string in the DB?
Or just display?
What do you mean with "autogenerate"? (Autogenerate could be a totally random number)
 
Hi Troester,
Thank you for great support.

What I'm looking to do is a simple form that will has:

Student ID: 4 didits #
First Name:
Last Name:
Email Address:

So on New Record:
Student ID: 0001
First Name: John
Last Name: Sam
Email Address: John.sam@yahoo.com

On save, email will be sent out to the administrator and to the email address in the form with the new Student ID #.

(Autogenerate could be a totally random number) = a Sequential Number such as: 0001, 0002, 0003 ... etc.

Please help.
 
You'd have to write some custom code in a form PHP plugin, running 'onAfterProcess', which creates your ID from the rowid (primary key element) value, and manually updates the row. It can't be done during normal form processing, as the PK (rowid) value isn't created until the row is saved to the database, as its the database that assigns the auto-incrementing PK.

-- hugh
 
:(
I thought I can do that with the calculation to generate the number or just a simple way to do that.
 
Is that possible to change the auto ID that created when create a list to 4 digits?
when creating a list: ID and Date auto generated.
So I'm thinking to use that id and change it to 4 digits and use it as student ID?
 
You can't change internalID format...

I think you can't do that without some php code. You can also use sql trigger.
 
As I said, the only way within Fabrik that works for your requirements is a form plugin. Can't use a calc, because the PK value won't exist during the submission phase when the calc's are run.

So a PHP form submission plugin, running "onAfterProcess", something like ...

Code:
$rowid = $formModel->formData['rowid'];
if (!empty($rowid)) {
   $myId = sprintf('%04f', $rowid);
   $myDb = JFactory::GetDbo();
   $myQuery = $myDb->getQuery(true);
   $myQuery->update('yourtable')-.>set('yourid = ' . $db->quote($myId))->where('id = ' . $myDb->quote($rowid));
   $myDb->setQuery($myQuery);
   $myDb->execute();
}

Change the table and field names in the query to suit.

-- hugh
 
Thank you Hugh,

Just want to be in the right track:
so I have added php plugin in the forms section, and I selected onAfterProcess
here is my full table:
 
Thank you Hugh,

Just want to be in the right track:
so I have added php plugin in the forms section, and I selected onAfterProcess
here is my full table:
table.PNG

and here is my complete code:

$rowid = $formModel->formData['mtoofflinestudent___offlinenumber'];
if (!empty($rowid)) {
$myId = sprintf('%04f', $rowid);
$myDb = JFactory::GetDbo();
$myQuery = $myDb->getQuery(true);
$myQuery->update('mtoofflinestudent')-.>set('mtoofflinestudent___offlinenumber = ' . $db->quote($myId))->where('id = ' . $myDb->quote($rowid));
$myDb->setQuery($myQuery);
$myDb->execute();
}


Am I in the right track, please. not sure which fields that need to be changed.

Thank you so much.
 
In the first line, put rowid back.

In the set(), remove the table name prefix, just offlinenumber.

Sent from my HTC One using Tapatalk
 
Hi, thank you again for your kind help,
I have followed you steps, but the number is still not generated in the [offlinenumber] filed;
empty.png

any tip :)
 
I assume there's an additional .
$myQuery->update('mtoofflinestudent')-.>set('mtoofflinestudent___offlinenumber = ' . $db->quote($myId))->where('id = ' . $myDb->quote($rowid));

Do you J! error reporting enabled?
 
Hi, here is my setting for the form php:

Not sure still not add the number to the student id filed.

setting.png
 
Can you copy/paste the code (the screenshot is hard to read)?
But it seems you still have the ., remove it.
ent')-.>set
 
here is my complete code:
---------------------------------------------------------

$rowid = $formModel->formData['rowid'];
if (!empty($rowid)) {
$myId = sprintf('%04f', $rowid);
$myDb = JFactory::GetDbo();
$myQuery = $myDb->getQuery(true);
$myQuery->update('mtoofflinestudent')->set('offlinenumber = ' . $db->quote($myId))->where('id = ' . $myDb->quote($rowid));
$myDb->setQuery($myQuery);
$myDb->execute();
}

---------------------------------------------------------

I have removed the . from :
now I got:

Whoops\Exception\ErrorException thrown with message "Call to a member function quote() on null"

Stacktrace:
#0 Whoops\Exception\ErrorException in /folders/plugins/fabrik_form/php/php.php:453
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top