Where would you start your order number iteration at?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

alixaxel
Forum Newbie
Posts: 15
Joined: Thu Mar 12, 2009 9:00 am

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

Post 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.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

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

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

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

Post 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.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

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

Post 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)
alixaxel
Forum Newbie
Posts: 15
Joined: Thu Mar 12, 2009 9:00 am

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

Post 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.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

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

Post 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
Post Reply