How to create a default random value for a field

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

How to create a default random value for a field

Post by buck1942 » 2016-08-25 05:24

I need help regarding how to create a RANDOM value, for a READ ONLY field, between 1 and 1000 (as an integer).

Thanks,

Buck

Bertv
Veteran Member
Posts: 65
Joined: 2013-12-11 15:59

Re: How to create a default random value for a field

Post by Bertv » 2016-08-25 07:16

You can use the MySQL function RAND() and do the update in the after_insert hook.

function your tablename_after_insert($data, $memberInfo, &$args){
// select random value
$randomvalue = sqlValue("SELECT round(RAND() * 1000"), $eo);
// update read-only field
$result = "UPDATE your_tablename SET read_only_field = $randomvalue WHERE id = ".$data['selectedID'];

return TRUE;
}
Bert
I am using Appgini 5.75

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: How to create a default random value for a field

Post by grimblefritz » 2016-08-25 11:17

It depends on WHEN the random value is needed. The solution above writes the value after you save the record. It would not be present when adding a new record, only after saving it.

If you're wanting to populate the field when adding a new record, BEFORE saving it, so it is present during editing... You will need to add code in either tablename_init() or tablename-dv.js

buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

Re: How to create a default random value for a field

Post by buck1942 » 2016-08-26 05:53

Bertv... I'm sorry but your sample code kept giving me a "white" screen. The code below works (at least for me).

grimblefritz is correct, in that, I need the random number fields ( two of them) to populate when adding a new record, BEFORE saving it, so it is present during editing...

My application: I am creating a simple math test for children, for example, 8 times 9... 8 and 9 would be randomly created and displayed so the student can answer the question, before saving the record.

grimblefritz, I tried the tablename_init(), but that did not work for me (or I entered something incorrectly). I used the code, below, in the after_insert_function... the fields were updated in the record, but after the fact. I need them display during edting.

I wonder if the "default" option on Appgini's main page could be used? Is is possible to get the code to work there? I tried, but I am not too sure that I used the proper syntax.

If y'all have another suggestion, I would appreciate it...

Buck


//==========================================================================
// select random value & update number_1 & number_2

$randomvalue = mt_rand (1,1000);
sql("UPDATE questions SET number_1 = $randomvalue WHERE id = ".$data['selectedID'], $eo);
$randomvalue = mt_rand (1,1000);
sql("UPDATE questions SET number_2 = $randomvalue WHERE id = ".$data['selectedID'], $eo);


//===========================================================================

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: How to create a default random value for a field

Post by grimblefritz » 2016-08-26 12:43

Sorry, when I said tablename_init(), I meant to say tablename_dv().

The thing with tablename_dv() is that it's called after the html is generated, but before it's returned to the browser. You can do what you want there, but only by modifying the html. Doable, but cumbersome.

(If AppGini support sees this, maybe we could have a tablename_before/after_select() hooks, that passes the record in $data? Similar to the before/after_insert/update functions?)

So, I'm thinking I would do this using jquery, something like this:

Code: Select all

// in file tablename-dv.js
$j(function(){
    $j('#number_1').val(Math.floor(Math.random()*1000)+1);
    $j('#number_2').val(Math.floor(Math.random()*1000)+1);
});
You might need to fiddle with the Math, but I think that will give you numbers in the range 1-1000

buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

Re: How to create a default random value for a field

Post by buck1942 » 2016-08-27 00:13

grimblefritz... sorry to be a pest,, but I cannot get it to work.

Here is my code followed by the ERROR that I am getting:

102 function questions_dv($selectedID, $memberInfo, &$html, &$args){
103
104 // in file tablename-dv.js
105 $j(function(){
106 $j('#number_1').val(Math.floor(Math.random()*1000)+1);
107 $j('#number_2').val(Math.floor(Math.random()*1000)+1);
108 });
109
110 }


Fatal error: Function name must be a string in C:\wamp\www\appgini\mathquiz\hooks\questions.php on line 105

I would appreciate your taking another look for me.

Thank you,

Buck

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: How to create a default random value for a field

Post by grimblefritz » 2016-08-27 00:16

buck, you're putting javascript code into questions.php. The code I posted needs to be place in tablename-dv.js - in your case, questions-dv.js. That's why I put that comment above the code :)

buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

Re: How to create a default random value for a field

Post by buck1942 » 2016-08-27 01:20

grimblefritz... I saved the file in the HOOKS directory and it WORKS as you had laid it out...

Now I will move on to the next issue.

Your routine works, but I had to change the fields from READ ONLY to allow entry. I will look into that... as I cannot allow a student to change the values.

Thank you for your help and sticking with me...

Buck

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: How to create a default random value for a field

Post by grimblefritz » 2016-08-27 01:33

See this post for the solution to the readonly problem.

http://forums.appgini.com/phpbb/viewtop ... 1391#p6255

Post Reply