dpka7974
dpka7974
2013-02-03 00:55

PHP - 如何加载新闻文章? [关闭]

  • sorting
  • dynamic
  • php
  • algorithm
已采纳

I am a newcomer to php so sorry if this is a lame question but I would really love to know how to solve this problem that I can’t get out of my mind.

On every dynamic site there are features like news articles and other kind of user generated lets call them boxed contents. If You surf the page You can see that there are a specified number of articles which are displayed and on the bottom of the page and there are page numbers or arrows that can be used to display older posts.

I would be interested in the process of making something similar to this in php. I am not looking for a ready code because I know that every site is unique but I would like to learn the algorithm. I am looking for the logical structure that solves this.

By that I mean for example I would like to set the maximum number of news articles (boxed contents) displayed on a page. I think I can do this with a loop. That’s not that hard for a single page but How do I solve that if there are about 300 articles and I want to show the 10 most recent on the first page then if the user clicks on the second page/next arrow It loads the other 10 articles which are older and so on.

My theory is If I click the number 2 page link It posts a php code that changes a variable which is used in the mysql query that we used in the article loading form so It loads the articles/boxed contents from a different starting id ( i.e. not 0 but 10 if we click the 2. page link or 20 if we click the 3. page link). The loop starts and It loads the following(previous) 10 articles.

I know that in the database I should calculate backwards when grabbing the article id because the newest article has the highest id always but for explanation I thought the example above is easier to understand and I can show You what I would like to do.

There is a big chance that my theory is totally wrong but I would like to share my thoughts so far.

Sorry if it is an incompetent question I would appreciate any word on this problem.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongpo1846 dongpo1846 8年前

    The way to do this is to leverage the MySQL limit clause with 2 parameters, the start and maximum number of rows to retrieve. In your example, it would be limit *page_number* * 10, 10. Alternatively, you could achieve the same goal in PHP, but I'd suggest keeping the heavy lifting of manipulating data in SQL.

    点赞 评论 复制链接分享
  • dongwu8064 dongwu8064 8年前

    mysql_query("SELECT * FROM myAritcleTableName ORDER BY primaryKeyTableName DESC"); // php code

    点赞 评论 复制链接分享

为你推荐