Find the current value and previous value from the database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
color0572
Forum Newbie
Posts: 8
Joined: Mon Jun 23, 2008 2:29 pm

Find the current value and previous value from the database

Post by color0572 »

Need your help? please

I would like to do this in php not sql

The result after querying (order by date) are
id | date | weight
2 | 06-12-08 12:15:00 | 150
2 | 06-12-08 12:10:00 | 150
2 | 06-12-08 12:05:00 | 150
2 | 05-12-08 12:10:00 | 140
2 | 05-12-08 12:03:00 |140
2 | 04-12-08 12:15:00 |155

I would want to know the current_date Weight which is (06-12-08 12:15:00 150) and the nearest previous_date Weight (05-12-08 12:10:00 140)

would someone help me with this coding in php?

Thank you very much for your help
co,
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Find the current value and previous value from the database

Post by onion2k »

Completely untested... but it'll be something like..

Code: Select all

SELECT *
FROM `table`
WHERE 1
AND `id` = 2 
GROUP BY DATE(`date`)
ORDER BY `date` DESC
LIMIT 2
color0572
Forum Newbie
Posts: 8
Joined: Mon Jun 23, 2008 2:29 pm

Re: Find the current value and previous value from the database

Post by color0572 »

Thank you very much for your very quick respond, but this is not what I would need. The result above I already did a quering.... and I would like to do some calculate here in php.... The basic for this calculate is find-down the current % of weight change (%loss or %gain) for that person.

At you can see from row1 to row3..in still one recorde user make a mistake enter 3 time and as same as row4 and row5 instill one recorde user make a mistake enter two time and so on....


I hope this more clear and make secen for you

Thanks again onion2k,
co,
hyder_m29
Forum Newbie
Posts: 12
Joined: Mon Jun 23, 2008 9:36 am

Re: Find the current value and previous value from the database

Post by hyder_m29 »

color0572 wrote:Thank you very much for your very quick respond, but this is not what I would need. The result above I already did a quering.... and I would like to do some calculate here in php.... The basic for this calculate is find-down the current % of weight change (%loss or %gain) for that person.

At you can see from row1 to row3..in still one recorde user make a mistake enter 3 time and as same as row4 and row5 instill one recorde user make a mistake enter two time and so on....


I hope this more clear and make secen for you

Thanks again onion2k,
co,
You're still not clear color.

The data can be retrieved from the database only via an sql query and if you're already getting a data via an sql query, why are you looking at alternative methods?!

And if you're looking for percentage gain / loss in weight, that's simple oneweight - secondweight / 100.
+/- will tell you whether gain is positive or negative (gain / loss)

And I don't understand what you mean when you say 'user makes a mistake'. The information is coming from a database right?
color0572
Forum Newbie
Posts: 8
Joined: Mon Jun 23, 2008 2:29 pm

Re: Find the current value and previous value from the database

Post by color0572 »

Thank you very much hyder_m29,

I am sorry for did not make this clear.

Yes, you are right. The data are come from the database (the database is read only), and what I mean by 'user makes a mistake' ... the 'USER' here is the people who are actually enter the weight info into the database. More clear by look at the date at the database

id | date | weight
2 | 06-12-08 12:15:00 | 151
2 | 06-12-08 12:10:00 | 150
2 | 06-11-08 12:05:00 | 150

2 | 05-12-08 12:10:00 | 140
2 | 05-12-08 12:03:00 | 140

2 | 04-12-08 12:15:00 | 155

At month June 06-12-08 supposes to be only one record instills 3 records (this is will be current_weight) and month May show 2 records instill one record (this is will previous_weight). In this case I just thinking that we need to go thought the loop and find the weight have the later date and then do the calculate, but I just don't know how to do it.

I hope you can understand me and I am sorry my English not good enough for explain
Post Reply