关于菜单和菜单的简单 PHP 问题内容最佳实践

发布于 2024-10-17 12:30:34 字数 571 浏览 3 评论 0原文

好的,这是我第一次构建一个完整的网站并使用 php。现在我遇到一个问题:

假设我的网站只有一个带有菜单的标题和一个包含内容的区域。当然,我想要一个 header.php 和几个内容文件,例如 content1.php content2.php 和 content3.php 。这样,您只需更改 1 个文件中的菜单即可,如您所知。

那么,最好如何构建网站:

A. 在每个内容文件中放入类似的内容:

  <?php include 'header.php'; ?>

  here the content of the content page 1

B. 制作一个索引文件,其中包含以下内容:

   <?php include 'header.php'; ?>

   <?php include 'content1.php'; ?>

那么当在菜单中单击 content2.php 的链接时,如何做到这一点?标题也仍在该页面上吗?

C、还有别的事吗?也许有一个关于如何制作此类页面的好教程?

OK, so for the first time I am building a full website and working with php. Now I encounter a problem:

Let'ss say my site is simply has a header with a menu and a area with content. Of course, I would like to have a header.php and several content files like content1.php content2.php and content3.php for example. This way you only have to change the menu in 1 file as you would understand.

How then is it best to build the site:

A. put something like this in every content file:

  <?php include 'header.php'; ?>

  here the content of the content page 1

B. make an index file with something like:

   <?php include 'header.php'; ?>

   <?php include 'content1.php'; ?>

how then is it done that when in the menu the link to content2.php is clicked the header is still on that page too?

C. something else? Maybe a good tutorial on how to make these kind of pages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

℉絮湮 2024-10-24 12:30:34

如果您希望最大化重用代码/元素的好处,那么您的第二个选择是正确的:

B.创建一个索引文件,如下所示:

<?php include 'header.php'; ?>
<?php include 'content1.php'; ?>

那么当在菜单中单击 content2.php 的链接时,标题仍然在该页面上,这是怎么做到的?

操作方法如下(一个简单的示例):

使用查询字符串 - mod_rewrite 通过 index.php 脚本路由所有类似的请求 (content1,2,3) 可以让这个变得漂亮。然后根据请求提供主要内容部分。

例如链接:

<a href='index.php?page=content1'>Content 1</a>

并检测要提供的内容:

<php
    $pages['content1'] = 'content1.php';
    $pages['content2'] = 'content2.php';

    $pages['default'] = $pages['content1']; //set default content

    $page = 'default';
    if(isset($pages[$_GET['page']]){
        $page = $pages[$_GET['page']]; //make sure the filename is clean
    }

?>
<?php include 'header.php'; //header here?>
<?php include $page; //correct content here?>

不仅有一个地方可以更改标题,而且现在有一个地方可以更改整个布局。

当然,这只是一个简单的示例,有许多 PHP 框架可以为您完成这一切(使用 MVC)。

If you're looking to maximize the benefit of reusing code/elements, then you're on the right track with your second option:

B. make an index file with something like:

<?php include 'header.php'; ?>
<?php include 'content1.php'; ?>

how then is it done that when in the menu the link to content2.php is clicked the header is still on that page too?

Here's how (a simplistic example):

Route all of the similar requests (content1,2,3) through your index.php script using the query string - mod_rewrite can make this pretty. Then serve the main content section based on the request.

For example a link:

<a href='index.php?page=content1'>Content 1</a>

And detecting the content to serve:

<php
    $pages['content1'] = 'content1.php';
    $pages['content2'] = 'content2.php';

    $pages['default'] = $pages['content1']; //set default content

    $page = 'default';
    if(isset($pages[$_GET['page']]){
        $page = $pages[$_GET['page']]; //make sure the filename is clean
    }

?>
<?php include 'header.php'; //header here?>
<?php include $page; //correct content here?>

Not only is there a single place to change your header, but now there's a single place to change your entire layout.

Of course this is just a simplistic example, there are many PHP frameworks that do all this for you (using MVC).

笨死的猪 2024-10-24 12:30:34

简单而不是包含内容文件的index.php,您可以这样做:

header.php:

 <a href="content1.php">content 1</a><br />
 <a href="content2.php">content 2</a><br />

content2.php:

<?php
    include('header.php');
?>

This is the page with 'content 1'

content2.php:

<?php
    include('header.php');
?>

This is the page with 'content 2'

Simple instead of a index.php including the content files, you do:

header.php:

 <a href="content1.php">content 1</a><br />
 <a href="content2.php">content 2</a><br />

content2.php:

<?php
    include('header.php');
?>

This is the page with 'content 1'

content2.php:

<?php
    include('header.php');
?>

This is the page with 'content 2'
逆流 2024-10-24 12:30:34

但是,如果我有 1000 个内容页面,是否有办法以不同的方式做到这一点?就像十年前的框架一样。

简短的回答,不。对于小型网站来说,最快的方法是使用@mark b 描述的标头方法。它不仅速度快,而且还允许您依赖 URL 路径,这对 SEO 有利。

也许答案很长。框架本质上已被 dom 所取代,其中将内容分配给 div 并进行更新。它速度快,比旧框架更容易控制,并且可以异步完成(通过 AJAX)。但是,它也比旧的 target= 功能需要处理更多的工作。客户喜欢它,但编码确实需要时间。事实上,如果您正在处理大量页面和大量内容,那么这会花费大量时间。

另一种选择是使用 Zend Framework 等模板系统将视图构建到统一的模板中进行显示。但是,它会增加相当大的框架开销。

最后,由于您可以使用 PHP,因此您可以从数据库中提取所有内容,并在一个“页面”上运行整个网站。 URL 字符串将包含一个参数,告诉它从数据库中提取哪些内容并在通用“getter”页面中替换。一个真正的 CMS 系统。

祝你好运。

But if i were to have 1000's of content pages, isn't there a way to do it differently? like it was 10 years ago with frames.

Short answer, no. The quickest way for a small website would be to use the header method that @mark b describes. Not only is it fast, but it also allows you to rely on URL paths which can be beneficial to SEO.

Long answer, maybe. Frames have essentially been replaced by use of the dom, where one assigns content to a div and does an update. It's fast, much easier to control than the old frames were, and can be done asynchronously (via AJAX) However, it's also a lot more work to deal with than the old target= functionality. Clients love it, but it does take time to code it. Significant time, in fact, if you're dealing with a ton of pages and lots of content.

Another option would be to use a templating system like Zend Framework to build your views into a unified template for display. But, it would add the overhead of a framework which can be sizable.

Finally, since you have PHP at your disposal, you could just pull all the content from the DB and essentially run the entire site on one "page". The URL string would contain a parameter that tells it what content to pull from the DB and replace in your generic "getter" page. A true CMS system of sorts.

Good luck.

魔法唧唧 2024-10-24 12:30:34

选项 A 减少了文件数量,但随着您的网站设计变得越来越复杂,您可能需要查看 Smarty< /a>,因为这将页面的“视图”与“数据”完全分开。另外,它非常强大并且是作为一个框架而建立的。

Option A reduces your number of files, though as you're getting more and more complex in your site design, you may want to look at Smarty, as that completely separates the "view" from the "data" of the page. Plus it's very robust and established as a framework.

感受沵的脚步 2024-10-24 12:30:34

您可以使用 phps __autoload()。这听起来有点脏,但是类包装了您的内容,然后在每个文件的末尾有一个访问器。

每个文件看起来都是这样的:

class header
{
  public static function getContent() {
    //return your content
}

header::getContent();

You could use phps __autoload(). It sounds kind of dirty, but but class wrappers around your content, then a single accessor at the end of each file.

Each file would look something like:

class header
{
  public static function getContent() {
    //return your content
}

header::getContent();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文