MiniVend Akopia Services

[Date Prev][Date Next][Thread Prev][Thread Next][Minivend by date ][Minivend by thread ]

Solved: Randomly picking a product



Hello users,

I solved my problem in a two days work at this weekend. It will not be the
everlasting solution, but it's enought for my needs, and perhaps for many
others too.

Here's what I did:

At first I wrote a new User-defined-Tag and put it into minivend.cfg like
this:


UserTag rand_prod Order max_num
UserTag rand_prod Routine <<EOF
sub{
my ($tmp) = @_;
srand();
$tmp = rand($tmp);
$tmp= int($tmp);
if ($tmp==0){
return 1}
else{
return $tmp}
}
EOF

The UserTag "rand_prod" returns a random number from 0 to "max_num".
"max_num" is given by the [db-count] Tag I found at the end of minivend.cfg
(it's not in the documentation!)

Then I edited the products.asc and filled in a new column named "row" with
an indexing number for each record from 1 to ...... end of records (50 in my
case) in the rows.


In the top of my html-page I inserted following:

<body MV="body 1">

[comment] the following line gets the maximum record-number
                     from products.asc to the variable "db_max" , once on
the page![/comment]

 [value name=db_max set="[db-count]" hide=1]

[comment] the next line calls the User-defined-Tag
                     and puts the returning random number to
"rand_num"[/comment]

        [value name=rand_num set="[rand_prod max_num='[value db_max]']"
hide=1]


        [loop search="se=[value rand_num]
           sf=row
           op=em
           "]
        <img src="[loop-field image]" width="150"><br>
        [loop-field title]<br>
        [loop-field description]
        [/loop]


The [loop-search] Tag did the rest. That was nearly all. - Nearly? Yes!

I tried to put that source more than once into my html-page. That was really
twicky. Every second third and so on, I put that source into the page, the
page did show everytimes the same record. What was that?

After a whole day of confused looking up that problem I had the right idea:
When the UserTag [rand_prod] is invoked at the top of the page more than one
time a second, the same random number appears because the srand()-function
is depending on time by seconds. Take care not to do the same mistake than I
did!

For that I changed my code like this:

 [value name=db_max set="[db-count]" hide=1]
        [value name=rand_num1 set="[rand_prod max_num='[value db_max]']"
hide=1]
        [value name=rand_num2 set="[calc]abs([value rand_num1]-20)[/calc]"
hide=1]
        [value name=rand_num3 set="[calc]abs([value rand_num1]-31)[/calc]"
hide=1]
        [value name=rand_num4 set="[calc]abs([value rand_num1]-42)[/calc]"
hide=1]

        [loop search="se=[value rand_num1]
           sf=row
           op=em
           "]
        <img src="[loop-field image]" width="150"><br>
        [loop-field title]<br>
        [loop-field description]
        [/loop]

        [loop search="se=[value rand_num2]
           sf=row
           op=em
           "]
        <img src="[loop-field image]" width="150"><br>
        [loop-field title]<br>
        [loop-field description]
        [/loop]


and so on. - And it works fine for my needs!

I would be interested, if someone tries with some more records, if it
important slows the page. Give me your feedback, please.

Thank you all for your attention and I hope I could help and someone else
can help _me_ another time.

Sincerely

                Holger Matschinski



Search for: Match: Format: Sort by: