Welcome, Guest
Username: Password: Remember me

TOPIC: Create an Auto-Increment Field in LimeSurvey

Create an Auto-Increment Field in LimeSurvey 1 year 9 months ago #87032

  • LSCitizen
  • LSCitizen's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 33
  • Karma: 0
Hello -

I'm creating a form that tracks events. The ID# will begin with a previous ID# (i.e.: 2012123) so the next ID# should be 2012124. Is there a way to do this in LimeSurvey?
The administrator has disabled public write access.

Re: Create an Auto-Increment Field in LimeSurvey 1 year 9 months ago #87060

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4066
  • Thank you received: 745
  • Karma: 341
Every case is given a sequential ID in the data. It can be accessed with the {SAVEDID} placeholder after the first page has been submitted.
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Create an Auto-Increment Field in LimeSurvey 1 year 9 months ago #87175

  • misterdot
  • misterdot's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 35
  • Thank you received: 1
  • Karma: 0
Is there any way to pad that number? I was looking at str_pad in Expression Manager, but that's not working...I assume b/c the response ID is an integer. In my case, I'm trying to capture the initials, location, and entry number for the person doing data entry. So questions would be:

Q1: what are your initials? MM
Q2: what's your location? New York (NY)
Q3: your entry code for this response is MM-NY005.

...where the sequence would be 001 - 900. All works except the padding. My latest attempt looks like this:

your entry code for this response is {INSERTANS:Q1}-{INSERTANS:Q2.value}{SAVEDID}.
It gives MM-NY5.
Thanks,
Matt
The administrator has disabled public write access.

Re: Create an Auto-Increment Field in LimeSurvey 1 year 9 months ago #87219

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4066
  • Thank you received: 745
  • Karma: 341
SAVEDID is only available if the survey is activated and at least one page has been submitted.

The following should work if the above is true:
Your entry code for this response is - {Q1.shown}-{Q2.code}-{SAVEDID}
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 1 year 9 months ago by tpartner.
The administrator has disabled public write access.

Re: Create an Auto-Increment Field in LimeSurvey 1 year 9 months ago #87226

  • misterdot
  • misterdot's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 35
  • Thank you received: 1
  • Karma: 0
Thanks, Tony. That does work, but I was wondering if there was a way to pad it with leading 0s. It gives me MM-NY5 instead of MM-NY005.
The administrator has disabled public write access.

Re: Create an Auto-Increment Field in LimeSurvey 1 year 9 months ago #87227

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4066
  • Thank you received: 745
  • Karma: 341
I don't know whether EM can do that.

You may need to put the SAVEDID in a <span> element and then use JavaScript to pad it.

Or, if you just need to store the value, use JavaScript directly to access the SAVEDID and pad it.
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Create an Auto-Increment Field in LimeSurvey 1 year 9 months ago #87236

  • misterdot
  • misterdot's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 35
  • Thank you received: 1
  • Karma: 0
Below is what I ended up doing. I'm a javascript novice, so I'm sure there's a more efficient way of doing it, but this works - question type is short free text:
Entry ID:
<script type="text/javascript" charset="utf-8"> 
	$(document).ready(function() { 
 
        function pad(number, length) {
          var str = '' + number;
          while (str.length < length) {
          str = '0' + str;
          }
 
        return str;
        }
 
var resp_id = pad({SAVEDID}, 3);
var upper_init = ('{INSERTANS:44474X131X1973}'.toUpperCase());
var answer1 = (upper_init + "-B" + resp_id);
 
document.getElementById('answer44474X121X1977').value = answer1;
});
 
</script>
The output ends up being XX-BNNN were XX is initials entered in a previous question and NNN is the 0 padded response ID. Javascript pad snippet comes from www.electrictoolbox.com/pad-number-zeroes-javascript/.
Thanks,
Matt
The administrator has disabled public write access.

Aw: Create an Auto-Increment Field in LimeSurvey 1 year 8 months ago #88881

  • mfs
  • mfs's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
Why not simply use
Your entry code for this response is - {Q1.shown}-{Q2.code}-{sprintf("%03d",SAVEDID)}

or alternatively
Your entry code for this response is - {sprintf("%s-%s-%03d",Q1.shown,Q2.code,SAVEDID)}
The administrator has disabled public write access.

Re: Aw: Create an Auto-Increment Field in LimeSurvey 1 year 8 months ago #88886

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5300
  • Thank you received: 291
  • Karma: 247
mfs has given some very useful hint. Simply format the string using the above function.

More information: www.php.net/manual/en/function.sprintf.php

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.271 seconds
Donation Image