㈠ php遞歸樹形菜單
可以參考以下代碼,但需要把你自己的資料庫鏈接,表等改一下就可以了
<html>
<head>
<link href='style.css' rel=stylesheet>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javaScript" src="TreeMenu.js"></script>
</head>
<body>
<?php
//基本變數設置
$GLOBALS["ID"] =1; //用來跟蹤下拉菜單的ID號
$layer=1; //用來跟蹤當前菜單的級數
//連接資料庫
$Con=mysql_connect("localhost","root","1234");
mysql_select_db("wiki");
//提取一級菜單
$sql="SELECT * FROM wiki where pid=0";
$result=mysql_query($sql,$Con);
//如果一級菜單存在則開始菜單的顯示
if(mysql_num_rows($result)>0) ShowTreeMenu($Con,$result,$layer,$ID);
//=============================================
//顯示樹型菜單函數 ShowTreeMenu($con,$result,$layer)
//$con:資料庫連接
//$result:需要顯示的菜單記錄集
//layer:需要顯示的菜單的級數
//=============================================
function ShowTreeMenu($Con,$result,$layer)
{
//取得需要顯示的菜單的項目數
$numrows=mysql_num_rows($result);
//開始顯示菜單,每個子菜單都用一個表格來表示
echo "<table cellpadding='0' cellspacing='0' border='0'>";
for($rows=0;$rows<$numrows;$rows++)
{
//將當前菜單項目的內容導入數組
$menu=mysql_fetch_array($result);
//提取菜單項目的子菜單記錄集
$sql="select * from wiki where pid=$menu[cid]";
$result_sub=mysql_query($sql,$Con);
echo "<tr>";
//如果該菜單項目有子菜單,則添加JavaScript onClick語句
if(mysql_num_rows($result_sub)>0)
{
echo "<td width='20'><img src='folder.gif' border='0'></td>";
echo "<td class='Menu' onClick='javascript:ShowMenu(Menu".$GLOBALS["ID"].");'>";
}
else
{
echo "<td width='20'><img src='file.gif' border='0'></td>";
echo "<td class='Menu'>";
}
//如果該菜單項目沒有子菜單,並指定了超級連接地址,則指定為超級連接,
//否則只顯示菜單名稱
//if($menu[url]!="")
//echo "<a href='$menu[cid]'>$menu[name]</a>";
//else
echo $menu['name'];
echo "
</td>
</tr>
";
//如果該菜單項目有子菜單,則顯示子菜單
if(mysql_num_rows($result_sub)>0)
{
//指定該子菜單的ID和style,以便和onClick語句相對應
echo "<tr id=Menu".$GLOBALS["ID"]++." style='display:none'>";
echo "<td width='20'> </td>";
echo "<td>";
//將級數加1
$layer++;
//遞歸調用ShowTreeMenu()函數,生成子菜單
ShowTreeMenu($Con,$result_sub,$layer);
//子菜單處理完成,返回到遞歸的上一層,將級數減1
$layer--;
echo "</td></tr>";
}
//繼續顯示下一個菜單項目
}
echo "</table>";
}
?>
</body>
</html>
㈡ 學生管理系統php源碼誰有
php學生管理系統源碼,供大家參考,具體內容如下
功能:
1.添加/刪除/修改
2.數據存儲.
界面分布:
index.php
--->主界面
add.php --->stu添加
action ---> sql中add/del/update
(處理html表單-->mysql的數據存儲 && 頁面跳轉)
edit.php --->stu修改
menu.php
-->首頁
1. index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>學生信息管理</title>
<script>
function doDel(id) {
if(confirm('確認刪除?')) {
window.location='action.php?action=del&id='+id;
}
}
</script>
</head>
<body>
<center>
<?php
include ("menu.php");
?>
<h3>瀏覽學生信息</h3>
<table width="500" border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>性別</th>
<th>年齡</th>
<th>班級</th>
<th>操作</th>
</tr>
<?php
// 1. 鏈接資料庫
try{
$pdo = new PDO("uri:mysqlPdo.ini","root","1");
}catch (PDOException $e) {
die('connection failed'.$e->getMessage());
}
//2.執行sql
$sql_select = "select * from stu";
//3.data 解析
foreach ( $pdo->query($sql_select) as $row) {
echo "<tr>";
echo "<th>{$row['id']} </th>";
echo "<th>{$row['name']}</th>";
echo "<th>{$row['sex']} </th>";
echo "<th>{$row['age']} </th>";
echo "<th>{$row['classid']}</th>";
echo "<td>
<a href='edit.php?id={$row['id']}'>修改</a>
<a href='javascript:void(0);' onclick='doDel({$row['id']})'>刪除</a>
</td>";
echo "</tr>";
}
?>
</table>
</center>
</body>
</html>
2. add.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>學生管理系統</title>
</head>
<body>
<center>
<?php include ('menu.php'); ?>
<h3>增加學生信息</h3>
<form action="action.php?action=add" method="post">
<table>
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>年齡</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>性別</td>
<td><input type="radio" name="sex" value="男">男</td>
<td><input type="radio" name="sex" value="女">女</td>
</tr>
<tr>
<td>班級</td>
<td><input type="text" name="classid"></td>
</tr>
<tr>
<!-- <td> </td>-->
<td><a href="index.php">返回</td>
<td><input type="submit" value="添加"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
3. action.php
<?php
/**
* Created by PhpStorm.
* User: hyh
* Date: 16-7-7
* Time: 下午9:37
*/
//1. 鏈接資料庫
try{
$pdo = new PDO("uri:mysqlPdo.ini","root","1");
}catch (PDOException $e) {
// echo 'Connection failed: ' . $e->getMessage();
die('connection failed'.$e->getMessage());
}
//2.action 的值做對操作
switch ($_GET['action']){
case 'add'://add
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$classid = $_POST['classid'];
$sql = "insert into stu (name, sex, age, classid) values ('{$name}', '{$sex}','{$age}','{$classid}')";
$rw = $pdo->exec($sql);
if ($rw > 0){
echo "<script>alter('添加成功');</script>";
}else{
echo "<script>alter('添加失敗');</script>";
}
header('Location: index.php');
break;
case 'del'://get
$id = $_GET['id'];
$sql = "delete from stu where id={$id}";
$rw = $pdo->exec($sql);
if ($rw > 0){
echo "<script>alter('刪除成功');</script>";
}else{
echo "<script>alter('刪除失敗');</script>";
}
header('Location: index.php');
break;
case 'edit'://post
$id = $_POST['id'];
$name = $_POST['name'];
$age = $_POST['age'];
$classid = $_POST['classid'];
$sex = $_POST['sex'];
// echo $id, $age, $age, $name;
$sql = "update stu set name='{$name}', age={$age},sex='{$sex}',classid={$classid} where id={$id};";
// $sql = "update myapp.stu set name='jike',sex='女', age=24,classid=44 where id=17";
print $sql;
$rw = $pdo->exec($sql);
if ($rw > 0){
echo "<script>alter('更新成功');</script>";
}else{
echo "<script>alter('更新失敗');</script>";
}
header('Location: index.php');
break;
default:
header('Location: index.php');
break;
}
4.edit.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>學生管理系統</title>
</head>
<body>
<center>
<?php include ('menu.php');
//1. 鏈接資料庫
try{
$pdo = new PDO("uri:mysqlPdo.ini","root","1");
}catch (PDOException $e) {
die('connection failed'.$e->getMessage());
}
//2.執行sql
$sql_select = "select * from stu where id={$_GET['id']}";
$stmt = $pdo->query($sql_select);
if ($stmt->rowCount() >0) {
$stu = $stmt->fetch(PDO::FETCH_ASSOC); // 解析數據
}else{
die("no have this id:{$_GET['id']}");
}
?>
<h3>修改學生信息</h3>
<form action="action.php?action=edit" method="post">
<input type="hidden" name="id" value="<?php echo $stu['id'];?>">
<table>
<tr>
<td>姓名</td>
<td><input type="text" name="name" value="<?php echo $stu['name'];?>"></td>
</tr>
<tr>
<td>年齡</td>
<td><input type="text" name="age" value="<?php echo $stu['age'];?>"></td>
</tr>
<tr>
<td>性別</td>
<td>
<input type="radio" name="sex" value="男" <?php echo ($stu['sex'] == "男")? "checked":"";?> >男
</td>
<td>
<input type="radio" name="sex" value="女" <?php echo ($stu['sex'] == "女")? "checked":"";?> >女
</td>
</tr>
<tr>
<td>班級</td>
<td><input type="text" name="classid" value="<?php echo $stu['classid']?>"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="更新"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</center>
<?php
?>
</body>
</html>
5. menu.php
<!DOCTYPE html>
<html lang="en">
<body>
<h2>學生管理系統</h2>
<a href="index.php"> 瀏覽學生</a>
<a href="add.php"> 添加學生</a>
<hr>
</body>
</html>
㈢ 如何用PHP根據用戶許可權,顯示對應的菜單想找一下相關的例子,希望各路大佬多多指教
把菜單成一個資料庫表,指定一個鍵。如:
id:1, title:文章管理, icon: document, url: admin/article/index (更多欄位根據需要設置)
id:2, title:產品管理, icon: cube, url: admin/proct/index
id:3, title:系統設置, icon: setting, url: admin/setting/index
然後在管理員表中添加一個varchar(500) (長度根據你的菜單總數估計)或text欄位
在管理員許可權管理中列出所有菜單項,把id作為健值,選中的保存在管理員表的許可權欄位中
格式類似: 1,2,3
然後在輸出菜單的時候進行許可權判斷
//將許可權欄位切割成數組
$perms = explode(',', $user['permission']);
foreach($menus as $menu){
if(in_array($perms, $menu['id'])){
echo '<a href="'. $menu['url'].'" >'.$menu['title'].'</a>';
}
}
然後在每個頁面根據頁面對應的許可權id做一個判斷
//比如在文章頁面
if(!in_array($perms, 1)){
exit('沒有許可權');
}
以上是大體思路,具體根據你的系統設計編寫代碼並對應地優化。
㈣ 誰有詳細php 下拉菜單代碼
這是我自己用的一段代碼,你自己改一下,點一下一級文本框,它所屬的文本框會收起或伸展。
<SCRIPT language=javascript>
<!--
function menu_tree(meval)
{
var left_n=eval(meval);
if (left_n.style.display=="none")
{ eval(meval+".style.display='';"); }
else
{ eval(meval+".style.display='none';"); }
}
-->
</SCRIPT>
<TABLE class=navi cellSpacing=1 align=center border=0>
<TBODY>
<TR>
<TH>後台 >> 新聞分類</TH></TR></TBODY></TABLE><BR>
<table border=0 cellspacing=1 align=center class=form>
<tr>
<th colspan="2">添加分類</th>
</tr>
<form action="" method="post">
<tr>
<td colspan="2" align="center" height='30'>
<select name="fid">
<option value="0">添加大類</option>
<?php
$query=$db->findall("shipin where fid=0");
while ($row=$db->fetch_array($query)){
$new_class_arr[$row[id]]=$row[name];
echo "<option value=\"$row[id]\">$row[name]</option>";
}
?>
</select>
<input type="text" name="name" value="">
<input type="submit" name="into_class" value="添加分類"/>
</td>
</form>
</tr>
</table>
<br>
<table border=0 cellspacing=1 align=center class=form>
<tr>
<th colspan="2">系統分類</th>
</tr>
<?php
foreach ($new_class_arr as $id=>$val){
?>
<tr>
<td onClick ="javascript:menu_tree('left<?=$id ?>');" >
<form action="" method="post">
<IMG src="images/menu.gif" align=absMiddle border=0>
<input type="hidden" name="id" value="<?php echo $id?>">
<input type="text" name="name" value="<?php echo $val?>">
<input type="submit" name="update_class" value="更新"/>
<input type="button" value="刪除" onclick="location.href='?del=<?php echo $id?>'"/>
</form>
</td>
</tr>
<tr id=left<?=$id ?>>
<td>
<table >
<tbody>
<?php
$query_fid=$db->findall("shipin where fid=$id");
while ($row_fid=$db->fetch_array($query_fid)){
?>
<tr>
<td>
<form action="" method="post">
<IMG src="images/menu.gif" align=absMiddle border=0>
<input type="hidden" name="id" value="<?php echo $row_fid[id]?>">
<input type="text" name="name" value="<?php echo $row_fid[name]?>">
<input type="submit" name="update_class" value="更新"/>
<input type="button" value="刪除" onclick="location.href='?del=<?php echo $row_fid[id]?>'"/>
</form>
</td></tr>
<?php }?>
</tbody>
</table>
</td>
</tr>
<?php
}
?>
</table>
</BODY></HTML>