Array Calculation Problems

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

Post Reply
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Array Calculation Problems

Post by AGISB »

I got a problem and I only got a time consuming solution.

I got a array with recipes including total calories. Now I want to create a daily menu with lets say 1500 calories max.

However I want to get as close to the 1500 as possible.

Now there is an array for breakfast, lunch and dinner.


My solution for now is start with breakfast and then add the highest calorie lunch and then starting to add dinner recipes untill it fits. If I get over the 1500 with each I take the next lunch recipe and go again with the dinner recipes and so on. This seems timeconsuming to me. Anyone got another solution idea?

I don't want to recompile the daily menus and do it on the fly. However this will be done by a cgi application once a day.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Array Calculation Problems

Post by BDKR »

AGISB wrote: I don't want to recompile the daily menus and do it on the fly. However this will be done by a cgi application once a day.
Without getting into the other algorythmic issues I'll offer something relating to the above quote. If you don't want to do all this calculating on a daily basis, don't. Why not run through each possilbe combination and only store those that are acceptable in a look up table of sorts. If you later add a new dish at say breakfast or dinner, run a script or program to recompute the entire thing.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

While going to the grocery store I had the same idea. It will get kind of big but I will just test it
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

AGISB wrote:It will get kind of big but I will just test it
Sure it could get kind of big, but it's better then creating it daily. Just be careful how you store it.
Post Reply