Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

How to do this: Add multiple unique randomized hidden fields from desired range

Quote Reply
How to do this: Add multiple unique randomized hidden fields from desired range
What I have done so far:

Created 31 link properties (using Add Column) with following name:
rand1
rand2
rand3
.....
rand31
------------------------


Problem:
When any listing is added,along with all other info submitted by end-users, I also want to add multiple (rand1 to rand31) hidden fields with randomized value.


So for add listing form, along with regular form fields, there will be 31 hidden fields as:



<input type="hidden" name="rand1" value="7">
<input type="hidden" name="rand2" value="25">
<input type="hidden" name="rand3" value="16">
.....
<input type="hidden" name="rand31" value="2">

-------------------------------------
Can this be done using a global or some other way so that every time the add-listing form is added, the order for 'values' in hidden form is randomized.

i.e. for listing 1 that is added, rand1 = 1, & so on till rand31.
while next for next listing that would be added, rand1 = 'would be some other number'.. (between 1 & 31).





Note: in above example 'value' are added for example. Ideally they could be any value between 1 & 31, and they will also be unique, so no to rand$ fields will be duplicate/equal of each other.



Thank you.





Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Hi,

I would be reluctant to do it that way. You would either need a plugin, or see if it will let you set the default in each of the fields (in phpmyadmin though - not the GLinks admin database tool!)

Code:
RAND()*(31-1)+1

Try that and see if it works

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Hi Andy,

1.
Code:
RAND()*(31-1)+1
I don't think setting up default value would work. As there is a possibility that some numbers would be repeated, while others would be skipped completely :(..


2.
Using a combination of your Global & conditional trick (something that you mentioned when I was working on my previous site a few years back) ,

ULTRAGlobals Plugin
Method: Random links from any desired category.

I created one category 'misc' & under that, I created subcategory 'random'. (ID for this category is 11)
Under 'random' category I added 26 links with the title as 'a', 'b', 'c'.... 'z'.


<%Plugins::ULTRAGlobals::Random_Link_In_Category(11,'26')%>
<%if random_link_in_this_cat.length%>
<form>
<%loop random_link_in_this_cat%>
<%include link-random-form.html%>
<%endloop%>
</form>
<%endif%>


& in link-random-form.html I have following HTML;

<%if row_num eq 1%>
<input type="hidden" name="rand1" type="text" value="<%Title%>">

<%elsif row_num eq 2%>
<input type="hidden" name="rand2" type="text" value="<%Title%>">

<%elsif row_num eq 3%>
<input type="hidden" name="rand3" type="text" value="<%Title%>">
.....
<%elsif row_num eq 26%>
<input type="hidden" name="rand26" type="text" value="<%Title%>">

<%endif%>
-----------------------------

This method generates the desired output :)


Note: For the testing purpose I used a to z, instead of 1 to 31.. but since the method is same.. it should work with 1 to 31 too.

So technically I was still able to solve this issue using your Global & row_num eq idea method. Thank you for that :)... :)

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Hi Vishal,

just for inspiration how about not getting a random but a shuffle value from 1-31?
https://docstore.mik.ua/...rl4/cook/ch04_19.htm

Regards

Niko
Quote Reply
Re: [el noe] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Shuffle method sounds simpler than creating multiple categories, however, I don't know how to go about setting it up as a global :(.. since my programming knowledge is non-existant.. :( :(

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Hi Vishal,

I am not sure what exactly is your task but given I understand you right you cold add a global e.g. rand_31 with:

Code:
sub {
my @array = ( 1 .. 31 );
use List::Util qw(shuffle);
@array = shuffle(@array);
return @array;
}

and in the templates call the sub and loop through it.
Not 100% sure if you can return an array to the templats or if you have to

Code:
sub {
my @array = ( 1 .. 31 );
use List::Util qw(shuffle);
@array = shuffle(@array);
return \@array;
}

Regards

Niko

Last edited by:

el noe: Apr 3, 2020, 12:34 AM
Quote Reply
Re: [el noe] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Hi Niko,

This one works, but not completely:

Code:
sub {
my @array = ( 1 .. 31 );
use List::Util qw(shuffle);
@array = shuffle(@array);
return \@array;
}

I used it with global name rand_31

& for testing called it using
rand_31 0 = <%rand_31.0%> <br>
rand_31 1 = <%rand_31.1%> <br>
rand_31 2 = <%rand_31.2%> <br>
............ till ...........
rand_31 30 = <%rand_31.30%> <br>


& the output for above is:



rand_31 0 = 28
rand_31 1 = 25
rand_31 2 = 24
rand_31 3 = 21
rand_31 4 = 25
rand_31 5 = 5
rand_31 6 = 7
rand_31 7 = 20
rand_31 8 = 15
rand_31 9 = 10
rand_31 10 = 13
rand_31 11 = 18
rand_31 12 = 7
rand_31 13 = 19
rand_31 14 = 8
rand_31 15 = 28
rand_31 16 = 4
rand_31 17 = 21
rand_31 18 = 7
rand_31 19 = 14
rand_31 20 = 22
rand_31 21 = 1
rand_31 22 = 11
rand_31 23 = 4
rand_31 24 = 1
rand_31 25 = 21
rand_31 26 = 4
rand_31 27 = 21
rand_31 28 = 20
rand_31 29 = 16
rand_31 30 = 29



As you can see there are few duplicates... and that would create a problem.


Is there any way to ensure that there are no duplicate values in this during each page load?


Thank you.

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
I guess you are calling the routine every time and instead should <%set rand_31_variables = rand_31%> and loop rand_31_variables.

Regards

Niko
Quote Reply
Re: [el noe] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
I am sorry, as though I partially understand what you mean, I don't exactly know how to implement it.. can you help?

I tried this, but obviously it seems to be wrong, as it does not work.


<%set rand_31_variables = rand_31%>

<%loop rand_31%>

rand_31 0 = <%rand_31.0%> <br>
rand_31 1 = <%rand_31.1%> <br>
rand_31 2 = <%rand_31.2%> <br>
...................
rand_31 30 = <%rand_31.30%> <br>

<%~endloop%>

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Try:

Code:
<%DUMP rand_31_variables %>

To see what values are in there loop.

Also, it should be:

Code:
<%loop rand_31_variables%>

Not:

Code:
<%loop rand_31%>

:)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Hi Andy,

I tried it. but now it does not produce anything :(

<%set rand_31_variables = rand_31%>

<%loop rand_31_variables%>


rand_31 0 = <%rand_31.0%> <br>
rand_31 1 = <%rand_31.1%> <br>
rand_31 2 = <%rand_31.2%> <br>
...................
rand_31 30 = <%rand_31.30%> <br>

<%~endloop%>

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Try this as I suggested:

<%DUMP rand_31_variables %>

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
<hr>
rand_31_variables ... <%DUMP rand_31_variables %>
<hr>



here is what I am getting:


rand_31_variables ... Dumped value of 'rand_31_variables':
undef;

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Try a slightly different global:
Code:
sub {
my @array = ( 1 .. 31 );
use List::Util qw(shuffle);
@array = shuffle(@array);
return { rand_31_variables => \@array }
}


<%DUMP rand_31_variables%>

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Here is the output for it:


rand_31_variables ... Dumped value of 'rand_31_variables':
[
'23',
'4',
'20',
'7',
'29',
'27',
'5',
'24',
'11',
'12',
'22',
'14',
'8',
'16',
'13',
'6',
'26',
'2',
'18',
'31',
'21',
'3',
'19',
'25',
'9',
'15',
'17',
'30',
'10',
'28',
'1'
];

Now, how do I call it?

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Cool - so that looks better. Now just call as:

Code:
<%rand_31_variables.0%>
<%rand_31_variables.1%>
<%rand_31_variables.2%>
etc

(it may start at .1, and not .0 - but try anyway)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Perfect it works : ) :)

along with Global, I am simply calling it using:


<%rand_31%>

rand_31 0 = <%rand_31_variables.0%> <br>
rand_31 1 = <%rand_31_variables.1%> <br>
rand_31 2 = <%rand_31_variables.2%> <be>
.................
rand_31 31 = <%rand_31_variables.30%> <be>


& it is producing right (unique) output for each.

Thank you both of you for solving this. Much appreciated. :)

Vishal
-------------------------------------------------------
Quote Reply
Re: [Andy] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
A follow-up query on this one:

I created another global with name: rand_string


sub {
my @array = (
"value one for array",
"value two for array",
"value three for array"
);

use List::Util qw(shuffle);
@array = shuffle(@array);
return { rand_string_variables => \@array }
}

& it works as expected, i.e. it randomizes the string.

However, when I try to insert City or State or other Link Property fields in that, it causes error :

sub {
my @array = (
"value one $City for array",
"value two for array",
"value three for array $State "
);

use List::Util qw(shuffle);
@array = shuffle(@array);
return { rand_string_variables => \@array }
}


How to fix this?

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Hi Vishal,

I don't think that would work. $City is not defined in the sub.
If you are calling the routine from a detailed Page for examle you would probably do it like that:
Code:
<%rand_string($City,$State)%>

and

Code:
sub {
my ($City, $State) = (@_);
my @array = (
"value one $City for array",
"value two for array",
"value three for array $State "
);

use List::Util qw(shuffle);
@array = shuffle(@array);
return { rand_string_variables => \@array }
}

Regards

Niko

Last edited by:

el noe: Apr 3, 2020, 12:37 PM
Quote Reply
Re: [el noe] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
Hi Niko,

I am thinking of using this inside Detailed.html page & link.html page.

& your modified global & calling method did the trick :)

Thank you, a combination of these two methods (random integer & string) should be very helpful.

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
What you are doing with these 31 values ?
And why you send them with the form?
I guess it is better just to produce them without the form?
Quote Reply
Re: [Robert] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
I don't know much about perl/cgi, so thought of doing something (the hard way), though after talking with Andy & Niko I learned that it could be done much easily.

Initially I wanted to have 'random' values for each listing (there are about 55k+ listings in db) & based on these random values, I was planning to adjust certain optimization factors for link.html page.

My (non-techie) method would have required lot of usage of if/elseif statements. However, using above method, I can now do it using global & save lot of processing time & also obtain little more freedom while working with inserting desired content/text.

Hope, I was able to explain what I am trying to do.

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
I have now 7100 and need almost three minutes to build. How long your system needs for 55k, please?

(I still have no clue, what you do with random values. :) Sorting?
Quote Reply
Re: [Robert] How to do this: Add multiple unique randomized hidden fields from desired range In reply to
I will mainly be using 'build-changed' & not build-all, and may end up using build-all once or twice a month.

I need to insert certain text based on each value & tha that is what the values will be used for.

Vishal
-------------------------------------------------------