Jump to content

How to name my textboxes


needs_upgrade
Go to solution Solved by kicken,

Recommended Posts

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 by needs_upgrade
Link to comment
Share on other sites

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;">
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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 by needs_upgrade
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.