Page 1 of 1

Question on code for issuing a receipt number

Posted: Sun Aug 17, 2008 5:37 pm
by ggrant3
I currently have some php code that I am using for issuing a 12 digit receipt number which inserts the current year as the first bock of numbers.

Code: Select all

// get the contents of the text file
  $receipt = file_get_contents('receipt.txt');
  // break it into an array of parts
  $parts = explode('-', $receipt);
 
  if ($parts[0] != date('Y')) {
    $parts[0] = date('Y');
    $parts[1] = '0000';
    $parts[2] = '0000';
  }
 
  if ($parts[2] <= 9998) {
    $parts[2]++;
    $parts[2] = sprintf('%04d', $parts[2]);
  }
 
  else {
    $parts[2] = '0001';
    $parts[1]++;
    $parts[1] = sprintf('%04d', $parts[1]);
  }
 
   // rebuild the number and reassign to $receipt
  $receipt = implode('-', $parts);
  $file = fopen('receipt.txt', 'w');
  fwrite($file, $receipt);
  fclose($file);
But I would like to change it to a smaller receipt number and I do not need the year to be incorporated in the number anymore.

I would like the number to be displayed like this: 000-0000

I am aware that I need to change the actual number in my "receipt.txt" folder to the seven digit layout, but I can't figure out how to modify the code to work properly and increase the receipt number by 1 each time an order is placed.

Can anyone help me out with this?

Re: Question on code for issuing a receipt number

Posted: Sun Aug 17, 2008 7:31 pm
by califdon
There's no difference in how you increment the number. Just omit the date part and change the array index numbers and the number of digits for the first part:

Code: Select all

// get the contents of the text file
  $receipt = file_get_contents('receipt.txt');
  // break it into an array of parts
  $parts = explode('-', $receipt);
 
  if ($parts[1] <= 9998) {
    $parts[1]++;
    $parts[1] = sprintf('%04d', $parts[1]);
  } else {
    $parts[1] = '0001';
    $parts[0]++;
    $parts[0] = sprintf('%03d', $parts[0]);
  }
 
   // rebuild the number and reassign to $receipt
  $receipt = implode('-', $parts);
  $file = fopen('receipt.txt', 'w');
  fwrite($file, $receipt);
  fclose($file);
This means that most of the time you will be incrementing the second part of your new number, and when it reaches something like 123-9999, the next assignment will be 124-0001. I believe that's what you said you want.