我需要帮助,我尝试创建一个分层组织php应用程序,然后我建立一个具有以下结构的数据库:
基本上,我有一份所有员工的名单。 每个员工的行在连续的列(c5到c10)上包含他所依赖的服务的名称(降序层次结构)。 当我从下拉列表中点击特定员工姓名时,我会发送其" id"到应该在两列表中返回的查询:
-1。在第二列中,所选择的员工所依赖的所有服务(按层次结构递减)。 -2。在第一栏中,第一栏中显示了各个部门或服务的管理人员。
要查找服务的名称,例如johana,请求对我来说很容易。
要查找管理员的姓名,例如,对于johana,我必须为她所依赖的服务运行4个不同的请求:
-1。用于营销:查询在字段中返回人名" name"该领域" c5"包含"营销"和#34; c6"是空的。 -2。 for europe:查询在字段中返回人名" name"哪个领域" c6"包含"欧洲"字段c7为空。 -3。用于宣传:查询在字段中返回人名" name"该领域" c7"包含"宣传"字段c8为空。 -4。 for tv:publicity:查询在字段中返回人名" name"该领域" c8"包含" tv"字段c9为空。
PS:对于其他需要,我插入一列表示最后一个单元格的列号不为空,另一列表示该单元格的值。
这是我的代码:
<?php
require_once '../config/dbconnect.php';
if(isset($_POST["prs_id"]) && !empty($_POST["prs_id"])){
$result = $conn->query("SELECT * FROM mytable WHERE id = ".$_POST['prs_id']."");
$rowCount = $result->num_rows;
if($rowCount > 0){
while($row = $result->fetch_assoc()){
// utile variables -------------------
$numcol = $row['lastcol'];
$currcol = 'c' . $row['lastcol'];
$currval = $row['lastval'];
$prevcol = 'c' . ($row['lastcol'] - 1);
$prevval = $row[$prevcol];
$nextcol = 'c' . ($row['lastcol'] + 1);
$nextval = $row[$nextcol];
// -----------------------------------
echo '<tr><td style="width:296px;">';
echo 'query to find manager name level 1'; // returned value of query("SELECT name FROM mytable WHERE c5 = $row['c5'] AND c6 = "" ");
echo '</td><td>';
echo $row['c5'];
echo '</tr><tr><td>';
echo 'query to find manager name level 2'; // returned value of query("SELECT name FROM mytable WHERE c6 = $row['c6'] AND c7 = "" ");
echo '</td><td>';
echo $row['c6'];
echo '</tr><tr><td>';
echo 'query to find manager name level 3'; // returned value of query("SELECT name FROM mytable WHERE c7 = $row['c7'] AND c8 = "" ");
echo '</td><td>';
echo $row['c7'];
echo '</tr><tr><td>';
echo 'query to find manager name level 4'; // returned value of query("SELECT name FROM mytable WHERE c8 = $row['c8'] AND c9 = "" ");
echo '</td><td>';
echo $row['c8'];
echo '</tr><tr><td>';
echo 'query to find manager name level 5'; // returned value of query("SELECT name FROM mytable WHERE c9 = $row['c9'] AND c9 = "" ");
echo '</td><td>';
echo $row['c9'];
echo '</tr><tr><td>';
echo 'employee name clicked in case his lastvalue is in c10';
echo '</td><td>';
echo $row['c10'];
echo '</td></tr>';
}
}
}
&GT;
答案 0 :(得分:0)
我找到了解决问题的方法,所以我将其发布给其他人:
<?php
require_once '../config/dbconnect.php';
if(!empty($_POST["prs_id"])) {
$result = $conn->query("SELECT DISTINCT * FROM mytable WHERE id = '".mysqli_escape_string($conn, $_POST['prs_id'])."' ");
$rowCount = $result->num_rows;
if($rowCount > 0){
while($row = $result->fetch_assoc()){
$numcol = $row['lastcol'];
$currcol = 'c' . $row['lastcol'];
echo '<div>';
if($currcol == 'c5'){
echo '<tr><td>';
$rsl5 = $conn->query("SELECT name FROM mytable WHERE c5 = '".mysqli_escape_string($conn, $row['c5'])."' AND c6 = '' ");
$cnt5 = $rsl5->num_rows;
while($rw5 = $rsl5->fetch_assoc()){
if($cnt5 == 1){
echo $rw5['name'];
} else {
echo $rw5['name'];
echo '<br />';
}
}
echo '</td><td>';
echo $row['c5'];
echo '</td></tr></table></div>';
} else if($currcol == 'c10') {
echo '<tr><td>';
$rsl5 = $conn->query("SELECT name FROM mytable WHERE c5 = '".mysqli_escape_string($conn, $row['c5'])."' AND c6 = '' ");
$cnt5 = $rsl5->num_rows;
while($rw5 = $rsl5->fetch_assoc()){
if($cnt5 == 1){
echo $rw5['name'];
} else {
echo $rw5['name'];
echo '<br />';
}
}
echo '</td><td>';
echo $row['c5'];
echo '</td></tr>';
echo '<tr><td>';
$rsl6 = $conn->query("SELECT name FROM mytable WHERE c5 = '".mysqli_escape_string($conn, $row['c5'])."' AND c6 = '".mysqli_escape_string($conn, $row['c6'])."' AND c7 = '' ");
$cnt6 = $rsl6->num_rows;
while($rw6 = $rsl6->fetch_assoc()){
if($cnt6 == 1){
echo $rw6['name'];
} else {
echo $rw6['name'];
echo '<br />';
}
}
echo '</td><td>';
echo $row['c6'];
echo '</td></tr>';
echo '<tr><td>';
$rsl7 = $conn->query("SELECT name FROM mytable WHERE c6 = '".mysqli_escape_string($conn, $row['c6'])."' AND c7 = '".mysqli_escape_string($conn, $row['c7'])."' AND c8 = '' ");
$cnt7 = $rsl7->num_rows;
while($rw7 = $rsl7->fetch_assoc()){
if($cnt7 == 1){
echo $rw7['name'];
} else {
echo $rw7['name'];
echo '<br />';
}
}
echo '</td><td>';
echo $row['c7'];
echo '</td></tr>';
echo '<tr><td>';
$rsl8 = $conn->query("SELECT name FROM mytable WHERE c7 = '".mysqli_escape_string($conn, $row['c7'])."' AND c8 = '".mysqli_escape_string($conn, $row['c8'])."' AND c9 = '' ");
$cnt8 = $rsl8->num_rows;
while($rw8 = $rsl8->fetch_assoc()){
if($cnt8 == 1){
echo $rw8['name'];
} else {
echo $rw8['name'];
echo '<br />';
}
}
echo '</td><td>';
echo $row['c8'];
echo '</td></tr>';
echo '<tr><td>';
$rsl9 = $conn->query("SELECT name FROM mytable WHERE c8 = '".mysqli_escape_string($conn, $row['c8'])."' AND c9 = '".mysqli_escape_string($conn, $row['c9'])."' AND c10 = '' ");
$cnt9 = $rsl9->num_rows;
while($rw9 = $rsl9->fetch_assoc()){
if($cnt9 == 1){
echo $rw9['name'];
} else {
echo $rw9['name'];
echo '<br />';
}
}
echo '</td><td>';
echo $row['c9'];
echo '</td></tr>';
echo '<tr><td>';
$rsl10 = $conn->query("SELECT name FROM mytable WHERE c9 = '".mysqli_escape_string($conn, $row['c9'])."' AND c10 = '".mysqli_escape_string($conn, $row['c10'])."' ");
$cnt10 = $rsl10->num_rows;
while($rw10 = $rsl10->fetch_assoc()){
if($cnt10 == 1){
echo $rw10['name'];
} else {
echo $rw10['name'];
echo '<br />';
}
}
echo '</td><td>';
echo $row['c10'];
echo '</td></tr></table></div>';
} else {
echo '<tr><td>';
$rsl5 = $conn->query("SELECT name FROM mytable WHERE c5 = '".mysqli_escape_string($conn, $row['c5'])."' AND c6 = '' ");
$cnt5 = $rsl5->num_rows;
while($rw5 = $rsl5->fetch_assoc()){
if($cnt5 == 1){
echo $rw5['name'];
} else {
echo $rw5['name'];
echo '<br />';
}
}
echo '</td><td>';
echo $row['c5'];
echo '</td></tr>';
for ($i=6; $i<=$numcol; $i++) {
$rsl = 'rsl' . $i;
$rw = 'rw' . $i;
$cnt = 'cnt' . $i;
$currcln = 'c' . $i;
$prevcln = 'c' . ($i - 1);
$nextcln = 'c' . ($i + 1);
echo '<tr><td>';
$rsl = $conn->query("SELECT name FROM mytable WHERE ".$prevcln." = '".mysqli_escape_string($conn, $row[$prevcln])."' AND ".$currcln." = '".mysqli_escape_string($conn, $row[$currcln])."' AND ".$nextcln." = '' ");
$cnt = $rsl->num_rows;
while($rw = $rsl->fetch_assoc()){
if($cnt == 1){
echo $rw['name'];
} else {
echo $rw['name'];
echo '<br />';
}
}
echo '</td><td>';
echo $row[$currcln];
echo '</td></tr>';
}
echo '</table></div>';
}
}
}
}
?>