简体   繁体   English

嵌套的PHP开关包含导航

[英]Nested PHP switch include navigation

I'm currently working on a small web project, and my plan is to build it all from the bottom up – to start by making a functioning website which uses only PHP, HTML and CSS, and then, if the user enables it, progressively enhance the website through the magic of Javascript, more particularly - jQuery. 我目前正在研究一个小型Web项目,我的计划是从下至上构建所有内容–首先创建一个仅使用PHP,HTML和CSS的功能正常的网站,然后,如果用户启用,则逐步通过Javascript(尤其是jQuery)的魔力增强网站。 However, I've been running into some problems on the PHP side of things, and since I'm merely novice when it comes to that scripting language, what I can't figure out is simply: 但是,我在PHP方面一直遇到一些问题,由于涉及该脚本语言只是我的新手,所以我不知道的很简单:

How to implement one PHP switch include navigation inside another one? 如何实现一个PHP开关在另一个内部实现导航?

To better clarify what I'm trying to achieve, let me show you a little mock up of what my site structure looks like: 为了更好地阐明我要实现的目标,让我向您展示一些网站结构的模拟模型:

  • Header.php (Header) Header.php(标题)
  • Page1.php (Content page 1) Page1.php(内容页面1)
  • Page2.php (Content page 2) Page2.php(内容页面2)
  • Page3.php (Content page 3) Page3.php(内容页面3)
  • Page3.1.php (Content page 3 sub-content 1) Page3.1.php(内容页面3子内容1)
  • Page3.2.php (Content page 3 sub-content 2) Page3.2.php(内容页面3子内容2)
  • Page3.3.php (Content page 3 sub-content 3) Page3.3.php(内容页面3子内容3)
  • Footer.php (Footer) Footer.php(页脚)

As you can see, when it comes to this mock up, it's on page 3 where I intend to nest one php navigation inside another one. 如您所见,当涉及到这种模拟时,它在第3页上,我打算将一个php导航嵌套在另一个php导航中。 This to enable some slideresque sub-navigation on that very page, but my efforts in doing that have so far been futile at best. 这样可以在该页面上进行一些类似滑子的子导航,但是到目前为止,我所做的努力充其量是徒劳的。 As far as code goes, this is what I've managed to scrap together. 就代码而言,这就是我设法拼凑的东西。

<a class="link" href="index.php?id=page1">Page1</a>
<a class="link" href="index.php?id=page2"> Page2</a>
<a class="link" href=” index.php?id=page3">Page3</a>

<?php

  switch($_GET['id']) {
  default:
  include('page1.php');

  break; case "page2":
  include('page2.php');

  break; case "page3":
  include('page3.php');  // *Nested navigation goes inside this page
  }
?>

*Inside of page3.php I've put pretty much the same code, only that it links to the sub-pages of that page (page3.1.php, page3.2, and page3.3.php ) instead. *在page3.php内,我放置了几乎相同的代码,只是它链接到该页面的子页面(page3.1.php,page3.2和page3.3.php)。

Unfortunately, this doesn't seem to work – first of all, when I enter localhost/index.php I my web browser I get the following notice: Notice: Undefined index: id in C:\\xampp\\htdocs\\php-navigation\\index.php on line 26. However, when I click one of the links, or enter a url like index.php?id=page1 I don't get that notice. 不幸的是,这似乎不起作用–首先,当我在Web浏览器中输入localhost / index.php时,我收到以下通知:注意:未定义的索引:C:\\ xampp \\ htdocs \\ php-navigation \\中的id第26行的index.php。但是,当我单击链接之一或输入诸如index.php?id = page1之类的URL时,我没有得到通知。 So it seems like I need to define the 'id' variable, but where and how to do that? 看来我需要定义'id'变量,但是在哪里以及如何做到这一点?

In addition to that, the nested navigation on page 3 doesn't work at all, instead, when clicking on of the links to the sub-pages, I get redirected to the index page. 除此之外,第3页上的嵌套导航根本不起作用,相反,当单击子页面上的链接时,我被重定向到索引页面。 I might have to do with the URL:s , but then again, what do I know? 我可能与URL:s有关,但是我又知道什么呢?

Please help me figure out how to make this work - all help, even just a slight nudge in the right direction, is welcome and much appreciated- thanks in advance! 请帮助我弄清楚如何进行这项工作-我们欢迎您提供所有帮助,即使只是朝正确的方向轻轻一点,也非常感谢-谢谢!

/ Johan Wendesten /约翰·温德斯滕

my freind english is not my first language so i am reading it again, but one mistake i found in your code is this. 我的英语不是我的母语,所以我又读了一次,但是我在您的代码中发现的一个错误是这样的。

for the page three thats why you are having problem for the page3 link 对于第三页,这就是为什么您在page3链接上遇到问题的原因

<a class="link" href=” index.php?id=page3">Page3</a>

the href part is wrong this one href=” it should be this href=" means your href part should be this href部分是错误的,这个href =“”应该是href =”,意味着您的href部分应该是this

href="index.php?id=page3"

and to remove the error for id 并删除ID的错误

add if statement like this 像这样添加if语句

if($_REQUEST=='GET'){ //all of your switch statement will come here }

so your code should look like this 所以你的代码应该看起来像这样

<?php

//start of if statement  
if($_REQUEST=='GET'){


  switch($_GET['id']) {
  default:
  include('page1.php');

  break; case "page2":
  include('page2.php');

  break; case "page3":
  include('page3.php');  // *Nested navigation goes inside this page
  }

}//end of if statement

The warning you get is about an undefined index - you are trying to access an element of an array that does not exist. 您收到的警告是有关未定义索引的-您正在尝试访问不存在的数组元素。 You need to change 你需要改变

switch($_GET['id']) {

to

if (isset($_GET['id'])) {
    $id = $_GET['id'];
} else {
    $id = "page1";
}

switch($id) {

For page 3, first off you may have an issue here: 对于第3页,首先您可能在这里遇到问题:

<a class="link" href=” index.php?id=page3">Page3</a>

You'll notice an extra space and a weird quote ( vs " - you want " ). 您会注意到一个多余的空格和一个奇怪的引号( vs " -您想要" )。

For the subpages, we'd have to see the URL's generated, but I'm guessing it has to do with the links as well - to keep with the way you are doing it, I would probably have the link look like: 对于子页面,我们必须看到URL的生成,但是我想它也与链接有关-为了与您的工作方式保持一致,我可能希望链接看起来像:

<a class="link" href="index.php?id=page3&subid=2">Subcontent 2</a>

and then in the php: 然后在php中:

if (isset($_GET['subid'])) {
    $subid = $_GET['subid'];
} else {
    $subid = 1;
}

switch($subid) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM