October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Build Category Pages Across Multiple Pages in PHP

Show root categories first, then use the selected category’s ID to query and display its subcategories on a second PHP page.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The question describes a standard parent-child category layout: show categories with a null parent_id, then open a second page showing the selected category’s children. A straightforward way to build it is to query root categories for the first page, link each one by its stable ID, and use that ID in a prepared query for the next page.

What the question is asking

The SitePoint question, listed as posted on November 13, 2013, asks for a category view spanning pages: the first page lists rows whose parent_id is null; selecting one opens a page listing its subcategories. The question is tagged PHP and SQL. A Stack Overflow chat transcript reproduces the question text, but does not show an answer, so the original thread’s solution cannot be established from the available record: SitePoint thread and transcript.

As an Amazon Associate I earn from qualifying purchases.

Use a parent-child relationship in the data

In a common relational design, each category has a unique ID and a nullable parent_id that refers to another category’s ID. A null parent marks a root category; a non-null parent identifies the category above it. The question does not specify its database or actual schema, so treat these names as an illustrative pattern, not as details of the original site.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, a table might contain id, name, and parent_id. Root categories can be selected with WHERE parent_id IS NULL. Child categories can be selected with WHERE parent_id = ?, binding the chosen category’s ID as a parameter.

Build the two-page request flow

1. List root categories

On the first page, query categories with a null parent. For each result, render a link to the child-list page and include that category’s ID in the URL, for example categories.php?id=12. Use the database ID rather than the category name as the lookup key: names may change or repeat, while the ID identifies the selected row.

2. Validate the selected ID

On categories.php, read the id parameter and validate that it is present and in the expected format before querying. Then use a prepared SQL statement to retrieve children whose parent_id matches that ID. Do not concatenate raw URL input into SQL; parameter binding helps prevent SQL injection.

3. Render children or an empty state

If the query returns rows, render them as the selected category’s subcategories. If it returns none, show a clear message such as “This category has no subcategories” rather than an unexplained blank page. If the ID is missing or invalid, return an appropriate error or send the visitor back to the category list.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What changes for deeper hierarchies or pagination

The described interaction only establishes a root page followed by a page of children. If categories can themselves contain further descendants, the same parent-ID pattern can be repeated at each level, or the application can provide a recursive tree view. The right choice depends on how deeply nested the real data is and how users need to navigate it; the question does not specify either.

Pagination is a separate concern from hierarchy. If a category has too many children for one page, apply pagination to that category’s child query, keeping the selected parent ID in the links for the next and previous result pages. If the number of children is manageable, no pagination mechanism is required by the described design.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.