Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6892503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:32:36+00:00 2026-05-27T06:32:36+00:00

For several days now I am trying to cope with the algorithm implementation at

  • 0

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:

  • If the input object date is greater than current object go right

  • If the input object date is less than current object go left

  • If the dates are the same stay at place

  • If the field is blank check if the product is in stock

    • If it is put into place and finish

    • If there is not do nothing and exit

  • If the field is full

{Check if on the list is this user id

  • If yes than check order priority

  • If no do nothing and exit

    • Check if there is product on stock
  • If yes replace record and exit

  • If no do nothing and exit
    }

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

  • If yes then put element on the end

  • If no do nothing and exit

}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:32:36+00:00Added an answer on May 27, 2026 at 6:32 am

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been tearing my hair out for several days now trying to figure out
I've been trying to figure out how to do this for several days now
I've been working on this for a few days now, and I've found several
I've been working in ASP.NET for several years now (since the 1.0 days!), but
Last several days I was trying to set up a proper local SVN (with
Merging in my Mercurial repository is not working like I expected. Several days ago
I haven't been able to solve this problem for several days now and I'm
I've been in a dilemma for several days and now I'd really appreciate your
I've been trying to work this out for 2 days now and I have
The solution for this error has escaped me for several days now, and it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.