Create an Auto-Increment Field in LimeSurvey

More
4 years 5 months ago #87032 by LSCitizen
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?

Please Log in to join the conversation.

More
4 years 5 months ago #87060 by tpartner
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.

Please Log in to join the conversation.

More
4 years 5 months ago #87175 by misterdot
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

Please Log in to join the conversation.

More
4 years 5 months ago - 4 years 5 months ago #87219 by tpartner
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.
Last Edit: 4 years 5 months ago by tpartner.

Please Log in to join the conversation.

More
4 years 5 months ago #87226 by misterdot
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.

Please Log in to join the conversation.

More
4 years 5 months ago #87227 by tpartner
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.

Please Log in to join the conversation.

More
4 years 5 months ago #87236 by misterdot
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

Please Log in to join the conversation.

More
4 years 4 months ago #88881 by mfs01
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)}

Please Log in to join the conversation.

More
4 years 4 months ago #88886 by Mazi
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)survey-consulting.com'"

Please Log in to join the conversation.