View Full Version : Code problem

10-26-2009, 11:33 PM
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:


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

10-26-2009, 11:51 PM
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?

10-27-2009, 12:09 AM
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

10-27-2009, 12:19 AM
The rest of the code would probably help, possibly the database design too

10-27-2009, 12:33 AM
Ok this is how the table looks

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

10-27-2009, 01:24 AM
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):

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.

10-27-2009, 01:54 AM
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

10-27-2009, 02:11 AM
tomws you helped me a lot ;)

10-27-2009, 03:23 AM
Not workingwith SQL. I need function. But thanks anyway ;)

10-27-2009, 01:55 PM
Maybe something like this?

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.

10-27-2009, 03:19 PM
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 ;-)