1

I am trying to use a form field to add a number to itself, reversed. Such as number input into field 15729 which would be added to 92751 and the result displayed. Any clue how to do this?

1 Answer 1

2

You could try Computed field to use PHP to calculate the reverse of the number.

Computed Field is a very powerful field module that lets you add a custom "computed fields" to your content types. These computed fields are populated with values that you define via PHP code. You may draw on anything available to Drupal, including other fields, the current user, database tables, you name it. (Feeling the power yet? :) ) You can also choose whether to store your computed field values in the database with other content fields, or have them "calculated" on the fly during node views. (Although you should note that Views use requires database stored values.) This field is literally the Swiss Army knife of fields. So start cooking up your PHP based values!

(heres a snippet from, http://www.phpmoot.com/php-reverse-number/ on how to reverse a number in php - there are lots of different ways to do, just google it)

<?php $num=65410205; // 65410205 $revnum=0; do{ $revnum=($revnum *10)+($num % 10); $num=(int)($num / 10 ); }while($num>0); echo $revnum; // 50201456 ?> 

Then add the two fields together with a Computed field.

Edit: didn't see your tag for javascript, you can use this https://stackoverflow.com/questions/6351825/reverse-decimal-digits-in-javascript#answer-6351965 to reverse the number, then simple JS logic to output the values in the textboxes you want... but using JS for this is not a drupal solution it's pure JS which is slightly off-topic.

1
  • That code is overly complicated. Just use strrev. Commented Oct 10, 2015 at 7:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.