Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

How to display comment, title and filename of uploaded files

  • baxterdmutt
  • baxterdmutt's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 week 5 days ago #269528 by baxterdmutt
LimeSurvey version: LimeSurvey Community Edition   Version 6.4.0+231218
Own server
==================
I have a survey where up to 6 files can be uploaded.
I know that I can display everything about the uploads with Q1.NAOK. But it's a long messy string.
How can list all of them in a nicer way. 1) Title, Comment, Filename sort of idea. I tried Q1_comment.NAOK but that's not right.  I didn't see how to do this in the manual.  
Thanks

Please Log in to join the conversation.

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
1 week 5 days ago #269530 by DenisChenu
There are no simple ay currently …

Need to be done via javascript. Else plugin can create new function : maybe create a feature request ?
community.limesurvey.org/feature-request/

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 week 5 days ago #269532 by Joffm
Hi,
this string is well structured.
Therefore you can capture your desired parts by ExpressionScript.
Use functions "substr"and "strpos".

 I'll provide an example later.

Joffm 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 week 4 days ago #269533 by Joffm
Here the example:
Let's say we upload this image in Q1
 

Now the string {Q1} looks like
 

With these functions you can display the desired parts
You see, the structure is always the same.
  • Find the start by the keyword
  • Find the end by the next keyword
  • Display the substring from the start with a length of  (end-start)
Everything a bit adapted because of different lengths of keywords.

Title:
{trim(substr(Q1, strpos(Q1,"title")+8,(strpos(Q1,"comment")-3-(strpos(Q1,"title")+8))))}

Comment:
{trim(substr(Q1, strpos(Q1,"comment")+10,(strpos(Q1,"size")-3-(strpos(Q1,"comment")+10))))}

Filename:
{trim(substr(Q1, strpos(Q1,"name")+7,(strpos(Q1,"filename")-3-(strpos(Q1,"name")+7))))}

 


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

  • baxterdmutt
  • baxterdmutt's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 week 4 days ago #269534 by baxterdmutt
OK I see how that works, but that will do one of the 6 possible uploads. I'm not the best with scripting so sorry, but how can I loop through all 6 possible uploads.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 week 4 days ago - 1 week 4 days ago #269535 by Joffm
You see that the function "strpos" has a third (optional) parameter "offset".
Use it to start the "second round" at the end of the first image, e.g. by using the closing curly bracket as a "end sign", and so on for the next.




As this will result in rather lengthy expressions, you may consider to split the original string in a question of type "multiple short text" "C" and then handle the subquestions separately.
Something like this
Equation to split the string and fill the subquestions
Code:
{C_1=substr(Q1,strpos(Q1,"title"))}
{C_2=substr(C_1,strpos(C_1,"title",10))}
{C_3=substr(C_2,strpos(C_2,"title",10))}
...
 
Then use the equations for each subquestion
 

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 week 4 days ago by Joffm.
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

  • baxterdmutt
  • baxterdmutt's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 week 4 days ago #269536 by baxterdmutt
Sorry, I don't understand where offset=1 (or whatever) will go.
I tried {trim(substr(Q1, strpos(Q1,"comment",1)+10,(strpos(Q1,"size")-3-(strpos(Q1,"comment",1)+10))))}
It seems like that's where the PHP manual suggests I put it but it doesn't changes anything. Sorry, I'll need an example. I don't need the whole 6 written for me. I just need an example of where the offset number would go, please.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 week 3 days ago #269540 by Joffm
Hi,
to get familiar with the function "strpos(haystack, needle, offset)" you may read this
[url] www.php.net/manual/en/function.strpos.php [/url]

Now, let's say there is this content of {Q1}
[{ "title":"Title 1","comment":"Comment 1","size":11.111,"name":"name1.PNG","filename":"fu_12345678","ext":"png" }, { "title":"Title 2","comment":"Comment 2","size":22.222,"name":"name2.PNG","filename":"fu_22345678","ext":"png" }, { "title":"Title 3","comment":"Comment 3","size":33.333,"name":"name3.PNG","filename":"fu_32345678","ext":"png" }]

you get the title of the first file by:
{trim(substr(Q0, strpos(Q0,"title")+8,(strpos(Q0,"comment")-3-(strpos(Q0,"title")+8))))}

To get the title of the second file you have to make sure that "strpos" starts after the first "title". Here the offset appears.
To cover all desired values (title, comment, name) it is suitable to start the search for the second file at the end of the first.
And as all these strings have different lengths you can't use a fixed value for offset.
Therefore I used the position of "ext". So the offset is 'strpos(Q0,"ext")'.
And the equation to get the second title:
{trim(substr(Q0, strpos(Q0,"title", strpos(Q0,"ext"))+8,(strpos(Q0,"comment", strpos(Q0,"ext"))-3-(strpos(Q0,"title", strpos(Q0,"ext"))+8))))}

Now it becomes easy. To get the title of the third file, we create the position of the second "ext" as offset.
How?
You get this position when you search for "ext" with an offset greater than the position of the first "ext", like
strpos(Q0,"ext",strpos(Q0,"ext")+5)
Find the position of "ext", but start five characters after the position of the first "ext".
{trim(substr(Q0, strpos(Q0,"title", strpos(Q0,"ext", strpos(Q0,"ext")+5))+8,(strpos(Q0,"comment", strpos(Q0,"ext", strpos(Q0,"ext")+5))-3-(strpos(Q0,"title", strpos(Q0,"ext", strpos(Q0,"ext")+5))+8))))}

 
The next analogue.
I said before it is straightforward, but lengthy.
Therefore I showed the other solution where you split the string first

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • baxterdmutt
  • baxterdmutt's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 week 3 days ago - 1 week 3 days ago #269542 by baxterdmutt
OK the strpos makes sense now. Thanks. But now I'm doing something else wrong. I have created a multiple short answer text question(with 6 spots). I set the default answers as the equations you gave as a working example. (with my question codes in place) the equations are valid, but the answers are always blank. Am I putting the equations in the wrong place. Where should they go?

Well I see why I couldn't get the results to display. I needed to put the Multi-Text in the next group.
Last edit: 1 week 3 days ago by baxterdmutt.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose