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 182285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:54:45+00:00 2026-05-11T14:54:45+00:00

Using php5.2 and MySQL 4.1.22 I’ve come across something that, at first, appeared simple

  • 0

Using php5.2 and MySQL 4.1.22

I’ve come across something that, at first, appeared simple but has since evaded me in regards to a simple, clean solution.

We have pre-defined ‘packages’ of product. Package 1 may have products A, B and C in it. Package 2 may have A, C, D and G in it, etc. The packages range in size from 3 to 5 products.

Now, a customer can pick any 10 products available and make a ‘custom’ package. Since we already have certain predefined packages, we’d like to build the custom package with smaller existing packages (for shipping ease) where possible.

So, for instance, a customer selects to create a ‘custom package’ of products A, B, C, D, E and F. We already have a predefined package that contains A, B and C called Foo. So, the order would then be Foo, D, E and F.

The catch is in having the least amount of individual items, followed by the least amount of packages. For instance:

Custom Package: A, B, C, D, E, F, G, H, I, J.

Predefined Package (1): A, B, C, D, E

Predefined Package (2): A, B, C

Predefined Package (3): D, E, F

If I simply take the largest match, then I have 1 (5pc) package and 5 individual items. Neither Package (2) nor (3) can be built with the remaining items.

If I look deeper, I find that by not building package (1) I can instead build package (2) and package (3). Which means I have 2 packages and 4 individual items (a better choice in this buisiness rule).

As I’m using MySQL, I’m under the restraint of only having one layer of sub select available (to my knowledge). So this sort will need to be performed in php. I’ve looked at using array_intersect() to determine matches, but every way I’ve found grows exponentially in regards to processing as the number of predefined packages grows linearly.

I ran this by a couple other coder friends and again, while it seemed like there should be an easy answer we all found that it wasn’t as simple as it seems. So, I thought I’d post it here as a nice noodle stretcher. Thanks much in advance for your time!

  • 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. 2026-05-11T14:54:46+00:00Added an answer on May 11, 2026 at 2:54 pm

    The problem is generally a ‘hard’ one (speaking in terms of computational complexity). In fact it rings some bells in the back of my head that it probably reduces to one of those classic algorithm problems like the Knapsack problem, but I can’t attach a proper name to it.

    However, with such a small problem space (they can only pick 10 products), it should be fairly quick to just brute-force the thing. When someone submits a custom build, just recursively attack it with all possibilities and see which one is the best.

    That is, take the components they’ve selected, and first try to remove the components of ‘Package 1’ from it. If that’s possible, take the remaining components and try to take the components of ‘Package 2’ from it, etc. Keep track of the best solution you’ve found so far as you go along.

    If it’s still not fast enough (but I think it probably will be, depending on how many pre-built packages you have), you could apply some dynamic programming methods to speed it up.


    Edited to add:

    Depending on the number of possibilities and how long this actually takes to run, you may want to write the code I described above, and then just go ahead and pre-compute all the solutions for every possible combination. Then when someone submits a custom build, you just have to fetch the answer instead of computing it from scratch every time.

    Even if you don’t want to pre-compute them all, I’d suggest storing the result every time someone does a custom build, then in the future if anyone else does the same custom build you don’t have to recalculate the solution.

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

Sidebar

Ask A Question

Stats

  • Questions 85k
  • Answers 85k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think it's Ruby on Rails that inspired MS to… May 11, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer http://www.devshed.com/c/a/Python/Windows-Programming-in-Python-Creating-COM-Servers/ May 11, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer The reason for this is that the binding system in… May 11, 2026 at 5:11 pm

Related Questions

Using php5.2 and MySQL 4.1.22 I've come across something that, at first, appeared simple
I have a fairly small MySQL database (a Textpattern install) on a server that
I have a dataset obtained from MySQL that goes like this: Array ( [0]
I have a login screen that I force to be ssl, so like this:
I want to benchmark PHP vs Pylons. I want my comparison of both to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.