Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-26-2009, 11:33 PM   PM User | #1
Begbie
New Coder

 
Join Date: Mar 2009
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Begbie is an unknown quantity at this point
Code problem

Hello I am new with php and I am also new with programming and basically this is a programming problem but I hope that someone could help me.
Here is the code and I'll try to explain what it is supposed to do:
PHP Code:
<?php

$query2 
="SELECT * 
        FROM arbeitszeiten a, schichtumsatz s 
        WHERE a.schicht = s.id
        "
;
$connection mysql_connect($dbhost$dbusername$dbpass); 
$SelectedDB mysql_select_db($dbname); 
$result2 mysql_query($query2);          // query executed
echo mysql_error();              // if any error is there that will be printed to the screen


$time["field"]; //this variable is taking one row from the tabel row of time
$x 0;
$counter 0;
$y =0;
$check $vonDatum;  //starting date

while($row2 mysql_fetch_array($result2)){

$x $x $time// here i get sum of time from all dates that have been listed in SQL query 

if ($check!=$row2["datum"]){ // check if the date is the same
$check=$row2["datum"]; //if it's not transform it
$counter ++; // count how many changes (different dates) has SQL query listed 
$y $y $zeit//here I get sum of time only on the day when the date is changing
}

?>
I have a database where I have on one date time of how much somebody (users) have worked (time). I want this time to calculate (for all users) => sum it. In the code above I managed to calculate for all dates but I need for each date separately so i put the counter to calculate changes of dates, and now I don't know what else to do. How to do this final step. How to calculate for each count(date) sum of time. Please help, because I am so confused now, that I don't know what to do .Thanks in advance.
Begbie is offline   Reply With Quote
Old 10-26-2009, 11:51 PM   PM User | #2
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
I've read your post about 10 times, and I'm not quite understanding what you need. Could you try explaining it in a different way?
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Old 10-27-2009, 12:09 AM   PM User | #3
Begbie
New Coder

 
Join Date: Mar 2009
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Begbie is an unknown quantity at this point
OK i will try to explain in different way.
I have different dates: 23-11-2008, 11-05-2009, 12-09-2009 and so one.
I have for that dates saved how much one user worked:
On date 23-11-2008 user1 worked 9 h, user2 3h, user3 5 h
On date 11-05-2009 user 2, worked 3h, user2 5h, user3 7h
....
So I need for each day sum of working hours:
For 23-11-2008 => 9 +3 +5 = 17
For 11-05-2009 => 3 + 5 + 7 = 15

So, in code for variable x result is 32 (17 + 15) and I need only 17 and 15.
So, I put counter which for 3 dates (3-11-2008, 11-05-2009, 12-09-2009) on the end has value 3.
And I thought if I try to count these times ($y = $y + $zeit) inside of "if" loop that I will get desired result, but I get only as result values on the end => 5, 7
So I hope that now you coul help me . Thanks

Last edited by Begbie; 10-27-2009 at 12:13 AM..
Begbie is offline   Reply With Quote
Old 10-27-2009, 12:19 AM   PM User | #4
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
The rest of the code would probably help, possibly the database design too
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Old 10-27-2009, 12:33 AM   PM User | #5
Begbie
New Coder

 
Join Date: Mar 2009
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Begbie is an unknown quantity at this point
Ok this is how the table looks
http://tinyurl.com/ylxwv4b

and important columns are datum and stunden.
About the code I can't upload all code because there isn't much of it and most of it is sql. the main part is what I have already uploaded.
And once again thanks for any help you give
Begbie is offline   Reply With Quote
Old 10-27-2009, 01:24 AM   PM User | #6
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
The right query can take care of this for you, so then you only need to extract the results. Assuming that I understand correctly that you want only the total hours worked per day, something like this might be close (I'll use English words so I don't confuse myself):
Code:
SELECT `date`, sum(`hours`) myHours FROM `myTable` GROUP BY `date` ORDER BY `date`
Then you can extract the dates and total hours for each date inside your while loop.
__________________
Are you a Help Vampire?
tomws is offline   Reply With Quote
Users who have thanked tomws for this post:
Begbie (10-27-2009)
Old 10-27-2009, 01:54 AM   PM User | #7
Begbie
New Coder

 
Join Date: Mar 2009
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Begbie is an unknown quantity at this point
thanks on advice I wil try, but the problem is that I get this column hours from two other columns: "from" and "until"
hours="until" - "from"
Do you know how to make this query in SQL and then sum. Thanks
Begbie is offline   Reply With Quote
Old 10-27-2009, 02:11 AM   PM User | #8
Begbie
New Coder

 
Join Date: Mar 2009
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Begbie is an unknown quantity at this point
tomws you helped me a lot
Begbie is offline   Reply With Quote
Old 10-27-2009, 03:23 AM   PM User | #9
Begbie
New Coder

 
Join Date: Mar 2009
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Begbie is an unknown quantity at this point
Not workingwith SQL. I need function. But thanks anyway
Begbie is offline   Reply With Quote
Old 10-27-2009, 01:55 PM   PM User | #10
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
Maybe something like this?
Code:
SELECT `date`, (sum(`until`) - sum(`from`)) myHours FROM `myTable` GROUP BY `date` ORDER BY `date`
That may not be right, but it ought to be in the correct direction, anyway.
__________________
Are you a Help Vampire?
tomws is offline   Reply With Quote
Old 10-27-2009, 03:19 PM   PM User | #11
Begbie
New Coder

 
Join Date: Mar 2009
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Begbie is an unknown quantity at this point
Yeah that thing works but I have another sql query that I need to list some other things and I can not use two queris at the same time because it cause a lot of problems. Anyway thanks ;-)
Begbie is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:31 PM.


Advertisement
Log in to turn off these ads.