We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28173
    • 409 Posts
    Hi,

    I have to generate an unique increasing ID transaction number with 6 digits (0000001, 000002, 000003 etc.) each time I'm calling a snippet, for example [[TransactionCounter]].
    I also need to have a placeholder like [[+TransactionID]] which could display the actual value of this counter somewhere on a resource.

    I don't know what is the best way to implement this.
    I have found an ouput filter [[+TransactionID:incr]] but I don't know how to record the previous value somewhere (in database? in a file? in a TV? or a setting parameter?)
    There is also an addon called Hits and even if it's not exactly the same purpose, it can maybe improved it to add the good zeros number to have a 6 digits ID ?

    I'm interested how you would doing this kind of stuff (my PHP knowledge is not very brilliant !!)

    Thanks smiley
    [ed. note: Spheerys last edited this post 9 years, 2 months ago.]
      • 3749
      • 24,544 Posts
      If the snippet is always called from the same page, I'd be tempted to use an unused resource field if there is one (e.g., introtext, longtitle, or description). You could update it in the snippet and display it with a simple tag like this one:

      [[!*description]]


      If there's no unused field, you could use a TV called transaction_number by directly updating the modTemplateVarResource object and display it with an uncached TV tag (note: this code won't work until the TV has an initial value for the resource).

      $tvId = 12; // change to ID of the transaction_number TV
      $docId = 14; // change to ID of the resource;
      
      $tvr = $modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvId, 'resourceid' => $docId));
      $value = $tvr->get('value') + 1;
      
      $trv->setValue(sprintf('%06d', $value));
      $tvr->save();
      




      If this will show on more than one page, I'd create a new System Setting, with a key of transaction_number, and use that (again, give it an initial value). The tag would be:

      [[!++transaction_number]]



      $setting = $modx->getObject('modSystemSetting', array('key' => 'transaction_number'));
      $value = $setting->get('value') + 1;
      
      $setting->set('value', sprintf('%06d', $value));
      $setting->save();
      


      The TV or the System Setting should be of type 'text'.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 4172
        • 5,888 Posts
        maybe, you could create a custom - db-table, where you log all transactions.
        Then you will have autoincrementing ids automatically and you could add additional infos, like a date to each transaction.
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!