Reputation: 181
I currently have a register closing site in use that allows my guys to enter their tips. When I get a new employee though, I have to add them to the database, add their name to the html page that asks for the tip amounts (sometimes for multiple stores/pages), add their name to the php page that submits the values to the database, and I have to add their names to all of the pages that I use for payroll/accounting/taxes.
I was trying to come up with a form that has option inputs using options populated from the "active employee" table, so I only have to enter them there. I have that part working!!
I then wanted to put a box beside the option box that lets them type in the amount of tips each person received.
I'm having issues with that part.
So far I have this, which gives me 5 option inputs with values from the database. I can't figure out the input box for the amounts after though.
<?php
require_once 'loginemployees.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
$res=$conn->query("select Name from Status");
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = $res->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['Name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
echo "Tips: $ <input type="text" name="lname">";
echo "</body>";
echo "</html>";
echo "<br><br>";
$res=$conn->query("select Name from Status");
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = $res->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['Name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
echo "<br><br>";
$res=$conn->query("select Name from Status");
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = $res->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['Name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
echo "<br><br>";
$res=$conn->query("select Name from Status");
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = $res->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['Name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
echo "<br><br>";
$res=$conn->query("select Name from Status");
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = $res->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['Name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
?>
Upvotes: 1
Views: 48
Reputation: 8773
Ok, first of all: That's a lot of unnessesary code! You can shorten it completely like this:
<?php
require_once 'loginemployees.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
/* Put your select element in variable.
Save the values in an array (id).
*/
$select = "<select name='id[]'>";
echo "<!DOCTYPE HTML>";
echo "<html lang='en'>";
echo "<head><meta charset='utf-8'></head>";
echo "<body>";
echo "<form method='POST' action='your_form_handler.php' accept-charset='utf-8'>";
$res=$conn->query("SELECT Name FROM Status");
while ($row = $res->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['Name'];
$select .= '<option value="'.$id.'">'.$name.'</option>';
}
$select .= "</select>";
/* Same here: Save the input values in an array (lname) */
$select .= "Tips: $ <input type='text' name='lname[]'>";
$select .= "<br/><br/>";
/* If you need more forms, just change the number here */
$numberOfForms = 5;
for($i = 0; $i < $numberOfForms; $i++){
echo $select;
}
echo "<input type='submit' value='submit'>";
echo "</form>";
echo "</body>";
echo "</html>";
?>
Now, in your backend when you submit the form, you can simply loop through the arrays:
<?php
if(!empty($_POST['id']) && !empty($_POST['lname'])){
for($i = 0; $i < count($_POST['id']); $i++){
/* This will just display whatever is filled in. Instead you should save it in a database or something */
echo "User with id: ". $_POST['id'][$i] ." had as tip: ". $_POST['lname'][$i] ."<br/>";
}
}
?>
Keep in mind that I haven't tested this because I don't have your database here but it should work just fine.
Upvotes: 2