1

I'm creating a website that will feature articles listed down a page (with their content). Think of a blog style. However, I think having 50 articles fully displayed on one page will be a bit ridiculous, so I'm wondering what the best way to divide it into pages automatically would be.

I'm fetching the articles from a database using PHP and MySQL.

muttley91
  • 12,278
  • 33
  • 106
  • 160

4 Answers4

2

A couple suggestions that can be mixed and matched with other things.

  1. Query for X number of articles. At the bottom of the page have a link to go to the next X number which queries for X number of articles starting after the last article displayed. Exact code would depends on how you organize things, but essentially just call X articles the content of 1 page and then give your dynamic page a value that gives it an offset to start from.
  2. Especially nice if you have long articles, use some ajax or even just good ole fashioned javascript to "expand" articles. limit their display to just the first paragraph or so and then have a "read more" button that shows the rest of the article content. If it's embedded in the page and hidden/shown with css and javascript it will still get indexed by search engines.
  3. Probably better than the previous for SEO purposes. Put each article on it's own page, and make your article list just display the first paragraph or so with a "read more" link to take you to the full article page. this is likely preferable because a) it's easy (KISS is always a good method) b) it gives your site more unique pages without mixing keywords as much (good for SEO) c) the articles can be linked to directly and easily from other sites (good for SEO)

Just some thoughts. There's never a "best" way to do anything, just ways that make more sense in context.

ima747
  • 4,667
  • 3
  • 36
  • 46
1

Count the amount of articles in the current category,

Divide total by amount per page to get a total number of pages.

Select articles in amount per page from mysql, where id>(pagenum*amount per page)

create links for navigation.

Example query:

$sql = "SELECT * FROM articles ORDER BY date ASC LIMIT 0, 20"; // This will return the first 20

$sql = "SELECT * FROM articles ORDER BY date ASC LIMIT 40, 20"; // This will return 40-60

Note: This is called pagination for future reference/searching. There are alot of good examples, and some frameworks that handle this for you.

Simple tutorial: http://www.phpfreaks.com/tutorial/basic-pagination

David Houde
  • 4,835
  • 1
  • 20
  • 29
1

I don't work with PHP specifically, but the general strategy for doing something like this is to design your query to dynamically limit based on the desired count of items per page and how many items should be skipped. In other words, your SQL is along the lines of the following:

$stmt = $mysqli->prepare("SELECT id, title, createdDate FROM tArticles ORDER BY createdDate desc LIMIT :skip, :itemsPerPage");

If you're tracking this by page number, your :skip bind parameter will be derived by taking the current page number (starting at 0) and multiplying times the number of items per page.

You should probably take a look at the answer for this question as well to avoid a potential common pitfalls with needing to cast your variables as integers before binding.

Community
  • 1
  • 1
StudyOfCrying
  • 530
  • 3
  • 9
1

I remember once I took a look on how Blogger did this, and It was quite interesting

It was something like this:

  • have a default post-timestamp (should be the newest post)
  • have a default display-per-page (something like 10-20)
  • then make a search on your DB starting at the post-timestamp and limit the search to the display-per-page
  • give the "next" link the post-timestamp of the last post of the search along with the display-per-page
ajax333221
  • 11,436
  • 16
  • 61
  • 95