|
查看: 1080|回复: 16
|
PHP MENU的问题
[复制链接]
|
|
|
各位好,我是个初学者,想问一个问题如果我用PHP写了一些MENU的CODE 如: $MENU[0]="DATA";把MENU这页连接到INDEX.PHP的页里用INCLUDE.我想要的效果是当我在INDEX那页按我的MENU其中一个MENU,那个页就会显示在同一页里,有如FRAME的效果但我要用PHP来写
请问我在MENU那个页里要怎么加哪些其他连接的网页呢?
请指教
[ 本帖最后由 Pool 于 17-3-2008 08:52 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 17-3-2008 02:12 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 17-3-2008 07:18 PM
|
显示全部楼层
原帖由 yhchan 于 17-3-2008 02:12 PM 发表 
不很明白,请再详细解释一下。。。
对不起,可能我表达有问题. 下面是我的MENU.PHP, 想问还要加什么CODES可以连接到DATA1,2,3,4 的资料呢?我把这个MENU.PHP加在INDEX那一页,在INDEX那页它可以显示出DATA1,2,3,4, 当我按DATA2是,它就会显示DATA2的资料,请问要如何写.
$menu[0]="DATA1"
$menu[1]="DATA2"
$menu[2]="DATA3"
$menu[3]="DATA4"
谢谢 |
|
|
|
|
|
|
|
|
|
|
发表于 18-3-2008 01:55 PM
|
显示全部楼层
是不是说,你的Menu.php有一个Array, Data1, Data2...是网址 (Data1.php....)
当你按Data1时,你要在index.php显示Data1.php的资料? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 19-3-2008 11:09 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 21-3-2008 01:10 PM
|
显示全部楼层
我觉得最简单的方法是用GET及include洛。
假设你的$menu是:
$menu[0] = 'page1.php';
$menu[1] = 'page2.php';
所以你的Links会是:
<a href="index.php?link=0">Menu 1</a>
<a href="index.php?link=1">Menu 2</a>
...
...
用$_GET['link']来决定要include哪一个page。 |
|
|
|
|
|
|
|
|
|
|
发表于 21-3-2008 01:20 PM
|
显示全部楼层
类似:
<?php
// Array of menu
$menu = array('page1.php', 'page2.php');
if (!empty($_GET)) {
// If something is posted by GET
$menu_number = $_GET['link'];
} else {
// nothing is posted by GET
$menu_number = -1;
}
for ($i = 0; $i < count($menu); $i++) {
echo '<a href="index.php?link=' . $i . '">Menu ' . ($i + 1) . '</a><br>';
}
if ($menu_number >= 0) {
// Show the contect
include $menu[$menu_number];
}
?>
不知你是不是想酱吗? |
|
|
|
|
|
|
|
|
|
|
发表于 22-3-2008 04:28 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 23-3-2008 08:53 AM
|
显示全部楼层
原帖由 yhchan 于 21-3-2008 01:20 PM 发表 
类似:
不知你是不是想酱吗?
就是要这个方法, 我的MENU有不通的名字,请问要怎样换呢? 就是说我把你这个CODES LINK在INDEX那样,它出来是
MENU1
MENU2
MENU3
MENU4
MENU5
如果我要它显出的是
DATA
INSERT
UPDATE
DELETE
SELECT
那要在MENU.PHP加什么呢?
还有一个问题,以下就是INDEX的页,我把MENU.PHP加在第一页,但当我按MENU1时,它会跳出一个新WINDOWS,要怎样出现在"2"那边呢?
谢谢
-----------------------------------------------------------------
| |
| |
1 | 2 |
| |
------------------------------------------------------------------ |
|
|
|
|
|
|
|
|
|
|
发表于 24-3-2008 10:56 AM
|
显示全部楼层
你可以在Menu.php用Pair Array,方法如下:
$menu = array('Data' => 'page1.php', 'Insert' => 'page2.php');
然后用foreach:
foreach ($menu AS $key => $val) {
echo '<a href="index.php?link=' . $val . '">' . $key . '</a><br>';
}
所以你的$_GET['link']会得到link=page1.php。直接include就行了。
if (!empty($_GET)) {
include $_GET['link'];
}
至于要分左右,你可以用Frame。请参考这个:
http://www.w3schools.com/html/html_frames.asp
(Navigation Frame的部分)
或者用Table洛。
<table>
<tr>
<td>
<?php
foreach ($menu AS $key => $val) {
echo '<a href="index.php?link=' . $val . '">' . $key . '</a><br>';
}
?>
</td>
<td>
<?php
if (!empty($_GET)) {
include $_GET['link'];
}
?>
</td>
</tr>
</table>
大概是这样,你可以找找Table的html用法... |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 28-3-2008 11:50 AM
|
显示全部楼层
真的谢谢yhchan, 成功的做了我要那个方法.
现在我INDEX那页是MENU LIST在左边,资料显示再右边,但按DATA(MENU LIST), 资料就显示到右边了.
但还有一个问题, 比如我按CONTACT(MENU LIST), 那个FORM就显示在右边,但当我按SUBMIT BUTTON时或其它BUTTON时, 第二页显示无法连接我那个写好的PHP + DATABASE 的页, 我在FORM的ACTION 那边已经LINK了我写好的PHP连接MySQL的PHP页.
请问还要哪里改改呢? |
|
|
|
|
|
|
|
|
|
|
发表于 28-3-2008 12:48 PM
|
显示全部楼层
嗯,不很明白。请再解释详细一点。
你用Table还是Frame?
假设你按Contact的Link是,你会显示contact.php。button及<form>都是在contact.php里的。<form>的action是action.php。。。
如果你用Table,那当你submit button时,整个Page应该会Reload去action.php。。。
如果你用Frame,那做边的menu是不会变的。只是右边的Frameset会reload到action.php。
你的问题是什么呢? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 31-3-2008 06:18 AM
|
显示全部楼层
<?php
$strAction = $_GET["txtdatabaseaction"];
if ($strAction == "Inserting")
include ("GradeTemplatesInsert.php");
if ($strAction == "Updating")
include ("GradeTemplatesUpdate.php");
if ($strAction == "Deleting")
include ("GradeTemplatesDelete.php");
If ($strAction == "Selecting")
include ("GradeTemplatesSelect.php");
?>
<head>
<script language="javascript">
function whichButton(pwhichbutton)
{
document.forms[0].txtdatabaseaction.value = pwhichbutton;
document.forms[0].link.value = "../Grade Templates/GradeTemplatesInsert.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesUpdate.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesDelete.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesSelect.php";
alert("Thank You For " + pwhichbutton + " Data")
//summit to server
document.forms[0].submit();
}
</script>
</head>
<!-- examid, description, datetaken, gradetypeid -->
<body>
<h1>Grade Templates</h1>
<form action ="<?php $strAction ?>">
<table>
<input type="hidden" name="link"> <br>
<input type="text" name="txtdatabaseaction"><br>
<tr>
<td>Exam ID</td>
<td><input type="text" name="txtexamid"> </td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="txtdescription"> </td>
</tr>
<tr>
<td>Date Taken</td>
<td> <input type="text" name="txtdatetaken"> </td>
</tr>
<tr>
<td>Grade Type ID</td>
<td><input size="50" type="text" name="txtgradetypeid"> </td>
</tr>
<tr>
<td><input type="button" value="Insert">
<input type="button" value="Update"></td>
<td><input type="button" value="Delete">
<input type="button" value="Select"></td>
</tr>
</table>
</form>
<script language="javascript">
/*Document Object Model */
document.forms[0].txtexamid.value="<?php echo $strExamId ?>"
document.forms[0].txtdescription.value="<?php echo $strDescription ?>"
document.forms[0].txtdatetaken.value="<?php echo $strDateTaken ?>"
document.forms[0].txtgradetypeid.value="<?php echo $strGradeTypeId ?>"
</script> |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 31-3-2008 06:55 AM
|
显示全部楼层
上面是我写的codes
但我不清楚以下这个CODES是对还是错, 我要的效果是当我按INSERT BUTTON是就会LINK到GradeTemplatesInsert.php
所以我就加了以下的JAVASCRIPT的FUNCTION;
function whichButton(pwhichbutton)
{
document.forms[0].txtdatabaseaction.value = pwhichbutton;
document.forms[0].link.value = "../Grade Templates/GradeTemplatesInsert.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesUpdate.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesDelete.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesSelect.php";
alert("Thank You For " + pwhichbutton + " Data")
//summit to server
document.forms[0].submit();
}
问题是,当我开始只是写两行的时候来测试, 我按INSERT BUTTON是它就会把资料成功的SEND到DATABASE里,我就接着写了完全部如以上的那样,但当我再次按全部的BUTTONS包括INSERT,按了只好变了没有东西(空白的一页),如果我不加
document.forms[0].link.value = "../Grade Templates/GradeTemplatesUpdate.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesDelete.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesSelect.php";
只是写成
function whichButton(pwhichbutton)
{
document.forms[0].txtdatabaseaction.value = pwhichbutton;
document.forms[0].link.value = "../Grade Templates/GradeTemplatesInsert.php";
alert("Thank You For " + pwhichbutton + " Data")
//summit to server
document.forms[0].submit();
}
</script>
在按INSERT是,它就会变了第二页,在那页会出现THANK YOU这样的字, 因为我在GradeTemplatesInsert.php里的最后加了这个THANK YOU的字, 那很明显如果我的到这个字,就是说我的资料已经成功的SEND到DATABASE了, 但为什么加了其他的CODES就没有这个效果呢?
是不是我漏了一些东西?
谢谢 |
|
|
|
|
|
|
|
|
|
|
发表于 31-3-2008 11:24 AM
|
显示全部楼层
document.forms[0].link.value = "../Grade Templates/GradeTemplatesInsert.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesUpdate.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesDelete.php";
document.forms[0].link.value = "../Grade Templates/GradeTemplatesSelect.php";
这里有点问题。你没用If Else,那肯定link.value 会是GradeTemplatesSelect.php。
我觉得可以更简单,不用用Javascript。
1) 每个button写成type="submit"
<input type="submit" value="test" NAME="submit">
<input type="submit" value="Delete" name="submit">
<input type="submit" value="Insert" name="submit">
<input type="submit" value="Select" name="submit">
Form submit过后,只需拿回$_GET['submit']就可以了。
$action = $_GET['submit'];
if ($action == "Delete") {
include ("GradeTemplatesDelete.php");
} elseif ($action == "Update") {
...
..
...
如果你要用javascript得话,那必须在每个button的onclick加上:
<input type="button" value="Update" onclick="whichButton(this);">
<input type="button" value="Delete" onclick="whichButton(this);">
...
然后在javascript里:
function whichButton(object)
{
document.forms[0].txtdatabaseaction.value = object.value;
if (object.value == 'Delete') {
document.forms[0].link.value = "../Grade Templates/GradeTemplatesDelete.php";
} else if (object.value == 'Update') {
document.forms[0].link.value = "../Grade Templates/GradeTemplatesUpdate.php";
}
...
...
alert("Thank You For " + object.value + " Data")
//summit to server
document.forms[0].submit();
}
应该可以做到你要的效果。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-4-2008 09:21 AM
|
显示全部楼层
我照着你的JAVASCRIPT方法做,但只是DELETE 和UPDATE可以用. 还有哪里要改呢?
<?php
$strAction = $_GET["txtdatabaseaction"];
if ($strAction == "Inserting")
include ("CourseInsert.php");
if ($strAction == "Updating")
include ("CourseUpdate.php");
if ($strAction == "Deleting")
include ("CourseDelete.php");
if ($strAction == "Selecting")
include ("CourseSelect.php");
?>
<head>
<script language="javascript">
function whichButton(object)
{
document.forms[0].txtdatabaseaction.value = object.value;
if (object.value == 'Insert') {
document.forms[0].link.value = "../Course/CourseInsert.php";
} else if (object.value == 'Update') {
document.forms[0].link.value = "../Course/CourseUpdate.php";
}
else if (object.value == 'Delete') {
document.forms[0].link.value = "../Course/CourseDelete.php";
}
else if (object.value == 'Select') {
document.forms[0].link.value = "../Course/CourseSelect.php";
}
alert("Thank You For " + object.value + " Data")
//summit to server
document.forms[0].submit();
}
</script>
</head>
<body>
<h1>Course</h1>
<form action ="<?php $strAction ?>">
<table>
<input type="hidden" name="link"> <br>
<input type="text" name="txtdatabaseaction"><br>
<tr>
<td>Course ID</td>
<td><input type="text" name="txtcourseid"></td>
</tr>
<tr>
<td>Credits</td>
<td><input type="text" name="txtcredits"></td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="txtdescription"> </td>
</tr>
<tr>
<td>
<input type="button" value="Insert" >
<input type="button" value="Update" >
</td>
<td>
<input type="button" value="Delete" >
<input type="button" value="Select" >
</td>
</tr>
</form>
<script language="javascript">
/*Document Object Model */
document.forms[0].txtcourseid.value="<?php echo $strCourseId ?>"
document.forms[0].txtcredits.value="<?php echo $intCredit ?>"
document.forms[0].txtdescription.value="<?php echo $strDescription ?>"
</script>
</body>
</html> |
|
|
|
|
|
|
|
|
|
|
发表于 1-4-2008 10:58 AM
|
显示全部楼层
试试这样:
<?php
$strAction = $_GET["submit"];
if ($strAction == "Insert") {
//include ("CourseInsert.php");
echo 'Inserting';
}
if ($strAction == "Update") {
//include ("CourseUpdate.php");
echo 'Updating';
}
if ($strAction == "Delete") {
//include ("CourseDelete.php");
echo 'Deleting';
}
if ($strAction == "Select") {
//include ("CourseSelect.php");
echo 'Selecting';
}
?>
<head>
</head>
<body>
<h1>Course</h1>
<form action ="<?php echo $PHP_SELF; ?>" method="GET">
<table>
<input type="hidden" name="link"> <br>
<input type="text" name="txtdatabaseaction"><br>
<tr>
<td>Course ID</td>
<td><input type="text" name="txtcourseid"></td>
</tr>
<tr>
<td>Credits</td>
<td><input type="text" name="txtcredits"></td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="txtdescription"> </td>
</tr>
<tr>
<td>
<input type="submit" value="Insert" name="submit">
<input type="submit" value="Update" name="submit">
</td>
<td>
<input type="submit" value="Delete" name="submit">
<input type="submit" value="Select" name="submit">
</td>
</tr>
</form>
</body>
</html>
根据你click的button来include不同的page。较简单,而且不需要javascript。然后form会post给自己($PHP_SELF)。
希望是你要的效果。。。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|