company
表,departments
表和imei numbers
表。代码:
$query = mysql_query ("SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' ORDER BY `imei` ASC");
代码:
$query = mysql_query ("SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' AND `depid` = '".$_SESSION['depid']."' ORDER BY `imei` ASC");
IF
循环。$_SESSION['depid']
的会话变量值。$_SESSION['companyid']
将被更改。$_SESSION['depid']
将保留在先前所选部门的下限中。$_SESSION['depid']
不为空,else
部分将再次执行。代码:
$query = mysql_query ("SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' AND `depid` = '".$_SESSION['depid']."' ORDER BY `imei` ASC");
将被执行。
$_SESSION['companyid']
将是第二个选定公司的公司ID,而$_SESSION['depid']
将是之前所选部门的部门ID。$_SESSION['depid']
不属于所选的$_SESSION['companyid']
。unset($_SESSION['depid'])
,但它没有用。如何查看第二选择公司的IMEI号码列表,当时已经选择了公司的部门?
还有其他办法吗?超过IF
循环
整个代码工作
<?php
session_start();
//$_SESSION['companyid'] = $_POST['companyid'];
//$_SESSION['depid'] = $_POST['depid'];
(isset($_POST['companyid']) ? $_SESSION['companyid'] = $_POST['companyid'] : unset($_SESSION['companyid']));
(isset($_POST['depid']) ? $_SESSION['depid'] = $_POST['depid'] : unset($_SESSION['depid']));
include "dbconnect.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
echo "<input type='hidden' id='comp' value='".$_SESSION['companyid']."'/>";
echo "<input type='hidden' id='dept' value='".$_SESSION['depid']."'/>";
echo "<input type='hidden' id='dept2' value='".$_SESSION['depid2']."'/>";
echo "<form method='post'>";
if ($_SESSION['depid'] == 0){
$companyid = $_POST['companyid'];
echo "<select name='companyid' id='companyid' onchange='form.submit()'>";
echo "<option value=''>Select Company</option>";
$query = mysql_query ("SELECT * FROM `company` ORDER BY `companyid` ASC") or die (mysql_error());
while ($row = mysql_fetch_assoc($query)){
echo "<option ".(($_SESSION['companyid'] == $row['companyid'])? 'selected==selected':'')." value='".$row['companyid']."'>".$row['company']."</option>";
}
echo "</select></br>";
$depid = $_POST['depid'];
$query = mysql_query("SELECT `depid`, `department` FROM `department` WHERE `companyid` = '".$_SESSION['companyid']."' ORDER BY `depid` ASC") or die (mysql_error());
echo "</br>";
echo "<select name='depid' id='depid' onchange='form.submit()'>";
echo "<option value=''>Select Department</option>";
while ($row = mysql_fetch_assoc($query)){
echo "<option ".(($_SESSION['depid'] == $row['depid']) ? 'selected==selected':'')." value='".$row['depid']."'>".$row['department']."</option>";
}
echo "</select></br>";
//$query = "SELECT `depid`, `department` FROM `department` WHERE `companyid` = '".$_SESSION['companyid']."'";
//echo $query;
$query = mysql_query("SELECT `depid`, `department` FROM `department` WHERE `companyid` = '".$_SESSION['companyid']."'") or die (mysql_error());
echo "<table>";
echo "<tr>";
echo "<th>IMEI NO.</th>";
echo "</tr>";
echo "</table>";
$query = mysql_query ("SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' ORDER BY `imei` ASC");
echo "<table>";
while ($row = mysql_fetch_assoc($query)){
echo "<tr>";
echo "<td>".$row['imei']."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
$companyid = $_POST['companyid'];
echo "<label>test</label>";
echo "<select name='companyid' id='companyid' onchange='form.submit()'>";
echo "<option value=''>".$row['company']."</option>";
$query = mysql_query ("SELECT * FROM `company` ORDER BY `companyid` ASC") or die (mysql_error());
while ($row = mysql_fetch_assoc($query)){
echo "<option ".(($_SESSION['companyid'] == $row['companyid'])? 'selected==selected':'')." value='".$row['companyid']."'>".$row['company']."</option>";
}
echo "</select></br>";
$depid = $_POST['depid'];
$query = mysql_query("SELECT `depid`, `department` FROM `department` WHERE `companyid` = '".$_SESSION['companyid']."' ORDER BY `depid` ASC") or die (mysql_error());
echo "</br>";
echo "<label>test</label>";
echo "<select name='depid' id='depid' onchange='form.submit()'>";
echo "<option value=''>Select Department</option>";
while ($row = mysql_fetch_assoc($query)){
echo "<option ".(($_SESSION['depid'] == $row['depid']) ? 'selected==selected':'')." value='".$row['depid']."'>".$row['department']."</option>";
}
echo "</select></br>";
//$query = "SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' AND `depid` = '".$_SESSION['depid']."'";
//echo $query;
echo "<table>";
echo "<tr>";
echo "<th>IMEI NO.</th>";
echo "</tr>";
echo "</table>";
$query = mysql_query ("SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' AND `depid` = '".$_SESSION['depid']."' ORDER BY `imei` ASC");
echo "<table>";
while ($row = mysql_fetch_assoc($query)){
echo "<tr>";
echo "<td>".$row['imei']."</td>";
echo "</tr>";
}
echo "</table>";
}
echo "</form>";
?>
</body>
</html>
答案 0 :(得分:0)
首先,您确实应该清理查询并使用mysqli。
更改代码:
$_SESSION['companyid'] = $_POST['companyid'];
$_SESSION['depid'] = $_POST['depid'];
为:
(isset($_POST['companyid']) ? $_SESSION['companyid'] = $_POST['companyid'] : unset($_SESSION['companyid']));
(isset($_POST['depid']) ? $_SESSION['depid'] = $_POST['depid'] : unset($_SESSION['depid']));