0

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
  • 1
    What you're saying sounds like a possible way to go do this. Go out and try it!
    – jchapa
    Feb 3, 2013 at 0:58
  • Thank You for the encouragement I am really a newbie so I thought first I ask it so I won't mess anything up but after hearing that it could work I will definitely give it a try. :) Feb 3, 2013 at 18:30

2 Answers 2

2

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
  • Thank You so much! This is exactly what I wanted. It really gives me the control. Thank You so much for helping me! Feb 3, 2013 at 18:23
0

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

3
  • mysql_query is DEPRECATED. It is not recommended for use in new development: php.net/manual/en/function.mysql-query.php
    – jchapa
    Feb 3, 2013 at 0:59
  • just an idea, i'm not saying use it, i was showing the general query
    – coders_org
    Feb 3, 2013 at 1:10
  • Thank You for Your time coders_org but this isn't what I was looking for. Thank You jchapa for pointing out that mysql_query is not recommended I didn't know that until now. Feb 3, 2013 at 18:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.