1

Trying to organize my thoughts on how to save multiple, time worked entries into db.

So my thoughts on how to approach this are as follows :

create 2 form fields ,

<select value="<?php echo $beg_time; ?>" name="beginningtime" id="beginningtime">
     <option value = "8">8am</option>
     <option value = "etcetc">etcetc</option>
     <option value = "6">6pm</option>
</select>
<select value="<?php echo $end_time; ?>" name="endtime" id="endtime">
     <option value = "8">8am</option>
     <option value = "etcetc">etcetc</option>
     <option value = "6">6pm</option>
</select>
// Third input to have time worked that I want to save
<?php $end_time - $start_time = $hours_worked;?>// replace with jquery to update on client side
<input type="hidden" value="jQueryhours_worked">

//Server Code :
//somehow concatenate $hours_worked, so that saved value may look like this :
//2,3,5,1

Is there an easier way of doing this, am I reinventing the wheel?

Steven
  • 687
  • 1
  • 10
  • 27
  • 1
    You cannot calculate the `$end_time` in PHP and place it in a hidden field, as the start and end times are not available until the user has selected 2 values from the dropdown. When that happens PHP is long finished and the only way would be to use javascript – RiggsFolly Apr 11 '16 at 17:58
  • The normal format for a calulation is also `$x = $y - $x` and no the other way around as you have it – RiggsFolly Apr 11 '16 at 18:01
  • Note, you have two id's with "beginningtime". You could go the javascript route. – jjwdesign Apr 11 '16 at 18:05
  • @jjwdesign Thanks , I fixed – Steven Apr 11 '16 at 18:07
  • @RiggsFolly Thanks for the heads up, think of this as pseudocode for finished product, altered to reflect – Steven Apr 11 '16 at 18:08
  • Just a thought, it's usually much easier to calculate time differences in military time. values 8, 18, display 8am, 6pm. – jjwdesign Apr 11 '16 at 18:10

2 Answers2

1

You might want to use 24-hour bases as a value. This is because 8-8 = 0, but 20-8 = 12. (20:00 is 8pm), meaning they have worked 12 hours.

Also, you should make sure you aren't using any double id attributes.

Your $hours_worked hidden field won't work, as php does not work like that.

You will need to store the hours worked for every day seperately.

The way I look at it, your code will not work in any case. I could point out small fixes to you but then you would just bump into the next problem. I feel like you should start by researching how to save the results of this function. You also might want to use javascript to make life easier.

Once you've done this, let me know in a comment and I'll update my answer with an actual fix (if by then an actual fix is possible)

Have a look at this question for creating the ability to actually store multiple form entries: Storing Form Data as a Session Variable

Once done, I'll assume you'll use $_SESSION to store these hours per day.

By this point you can just say something like this:

//Checks if the $_SESSION["workedhours"] already has anything stored
//If not, no , beforehand.
if(!isset($_SESSION["workedhours"]){
//this creates the initial session.
  $_SESSION["workedhours"] = $hours_worked;         } 
}
else{
     //This adds to it
     $_SESSION["workedhours"] .= ",$hours_worked";
}

In this case, you will also want to have the start_session() running. This goes on the very first line of your file (inbetween the <?php tags,

  <?php
    //Checks if a session already exists, if not, start one      
    if (session_status() == PHP_SESSION_NONE) {
      session_start();
    }
  ?>

I also recommend you read this, as it's important to know how to get the inserted data from your form:

http://www.w3schools.com/php/php_forms.asp

edit note: The $_SESSION is a temporary thing. It only holds data as long as your browser is open. If you want to store this data permanently, you'll have to use a database. Everything about my answer changes if you use a database, but it doesn't seem like you're using one right now.

Community
  • 1
  • 1
NoobishPro
  • 2,539
  • 1
  • 12
  • 23
  • Thank you for your suggestions. I am relatively new to php, so still learning ! Will be researching jQuery for getting time values, and the 24hour thing is a good idea, would a loop be better for getting the hours? for instance : while $start_time < $endtime, i++. at the end of loop, i = $worked hours(would of course define 12 as a reset to 1 instead of 13), btw, why would I need to store hours_worked seperately? Could I not just have one session w/1 hr worked, create another session and concatenate the results so they save in the db as 1,1? – Steven Apr 11 '16 at 18:15
  • You can use a tilde ( ` ) to format `code` in comments. like this: ``code``. Anyway, you could do that in an if statement, no need for a loop at all. Just say `if($end_time <= $start_time){$end_time = $end_time + 12;)`, right? - sidenote: do *not* confuse a `while` with an `if` statement. If you create a while like that, it will skip over all the hours that are *not* `true` according to the while loop. – NoobishPro Apr 11 '16 at 18:20
  • Yes, you could do that. You can store the hours worked into the `$_SESSION` while they are submitting the form, and then later store the `$_SESSION["workedhours"]` in the database as `text` – NoobishPro Apr 11 '16 at 18:22
0

Why don't you just save every start time and end time of each action into db? You can do various calculation operations directly on SQL layer and you always have the discreet time values at hand for further actions

MacGyer
  • 198
  • 1
  • 6
  • would that not create a crazy db situation? for instance, i would have to have columns as such : starttime1, endtime1, starttime2, endtime2, possibly with dozens needing to be added – Steven Apr 11 '16 at 18:10
  • OP does not seem to be interested in saving any dates, although that wouldn't add much craziness. Why would you do that? You can just create 2 columns: `starttime` and `endtime` and if you want, save the date in a third column `day`. I don't see why that would be crazy at all. – NoobishPro Apr 11 '16 at 18:18
  • @Babydead there will be multiple end start date times that will be saved per one row, not hard to have mm/dd/hh but to have a colum for each consecutive save? – Steven Apr 11 '16 at 18:21
  • I don't understand where you're getting the idea of needing to have multiple columns for each save? You just create a row for every start time and end time. These 2 columns are the only ones you'll ever need. If you are storing these for one person the date field will be enough to calculate the worked hours. If it's for multiple people, you will obviously use an ID linked to said person to get the rows right. I honestly can't see why you would need any more columns for the times. – NoobishPro Apr 11 '16 at 18:32