sebap123
sebap123

Reputation: 2685

Need advice in algorithm implementation and creation in PHP based shop

For several days now I am trying to cope with the algorithm implementation at the online shop which I am writing in PHP. I do not know whether the problem is only the implementation, or perhaps bad algorithm design. Hovewer, for me it seems fine. I only haven`t checked its complexity of it, but it's such a problem.

After a long deliberation on the same algorithm, without thinking on implementation I came up with the use of binary search tree (bst) with additional data inserted into list consist of user defined info (later about it). The whole orders list would be displayed, or returned using inorder method.

I write it like that:

{Check if on the list is this user id

{If there is not user id on the list check if product is on stock

}

Maybe it looks a little bad, but I was not able to do indentation.

Data is transferred into algorithm in a loop until the end of orders list. The list is unordered.

This is my implementation:

class BinaryTree {    
    private $predescor = array(
        'd'=>array('data'=>0),
        'p'=>null,
        'r'=>null,
        'l'=>null
    );

    private $ancestor = array(
        'd'=>array('data'=>0),
        'p'=>null,
        'r'=>null,
        'l'=>null
    );

    private $i = 0;

    public function insertIntoTree(&$root,$element)
    {
        $this->predescor = $root;
        $this->predescor;

        while($this->predescor)
        {
            if($element['d']['data']==$this->predescor['d']['data'])
            {
                $this->inertIntoList($element,$this->predescor['d']);
                return true;
            }

            $this->predescor = $this->predescor;
            if($element['d']['data']<$this->predescor['d']['data'])
            {
                $this->predescor = $this->predescor['l'];
            }
            else
            {
                $this->predescor = $this->predescor['r'];
            }
        }

        $element['p'] = $this->predescor;

        if(!$this->predescor)
        {
            $root = $element;
        }
        else if($element['d']['data']<$this->predescor['d']['data'])
        {
            $this->predescor['l'] = $element;
        }
        else
        {
            $this->predescor['r'] = $element;
        }

        return true;
    }    
    public function putList(&$list,$root)
    {
        if($root!=null)
        {
            $this->putList($list, $root['l']);
            $lista[$this->i] = $root;
            $this->i++;
            $this->putList($list, $root['r']);
        }
        return;
    }

    private function insertIntoList($element,&$position)
    {
        if($position == null)
        {
            $position = $element;
            return true;
        }

        foreach($position['user'] as &$key)
        {
            if($key == $element['d']['user'])
            {
                if($key['priority']<$element['d']['user']['priority'])
                {
                    return false;
                }
                else if($key['priority']==$element['d']['user']['priority'])
                {
                    return false;
                }
                else
                {
                    if(Orders::checkOrder($element['d']['user']['order']))
                    {
                        $key['order'] = $element['d']['user']['order'];
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        //@todo add at the end
        return true;
    }
}

I would like to advise whether there is a simpler way than using bst consisting of a quite complex arrays, which would also be easier to implement? Because now I can not inplement it in PHP.

Thank you in advance.

Upvotes: 0

Views: 144

Answers (1)

I wouldn't start by coding this in php at all.

I'd start by building this into the database. ("Orders" implies a database.) I'd start by clarifying a couple of points. Assuming that one order can have many line items . . .

  • The number of days since the last order seems to clearly apply to the order, not to individual products.
  • The user can have "only one request carried at a time". Request for what? Doesn't seem to make sense for this to apply either to an order or to an order's line item.
  • The order priority seems to clearly apply to the order, not to line items. But a line-item priority might make more sense. (What products does the customer need first?)
  • Whether the product is in stock seems to apply to the line items, not to the order as a whole.

I'd start by creating two views. (Not because you'll eventually need two views, but because some things are still unclear.)

One view, which has to do with "ranking" as applied to an order, would calculate or display three things.

  • Number of days since the last order.
  • Is this order the "one request carried at a time"?
  • The order priority.

If the numbers assigned to these three things are consistent in scale, you can just sort on those three columns. But that's not likely. You'll probably need to weight each factor, possibly by multiplying by a "weighting" factor. A calculation on the result should let you put these in a useful order. It's not yet clear whether the calculation is best done in the view or in a stored procedure.

The other view would have to do with whether a line item is in stock. It's not clear whether one line item out of stock means the whole order is incomplete, or whether one line item out of stock changes the calculation of a weighted number that scales along with the others above. (You can make a good argument for each of those approaches.)

Upvotes: 1

Related Questions