开发者

A CCK field that's a random number

开发者 https://www.devze.com 2023-01-09 22:29 出处:网络
Anyone aware of a CCK module that adds a text field that\'s just a randomly generated number? This means that when the user tries to create a fresh node, he sees a pre-populated random number as one

Anyone aware of a CCK module that adds a text field that's just a randomly generated number?

This means that when the user tries to create a fresh node, he sees a pre-populated random number as one of the fields (and can't change that开发者_Go百科 field)


Try the Computed Field module, which allows you to create fields whose values are defined by PHP snippets. For a random number, you might use something like this in your field's "Computed Code" configuration:

$node_field[0]['value'] = rand();

Note that rand() also accepts min and max arguments, in the form of rand(min, max).

Also be sure to enable the "Store using the database settings below" setting with a data type of "int" and a reasonable data length based on the expected range of values for rand(). This will ensure that the field isn't recomputed every time the node is displayed.

EDIT: I just realized that the above method stores a new random value each time the user updates/resaves the node. If you'd like the field to generate and store a random number once and only once for each node (at the node's initial save), try something like this instead:

if (!$node_field[0]['value']) {
  $node_field[0]['value'] = rand();
}

This will only set a value for the field if one doesn't already exist.

0

精彩评论

暂无评论...
验证码 换一张
取 消