自动化 PHP 包括
Automatize PHP include
我正在寻找一种方法来自动化我的 php 脚本,它应该在名为 SUBPAGES
的文件夹中搜索 php 文件,并将 php 文件包含在Dropdown
的帮助,就像我的例子,但每次我添加一个新文件时都没有重新编码。
它应该自己找到存储在 SUBPAGES
文件夹中的文件,并自动包含所选文件。
谁能帮我解决一下。
<html>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<div align="center">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="100%">
<tr>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0" width="" height="">
<tr>
<td width="300" height="50" bgcolor="#F2F2F2">
<p align="center">
<form name="form">
<p align="center">
<select name="link" SIZE="1" onChange="window.location.href = document.form.link.options[document.form.link.selectedIndex].value;">
<option value="#" style="display:none">Choose</option>
<option value="index.php?id=page1"> Seite1 </option>
<option value="index.php?id=page2"> Seite2 </option>
</select></p>
</form>
</td>
</tr>
<tr>
<td height="20" bgcolor="#999999"> </td>
</tr>
<tr>
<td height="350">
<?php
error_reporting(0);
switch($_GET['id']) {
default:
include('Subpages/page1.php');
break; case "page2":
include('Subpages/page2.php');
}
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>
简单的方法。首先检查安全类型,include
之后
$page = 'Subpages/page'.settype($_GET['id'],'integer').'.php' ;
$defaultpage = 'Subpages/page1.php' ;
if (file_exists($page)) {
include($page);
}else{
include($defaultpage);
}
使用 jquery 和加载函数更容易。您只需要读取所选选项的值并加载您需要的文件
<div align="center">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="100%">
<tr>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0" width="" height="">
<tr>
<td width="300" height="50" bgcolor="#F2F2F2">
<p align="center">
<form name="form">
<p align="center">
<select name="link" SIZE="1">
<option value="#" style="display:none">Choose</option>
<option value="page1"> Seite1 </option>
<option value="page2"> Seite2 </option>
</select>
</p>
</form>
</td>
</tr>
<tr>
<td height="20" bgcolor="#999999"> </td>
</tr>
<tr>
<td height="350" id="result"></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$('select').on('change', function() {
$( "#result" ).load( "Subpages/" + $(this).val() + ".php" );
});
</script>
</body>
我正在寻找一种方法来自动化我的 php 脚本,它应该在名为 SUBPAGES
的文件夹中搜索 php 文件,并将 php 文件包含在Dropdown
的帮助,就像我的例子,但每次我添加一个新文件时都没有重新编码。
它应该自己找到存储在 SUBPAGES
文件夹中的文件,并自动包含所选文件。
谁能帮我解决一下。
<html>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<div align="center">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="100%">
<tr>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0" width="" height="">
<tr>
<td width="300" height="50" bgcolor="#F2F2F2">
<p align="center">
<form name="form">
<p align="center">
<select name="link" SIZE="1" onChange="window.location.href = document.form.link.options[document.form.link.selectedIndex].value;">
<option value="#" style="display:none">Choose</option>
<option value="index.php?id=page1"> Seite1 </option>
<option value="index.php?id=page2"> Seite2 </option>
</select></p>
</form>
</td>
</tr>
<tr>
<td height="20" bgcolor="#999999"> </td>
</tr>
<tr>
<td height="350">
<?php
error_reporting(0);
switch($_GET['id']) {
default:
include('Subpages/page1.php');
break; case "page2":
include('Subpages/page2.php');
}
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>
简单的方法。首先检查安全类型,include
之后$page = 'Subpages/page'.settype($_GET['id'],'integer').'.php' ;
$defaultpage = 'Subpages/page1.php' ;
if (file_exists($page)) {
include($page);
}else{
include($defaultpage);
}
使用 jquery 和加载函数更容易。您只需要读取所选选项的值并加载您需要的文件
<div align="center">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="100%">
<tr>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0" width="" height="">
<tr>
<td width="300" height="50" bgcolor="#F2F2F2">
<p align="center">
<form name="form">
<p align="center">
<select name="link" SIZE="1">
<option value="#" style="display:none">Choose</option>
<option value="page1"> Seite1 </option>
<option value="page2"> Seite2 </option>
</select>
</p>
</form>
</td>
</tr>
<tr>
<td height="20" bgcolor="#999999"> </td>
</tr>
<tr>
<td height="350" id="result"></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$('select').on('change', function() {
$( "#result" ).load( "Subpages/" + $(this).val() + ".php" );
});
</script>
</body>