dpka7974 2013-02-03 00:55
浏览 28
已采纳

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

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 2013-02-03 01:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况