我正在尝试为用户构建一个页面,以便将工作订单添加到项目中。如果项目已存在于数据库中,则与其相关的任何工作订单将列在侧面显示中。我希望根据查询所选项目的最后一个分配号码或者项目中的第一个工单,一个以' -01'为前缀的新号码自动分配新的工单号。
不幸的是,我对项目的第一个工作单没有兴趣,而如果我直接在PHP MyAdmin中运行SQL,我会得到我期望的结果。
非常感谢您的协助。我的代码在下面分享:
HTML:
<?php
include 'core/init.php';
protect_page();
include 'includes/overall/header.php';
//get all project types data
$sql = "SELECT * FROM projects ORDER BY proj_nbr";
$result = mysqli_query($connect,$sql);
$rowCount = mysqli_num_rows($result);
//get all project managers' data
$sql_staff = "SELECT * FROM staff ORDER BY staff_number";
$result_staff = mysqli_query($connect,$sql_staff);
$rowCount_staff = mysqli_num_rows($result_staff);
//generate new work order number
$sql_wo_nbr = "SELECT MAX(wo_nbr) as wo_nbr FROM workorders ";
$result_wo_nbr = mysqli_query($connect,$sql_wo_nbr);
$rowCount_wo_nbr = mysqli_num_rows($result_wo_nbr);
?>
<div class="welcome-headers">
<h1>Welcome</h1>
<h2>Add New Work Order</h2>
</div>
<br>
<?php
if (!empty($_GET['success'])){
$msg = $_GET['success'];
echo $msg;
} else {
}
?>
<div class="forms-add">
<form action="add_workorders.php" method="POST">
<label class="labels-unedit">Work Order Number (Auto-generated):</label>
<input class="inputs-unedit" type="text" id="wo_nbr" name="wo_nbr"size="8"maxlength="8" value = "" readonly>
<!-- <input type="text" id="pj_nbr_hidden" name="pj_nbr_hidden"> -->
<br><br>
<fieldset>
<legend>Project Details</legend>
<label>Project Number:</label>
<select class= "selects" id="proj_nbr" name="proj_nbr" required onchange="getNewWoAndDates(this.value);">
<option value="">Select a project </option>
<?php
//echo "Total number of records found: <strong>$rowCount</strong>.";
if($rowCount>0){
while ($row=mysqli_fetch_assoc($result)){
echo '<option value="'.
$row['proj_id'].'">'.
$row['proj_nbr'].
' - '.
$row['proj_desc'].
' </option>';
}
}else{
echo '<option value="">Project not available</option>';
}
?>
</select>
<br><br>
<label>Start Date:</label>
<input class="inputs" type="text" id="proj_start_date" name="proj_start_date"size="8"maxlength="8" value = "" readonly>
<br><br>
<label>End Date:</label>
<input class="inputs" type="text" id="proj_end_date" name="proj_end_date"size="8"maxlength="8" value = "" readonly>
</fieldset>
<br><br>
<fieldset>
<legend>Work Order Details:</legend>
<label>Description:</label>
<input class="inputs" type="text" id="wo_desc" name="wo_desc"size="50"maxlength="50" required autocomplete="off">
<br><br>
<label>Manager:</label>
<select class="selects" id="wo_mgr_id" name="wo_mgr_id">
<option value="">Select a manager</option>
<?php
//echo "Total number of records found: <strong>$rowCount</strong>.";
if($rowCount_staff>0){
while ($row=mysqli_fetch_assoc($result_staff)){
echo '<option value="'.$row['staff_id'].'">'.
$row['staff_number'].
' - '.$row['first_name'].' '.$row['last_name'].
' </option>';
}
}else{
echo '<option value="">Staff not available</option>';
}
?>
</select>
</fieldset>
<br><br>
<fieldset>
<legend>Work Order Duration:</legend>
<label>Start Date:</label>
<input class="inputs" type="date" id="start_date" name="start_date" required>
<br><br>
<label>End Date:</label>
<input class="inputs" type="date" id="end_date" name="end_date" required >
</fieldset>
<br><br>
<button class="submit-button" type="submit" name="submit" >Save Work Order</button>
</form>
</div>
<div class="side_display" id= "wo_display">
No work orders to display for the selected project.
</div>
</body>
<br><br>
<?php include 'includes/overall/footer.php'; ?>
使用Javascript:
function getNewWoAndDates()
{
var id = $("#proj_nbr").val();
//alert(id);
if (id != '')
{
$.ajax
({
url: "get_proj_nbrs2.php",
method:"POST",
data: { theid : id }, //data to SEND to PHP file
dataType: "JSON",
success: function(result)
{
console.log(result); //returns NULLs instead of actual values
$('#wo_nbr').val(result[0].wo_nbr_new);
$('#proj_start_date').val(result[0].proj_start_date);
$('#proj_end_date').val(result[0].proj_end_date);
//Below code to populate the side display:
var wo_count = result[1].length;
var myDiv = document.getElementById("wo_display");
if (wo_count>0)
{
myDiv.innerHTML = "<table id='newTable'><tr><th>Work Order Number</th><th>Work Order Description</th></tr></table>";
var myTable = document.getElementById("newTable");
for (i=1; i<=wo_count;i++)
{
var myRow = myTable.insertRow(i);
var myCell1 = myRow.insertCell(0);
var myCell2 = myRow.insertCell(1);
myCell1.innerHTML = result[1][i-1].wo_nbr;
myCell2.innerHTML = result[1][i-1].wo_desc;
}
}
else
{
myDiv.innerHTML = "There are no work orders to display for the selected project.";
}
}
});
}else
{
alert("Please select a Project Number");
}
}
PHP:
<?php
include 'connect_db.php';
if (isset($_POST['theid']) && !empty($_POST['theid']))
{
$new_wo = array();
$result = array();
$workorders = array();
$sql2 = "SELECT p.proj_nbr as wo_proj_nbr,p.start_date as proj_start_date,p.end_date as proj_end_date, MAX(w.wo_nbr) AS wo_nbr,
CASE
WHEN SUBSTRING(MAX(w.wo_nbr),7,2)+1 <= 9 THEN CONCAT(p.proj_nbr,'-0',SUBSTRING(MAX(w.wo_nbr),7,2)+1)
WHEN SUBSTRING(MAX(w.wo_nbr),7,2)+1 > 9 THEN CONCAT(p.proj_nbr,'-',SUBSTRING(MAX(w.wo_nbr),7,2)+1)
WHEN w.wo_nbr IS NULL THEN CONCAT(p.proj_nbr,'-','01')
ELSE 'No Work Order'
END AS wo_nbr_new
FROM workorders as w
INNER JOIN projects as p on p.proj_id = w.proj_id
WHERE w.proj_id = '".$_POST['theid']."'";
if($result2 = mysqli_query($connect,$sql2)){
$rowCount = mysqli_num_rows($result2);
while($row=mysqli_fetch_array($result2))
{
$output['proj_start_date'] = $row['proj_start_date'];
$output['proj_end_date'] = $row['proj_end_date'];
$output['wo_nbr_new'] = $row['wo_nbr_new'];
array_push($new_wo,$output);
}
}
$sql_wo = "SELECT p.proj_nbr, p.proj_desc, w.wo_nbr, w.wo_desc
FROM workorders AS w
LEFT JOIN projects AS p ON w.proj_id = p.proj_id
WHERE w.proj_id = '".$_POST['theid']."'
ORDER BY w.proj_id, w.wo_nbr";
if($result_wo = mysqli_query($connect,$sql_wo)){
$rowCount_wo = mysqli_num_rows($result_wo);
//show work orders available for the selected project
while($rowx=mysqli_fetch_array($result_wo))
{
$wo_added = array('wo_nbr'=>$rowx['wo_nbr'],'wo_desc' => $rowx['wo_desc']);
array_push($workorders,$wo_added);
}
}
array_push($result,$new_wo);
array_push($result,$workorders);
echo json_encode($result);
}
?>