Decrementing a counter
            3 posts by 2 authors in: Forums > CMS Builder
Last Post: November 24, 2009   (RSS)          
By gkornbluth - November 21, 2009 - edited: November 21, 2009
          Hi all,
Anyone know if there's a way to use something like the built in incrementCounterField function to decrement a counter instead?
Thanks,
Jerry Kornbluth
        Anyone know if there's a way to use something like the built in incrementCounterField function to decrement a counter instead?
Thanks,
Jerry Kornbluth
      The first CMS Builder reference book is now available on-line!
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
                    Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Decrementing a counter
By Chris - November 24, 2009
          Hi Jerry,
There's nothing built-in, but you could copy it, rename it, change it to do what you want.
Here, I've done just that (changes in red.) Call it with -1 as the fourth parameter to decrement a counter.
                          
        There's nothing built-in, but you could copy it, rename it, change it to do what you want.
Here, I've done just that (changes in red.) Call it with -1 as the fourth parameter to decrement a counter.
function incrementCounterFieldBy($tablename, $fieldname, $recordNumber, $amount = 1) {
  global $VIEWER_NAME;
  // error checking
  if (!$tablename)    { die(__FUNCTION__ . ": No 'tablename' value specified!"); }
  if (!$fieldname)    { die(__FUNCTION__ . ": No 'fieldname' value specified!"); }
  if (!$recordNumber) { die(__FUNCTION__ . ": No 'recordNumber' value specified!"); }
  // update counter
  $escapedTableName = mysql_escape(getTableNameWithPrefix($tablename));
  $query  = "UPDATE `$escapedTableName` SET `$fieldname` = IFNULL(`$fieldname`,0) + $amount";
  $query .= " WHERE `num` = '" .mysql_escape($recordNumber). "'";
  $result = @mysql_query($query);
  if (!$result) { die(__FUNCTION__ . " MySQL Error: ". htmlspecialchars(mysql_error()) . "\n"); }
  if (!mysql_affected_rows()) {
    die(__FUNCTION__ . ": Couldn't find record '" .htmlspecialchars($recordNumber). "'!");
  }
}
      All the best,
Chris
                    Chris
Re: [chris] Decrementing a counter
          Thanks Chris,
You're the best.
I'll play with this tomorrow.
Jerry
        You're the best.
I'll play with this tomorrow.
Jerry
      The first CMS Builder reference book is now available on-line!
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
                    Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php