needs_upgrade Posted September 18, 2013 Share Posted September 18, 2013 (edited) Hello guys, I would like to make a form for employees' attendance from a start_date to an end_date. The name of the employees are listed from top to bottom and the dates will be listed from left to right of the names of the employees. My problem is that I don't know how to name my textboxes so the system would know that a particular textbox is for employee 'A' and for a particular date between the start_date and end_date. The expected datatype to be entered into the textboxes is an integer or decimal number like 1 or 0.5. Here is what I've done so far: $sql = "SELECT DATE_FORMAT(sp_start, '%b %e, %Y'), DATE_FORMAT(sp_end, '%b %e, %Y'), TO_DAYS(sp_start), TO_DAYS(sp_end) FROM salary_periods WHERE sp_id = '$sp_id'"; $sres = mysql_query($sql); $srow = mysql_fetch_array($sres); ?> <form action="somepage.php" method="post"> <table> <tr><td colspan="4" align="center">Salary Period</td></tr> <tr class="even"> <td width="100" align="right">Start Date</td> <td width="250" align="left"><?PHP echo $srow[0] ?></td> <td width="100" align="right">End Date</td> <td width="250" align="left"><?PHP echo $srow[1] ?></td> </tr> </table><br /> <table> <tr> <td width="150" align="center">Employee</td> <td width="550" align="center" colspan="<?PHP echo $srow[3] - $srow[2] + 1; ?>">Dates</td> </tr> <tr> <td> </td> <?PHP for ($j=$srow[2]; $j<=$srow[3]; $j++) { $sql = "SELECT DATE_FORMAT(FROM_DAYS($j), '%m/%d')"; $tres = mysql_query($sql); $trow = mysql_fetch_array($tres); ?><td align="center"><?PHP echo $trow[0] ?></td><?PHP } ?> </tr> <?PHP $i = 1; $sql = "SELECT emp_id, CONCAT(lname,', ',fname) FROM employees WHERE active = 1"; $res = mysql_query($sql); while ($row = mysql_fetch_array($res)) { $m = fmod($i, 2); if ($m == 0) { // $i is EVEN ?><tr class="even"><?PHP } else { // $i is ODD ?><tr class="odd"><?PHP } ?> <input type="hidden" name="emp_id<?PHP $i ?>" value="<?PHP echo $row[0] ?>"> <td align="left"><?PHP echo $row[1] ?></td> <?PHP for ($j=$srow[2]; $j<=$srow[3]; $j++) { $sql = "SELECT DATE_FORMAT(FROM_DAYS($j), '%m/%d')"; $tres = mysql_query($sql); $trow = mysql_fetch_array($tres); ?><td><input type="text" name="TextboxNameHere" size="2" style="text-align:right;"></td><?PHP } ?> </tr> <?PHP $i++; } ?> </table> </form> <?PHP How should I name my textboxes so that each textbox will be assigned to a particular employee and a particular date? Edited September 18, 2013 by needs_upgrade Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 18, 2013 Share Posted September 18, 2013 You can use the same technique that you're using with the hidden fields. Just remember to add "echo". The "echo" was needed before $i below: <input type="hidden" name="emp_id<?php echo $i; ?>" value="<?php echo $row[0]; ?>"> Note that you could also use array syntax. The $id below would correspond to the employee's ID in the database. That way you can quickly associate the text-box entry with an employee. <input type="text" name="TextboxNameHere[<?php echo $id; ?>]" size="2" style="text-align:right;"> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 18, 2013 Share Posted September 18, 2013 use a multi-dimensional array name for the field, with the first index being the employee id and the second index being the date. also, don't repeat code, especially code that runs a query inside of a loop. you are already getting all the formatted dates using a for(){} loop. store those dates in an array and simply loop over that array each time you need to produce the result based on those dates. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 18, 2013 Share Posted September 18, 2013 as a continuation of the above reply, the date you submit from the form should already be in the YYYY-MM-DD format so that you don't need to do any further conversion of it to use it in a query. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 18, 2013 Share Posted September 18, 2013 one way, using just two queries - $sql = "SELECT DATE_FORMAT(sp_start, '%b %e, %Y'), DATE_FORMAT(sp_end, '%b %e, %Y'), TO_DAYS(sp_start), TO_DAYS(sp_end), sp_start, sp_end FROM salary_periods WHERE sp_id = '$sp_id'"; $sres = mysql_query($sql); $srow = mysql_fetch_array($sres); ?> <form action="somepage.php" method="post"> <table> <tr><td colspan="4" align="center">Salary Period</td></tr> <tr class="even"> <td width="100" align="right">Start Date</td> <td width="250" align="left"><?PHP echo $srow[0] ?></td> <td width="100" align="right">End Date</td> <td width="250" align="left"><?PHP echo $srow[1] ?></td> </tr> </table><br /> <table> <tr> <td width="150" align="center">Employee</td> <td width="550" align="center" colspan="<?PHP echo $srow[3] - $srow[2] + 1; ?>">Dates</td> </tr> <tr> <td> </td> <?php $start_date = new DateTime($srow['sp_start']); $end_date = new DateTime($srow['sp_end']); $end_date = $end_date->modify('+1 day'); // if you need the end date in the range $interval = new DateInterval("P1D"); $dates = new DatePeriod($start_date, $interval, $end_date); // Traversable date range foreach($dates as $date){ echo "<td align='center'>{$date->format('m/d')}</td>"; } echo "</tr>\n"; $i = 1; $sql = "SELECT emp_id, CONCAT(lname,', ',fname) FROM employees WHERE active = 1"; $res = mysql_query($sql); while ($row = mysql_fetch_array($res)) { $m = fmod($i, 2); if ($m == 0) { // $i is EVEN echo '<tr class="even">'; } else { // $i is ODD echo '<tr class="odd">'; } echo "<td align='left'>{$row[1]}</td>"; foreach($dates as $date){ echo "<td><input type='text' name='emp[{$row[0]}][{$date->format('Y-m-d')}]' size='2' style='text-align:right;'></td>"; } echo "</tr>\n"; $i++; } ?> </table> </form> Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted September 19, 2013 Author Share Posted September 19, 2013 Thanks mac_gyver. i will try that now. Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted September 19, 2013 Author Share Posted September 19, 2013 (edited) If it is not too much ask mac_gyver, how would you access/catch the input of each textbox? I should be able to insert the input in each box into the database, like this: $sql = "INSERT INTO attendace(att_date, emp_id, work_hrs) VALUES('DateHere', 'EmpIDHere', 'TextboxValueHere')"; Thanks! Edited September 19, 2013 by needs_upgrade Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted September 19, 2013 Solution Share Posted September 19, 2013 foreach ($_POST['emp'] as $empId=>$dateList){ foreach ($dateList as $date=>$textboxValue){ //... } } Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted September 19, 2013 Author Share Posted September 19, 2013 thanks mac_gyver and kicken. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.