Buy Now
Sign in

random extraction from db

mcasamassima , May 23 2017
Answered
M
mcasamassima
I need to extract random records through the order by rand () function, how can i do it?
admin
Hi mcasamassima,

Which version you are using ?

Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
M
mcasamassima
I'm using the latest version of ccv5
admin
Hi mcasamassima,

Do you have the SQL syntax to do that ?

Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
M
mcasamassima
Hi, the syntax is select field from table where cond order by rand().
Thanks
Michele
admin
Hi Michele,

Please try to use this code in the order by:

<?php
return array("rand()");


Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
M
mcasamassima
i receive this error
"1305 - FUNCTION operatori_economici_incarichi.rand does not exist"
Thanks Mike
admin
Hi Mike,

Try this then:

<?php
return array(mt_rand() / mt_getrandmax());


Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
M
mcasamassima
Unfortunately it does not work...
admin
Hi Mike,

Could you please try this code in the order box then:

<?php
return 0.369500624360052;


dos it work ?

Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
M
mcasamassima
i receive this error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<?`operatori_economici_incarichi`.`php` `operatori_economici_incarichi`.`retur' at line 1

Thanks Mike
admin
Hi Mike,

Then I think you have placed the code incorrectly ?

Bet regards,
Max
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
M
mcasamassima
Mistake me, but is it without the "array" right?
I think i only want the form "return array" within the field "order by"
M
mcasamassima
solved !! You have to put two points ahead!
in the order box :

<? Php
Return array (": rand ()");
?>
R
rafaelscouto

Good morning, I would like to take this topic. I need to create a lottery system, I have a database like this: ID, NAME, REGISTRATION.​
​​
​I would like to return this data randomly, based on ID for example.​
​​
​There will be five draws, so I need the data that is drawn to be repeated, how can I do that?​
​​
​I'm using CC6

GreyHead

HI rafaelscouto,

Please check this StackOverFlow answer

Bob

ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
R
rafaelscouto

Thanks for the contact, but I have no idea how to resolve this.

R
rafaelscouto

Please, I need to resolve this.

admin

Hi Rafael,

In the "read data", set the "select type" to "first matching record", then write the following code in the "offset" box:

{var:random}

Now add a "PHP" function, set the name to "random" and write this code:

return rand(0, 9); //assuming you have 10 records in the table

Best regards

Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
R
rafaelscouto

I did exactly as you said, but only returns the 1st ID.​
​Here are pictures of how it is

https://imgur.com/a/kkRRZ

Attachments
img_01.png
img_01.png
(22.1 KiB)
49 Downloads/Views
img_01.png
img_01.png
(22.1 KiB)
55 Downloads/Views
img_02.png
img_02.png
(23.43 KiB)
44 Downloads/Views