Page 3 of 3

Re: Where would you start your order number iteration at?

Posted: Fri Mar 13, 2009 9:47 am
by alixaxel
kaisellgren wrote:Both implementations work, but I aimed for better performance. :D
If you are looking for performance, I'm sure that the following changes would make the execution faster:

Code: Select all

 
/*
I prefer using the str_split()/foreach() combination for better code
readability, but you can change to for() to further improve performance.
*/
function Calculate($number, $iterations = 1)
{
    while ($iterations-- >= 1) // Maybe while (--$iterations >= 0) is faster?
    {
        $result = 0;
        $number = str_split(strrev($number), 1); // Slower, but prettier.
 
        foreach ($number as $key => $value) // Again, prettier. =)
        {
            $result = $this->d[$result][$this->p[($key + 1) % 8][$value]];
        }
 
        $number = strrev(implode('', $number)) . $this->inv[$result];
    }
 
    return $number;
}
 
function Check($number, $iterations = 1)
{
    $result = substr($number, 0, -$iterations);
 
    if ($this->Calculate($result, $iterations) == $number)
    {
        return $result;
    }
 
    return false;
}
 
Also you have the incorrect result for Calculate(1337, 100), you should have an additional 2 at the end.

Re: Where would you start your order number iteration at?

Posted: Fri Mar 13, 2009 9:58 am
by kaisellgren
alixaxel wrote:Also you have the incorrect result for Calculate(1337, 100), you should have an additional 2 at the end.
What?
13375421749977373094095856463477749335848455242775507269446021270533068925331696928471276574898221873092
13375421749977373094095856463477749335848455242775507269446021270533068925331696928471276574898221873092
For me both classes work fine. (1337 with 100 iterations)
alixaxel wrote:If you are looking for performance, I'm sure that the following changes would make the execution faster:
Yup, it works faster, but not as fast though. To me ugly code is ok if it's well commented. :P

I think the OP decided to use simple primary keyed IDs and made them to start at a high value.

Re: Where would you start your order number iteration at?

Posted: Fri Mar 13, 2009 10:17 am
by Jenk
That's a lot of fuss over something so insignificant. I'm curious to know if anyone has actually had a client willing to spend time and money on you actually developing that, *just* for an order number.

Re: Where would you start your order number iteration at?

Posted: Fri Mar 13, 2009 10:21 am
by kaisellgren
Jenk wrote:I'm curious to know if anyone has actually had a client willing to spend time and money on you actually developing that, *just* for an order number.
Sure.. 8)

Re: Where would you start your order number iteration at?

Posted: Fri Mar 13, 2009 10:31 am
by alixaxel
Jenk wrote:That's a lot of fuss over something so insignificant. I'm curious to know if anyone has actually had a client willing to spend time and money on you actually developing that, *just* for an order number.
Agreed, this went from a simple auto-increment value to a complex check digit algorithm and finally to algorithmic optimizations lol, but to answer your question, yes - I had a client with big pockets that wanted a simple way of validating shipping codes in the application I developed and this was how I first found about the Verhoeff algorithm, ever since then I use it on most of my ID database fields.

Re: Where would you start your order number iteration at?

Posted: Fri Mar 13, 2009 10:43 am
by crazycoders
I didn't read everything so i'll be brief... usually, you may want to start your numbering at the number your client wishes and that number is not the ID of the column. Simply because, sometimes, order numbers may mutate into something that contains numbers and letters.

So i'd use an auto-inc for the ID of the order table, you COULD base your ORDERID column (a textual column) on your ID but then, you only need to generate your own algorithm that would generate order ids.

This method solves two problems:

1) In solutions that bill a user, you have to keep a trace of what happens... ALWAYS. I don't know of any country that states that you can play around and delete billing info.

2) Several countries force their companies to fill all of their order numbers, no number must miss, so what happens in systems that increment the seed value with or without transaction success? When you use your order table's id in a seeded auto-inc column and your transaction fails, some system will keep the increment, and you will create a gap. With a system that forces you to generate the next sequential id yourself, you can then prevent gaps of that kind...

Cheers