0

before anyone asks; I've googled my 'question', I've also looks at the 'Questions that may already have your answer' and none of them work.

What I'm wanting to do is 'Pagination'. However, I don't want to use Databases as I've never had to and I'd rather not give up and go to them now as XML does everything I want it for.

The code I have is the following:

$files = glob('include/articles/*.xml');
    foreach($files as $file){
        $xml = new SimpleXMLElement($file, 0, true);
}

I've tried these ones already: XML pagination with PHP, PHP XML pagination and Pagination Filtered XML file and have achieved nothing. I have also tried a lot of Javascript 'pagination' scripts and still nothing.

So to sum it up: I have four articles (More to be added) and I want to show 2articles per a page. The following information will be 'pulled' from the xml file: ID, TITLE, CONTENT, PICTURE, AUTHOR, DATE by doing $xml->id and so on for the rest of them. Does anyone know of any way of doing this? as I've spent the past four hours (Its 4:04AM GMT) and have found nothing that works yet. (If I find anything that does work I'll make sure to update the question with the working code encase there is anyone else out there that needs help with this too.)

Community
  • 1
  • 1

1 Answers1

0

For a start define the order in which you want your articles to appear. I.e. which article goes on page 1, which one on page 2, etc. This is important, because that order will be the base for your pagination algorithm. Please note that glob() is not guaranteed to return results in any specific order, which means the order can change from one invocation of your script to another (notably when you add new articles) -- almost certainly not what you want.

Then the second step is to introduce another variable which is part of your URL that denotes the actual page (number) you're on. The URL query string would be a natural choice for putting this information, so your URL's look like: article.php?page=1. On the PHP side you can use the $_GET superglobal to retrieve the query string parameters.

Thirdly, use the new style URL's whenever you link to your article.php script. Additionally, validate the input --especially when you also want to display the current page based on this parameter (or you will end up with an injection vulnerability). This also means you want to have a default value (in case the value is invalid/wrong/ or not supplied at all for some reason).

Finally, filter your articles based on the two key pieces of information: the order of the articles w.r.t. the page number and the page number: i.e compute the actual articles that should appear on the current page.

user268396
  • 11,576
  • 2
  • 31
  • 26
  • Thank you :-) I had no idea where to go with this. Ill keep you posted on what happens. If I get it working ill post the results above. – user2673501 Aug 12 '13 at 17:09