简体   繁体   English

使用PHP自动添加新的帖子链接

[英]Using PHP to automatically add new post links

I'm certain I can use PHP to accomplish this task, but I'm not sure how. 我确定我可以使用PHP完成此任务,但是我不确定如何执行。 What I currently have is a faux news (ha ha ho ho) site for practise here. 我目前拥有的是一个在这里练习的人造新闻(ha ha ho ho)网站。

http://puu.sh/402Rl.png http://puu.sh/402Rl.png
For Browse News, I would like all html documents within a specified folder to be shown in the format I have 对于“浏览新闻”,我希望指定文件夹中的所有html文档以我具有的格式显示

SAMPLE 样品

<p class="content centeralign">
8.12.13 <!-- ARTICLE NAME -->
</p>
<hr noshade></hr>

Although it's not much, setting up a way to do this automatically would save some time. 尽管数量不多,但是设置一种自动执行此操作的方法将节省一些时间。

Here is how I would imagine the logistics behind this would function ---- 我想像一下这背后的物流将发挥作用----

All HTML files will be listed in a folder 所有HTML文件将列在一个文件夹中

They will all have a consecutive order based on the date they were created (eg 1.html, 2.html, 3.html etc. 它们都将基于创建日期具有连续的顺序(例如1.html,2.html,3.html等)。

PHP would find each document and add it in the right order PHP将找到每个文档并以正确的顺序添加它

A bit inside the file would define the title (meta tags?) 文件中的一点将定义标题(元标记?)

That seems a really bad way of doing it, but anyway.. You want to be you want to be using the directory functions. 这似乎是一种非常糟糕的方法,但是无论如何..您想要成为您想要使用目录功能。 Specifically http://www.php.net/manual/en/function.readdir.php 具体是http://www.php.net/manual/en/function.readdir.php

A good idea would be to set up a MySQL system for this, but it is achieveable with PHP only. 一个不错的主意是为此建立一个MySQL系统,但这只能通过PHP来实现。

You could get all .html files in the folder with glob , and then include them with this code: 您可以使用glob获取文件夹中的所有.html文件,然后将其包含在以下代码中:

foreach (glob("/htmlfiles/*.{htm, html}") as $filename) {
    include "$filename";
}

The nice thing about this, is that it's sorted alphabetically and numerically too. 这样做的好处是,它也按字母和数字排序。

Edit: You would then use this system twice, with two folders, one for the meta tags/title, and one for the page itself. 编辑:然后,您将使用此系统两次,有两个文件夹,一个用于元标记/标题,一个用于页面本身。 Again, not the best way to do it. 同样,这不是最好的方法。 You should really check out a CMS . 您应该真正检查CMS

If you used MYSQL, you can still get all html files from the folders as you are now, but just use MYSQL to make simple basic references, such as the filename, date, category, etc. Then you don't need to use complex (and likely failing) file and directory code to determine when a file was created and which order to serve them in. The DATE in your MYSQL would be when to serve them. 如果使用MYSQL,您仍然可以像现在一样从文件夹中获取所有html文件,而只需使用MYSQL进行简单的基本引用即可,例如文件名,日期,类别等。然后,您无需使用复杂的(和可能失败的)文件和目录代码,以确定何时创建文件以及以哪种顺序提供文件。MYSQL中的DATE是何时提供文件。

To answer your meta questions, I would have a header.php file with all the meta data in for the site (doc declaration, titles, css links, etc) and then each individual file could have a variable to pass to the header when it's included. 为了回答您的元问题,我将创建一个header.php文件,其中包含该网站的所有元数据(文档声明,标题,css链接等),然后每个文件都可以具有一个变量,以在传递给标头时包括在内。

eg 例如

File: about.php 档案:about.php

  $PageTitle = 'About';
  include_once('header.php');
  <some more code>
  <h1>$PageTitle</h1>

File: 1.php 文件:1.php

  $PageTitle = 'News about something';
  include_once('header.php');
  <some more code>
  <h1>$PageTitle</h1>

File: header.php 文件:header.php

  usual code, doc declaration, head, etc
  <title>$PageTitle</title>

So at the start of each file you declare what the title will be then include header.php. 因此,在每个文件的开头,您将声明标题,然后包括header.php。 The title is used on the meta title (so your browser and tab etc) and the var in the file can also be used on headers (ie h1, h2) and links if needed. 标题用于元标题(因此您的浏览器和选项卡等),并且文件中的var也可以用于标题(即h1,h2)和链接(如果需要)。

you could do all this without MYSQL still, but using a database to even just reference things like date_of_creation - last_update_date - author, etc, could save you headaches, but is up to you how you want to do it and what skills you have etc. 您仍然可以在不使用MYSQL的情况下完成所有这些操作,但是使用数据库甚至仅引用诸如date_of_creation-last_update_date-author等之类的东西,就可以省去您的头痛,但是由您自己决定如何做以及拥有什么技能等。

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

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