Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Code problem

  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    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

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    The rest of the code would probably help, possibly the database design too
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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?

  • Users who have thanked tomws for this post:

    Begbie (10-27-2009)

  • #7
    New Coder
    Join Date
    Mar 2009
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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

  • #8
    New Coder
    Join Date
    Mar 2009
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts
    tomws you helped me a lot

  • #9
    New Coder
    Join Date
    Mar 2009
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Not workingwith SQL. I need function. But thanks anyway

  • #10
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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?

  • #11
    New Coder
    Join Date
    Mar 2009
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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 ;-)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •