简体   繁体   中英

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. What I currently have is a faux news (ha ha ho ho) site for practise here.

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

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

They will all have a consecutive order based on the date they were created (eg 1.html, 2.html, 3.html etc.

PHP would find each document and add it in the right order

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

A good idea would be to set up a MySQL system for this, but it is achieveable with PHP only.

You could get all .html files in the folder with glob , and then include them with this code:

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 .

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.

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.

eg

File: about.php

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

File: 1.php

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

File: 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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