0

I created index.php that basically looks up all the individual php files (articles) in a few subfolders and prints them out. Since the quantity of articles is over 30 and growing, I implemented a multi-page solution using PHP session(). So, page2.php simply prints out the second part of my array titled "page2" and page3.php prints out the third part of my array titled "page3". These array parts are arrays themselves (it's an array of arrays).

All works fine.

However, this requires me to hard code "page2.php", "page3.php", etc. This will get out of hand at some point.

How would you guys handle this? Can I dynamically create "pageX.php" upon user clicking the "Next" button? Or, should I do some type of AJAX solution? Is there a standard way of doing a multipage PHP index?

0

It is much more common to use url parameters. For example:

example.com/index.php?page=1
example.com/index.php?page=2

Then on index.php you can use $_GET['page'] to figure out which page you are retrieving, then serve that data. If you are actually generating php files on your server you can serve that page using:

include 'page2.php';

Or even better:

include 'page'.$_GET['page'].'.php';

Disclaimer: You should sanitize your variable before using it: such as:

$page = sanitize($_GET['page']); //You need to write the sanitize function
include "page$page.php";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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