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

For a parent-child category layout, show categories with a parent_id of NULL on the first page, then link each category to a page that queries its children using that category’s ID. The available record for the November 13, 2013 SitePoint question describes this interaction, but does not include the original replies or establish a solution. This article explains a conventional implementation rather than attributing advice to a forum participant. SitePoint question record.

What the category-pages question describes

The question asks for a category layout across pages: the first view lists rows whose parent_id is null, and selecting one opens another page containing its subcategories. It is tagged PHP and SQL in the indexed record. A Stack Overflow chat transcript repeats the question but does not show a reply or solution, so the available sources do not establish what answer, if any, appeared in the original thread. Transcript reproducing the question.

Model categories with a parent identifier

A common way to represent a tree in a relational database is an adjacency list: each category has an identifier and a nullable parent_id. A null parent marks a root category; otherwise, the value points to the identifier of the category immediately above it.

For example, a table might contain id, name, and parent_id. The precise schema depends on the application and database; neither is specified in the question record. This structure is suitable for the two-level interaction described. If the application needs arbitrary-depth trees or more complex hierarchy operations, assess the representation against those needs rather than assuming a single schema is best.

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

Build the two-page request flow

1. List root categories

On the first page, select categories whose parent is null and render each name as a link carrying its stable identifier. In SQL, the root condition is WHERE parent_id IS NULL; use IS NULL, not = NULL.

2. Pass the selected category ID

Each link can point to a detail route such as category.php?id=12. The number here is an illustrative ID, not a value from the forum question. In PHP, read the request parameter, validate that it is a positive integer, and handle missing or malformed input before querying. Do not trust a URL parameter simply because it was generated by your own page.

3. Query the selected category’s children

Use the validated ID as a bound parameter in a prepared statement, conceptually:

SELECT id, name FROM categories WHERE parent_id = ? ORDER BY name

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

Bind the ID through the database library’s prepared-statement API; do not concatenate raw request input into SQL. The exact PHP code differs between PDO and MySQLi, and the question does not identify which database driver is in use.

4. Render results, including an empty state

Display the returned rows as links if users can continue navigating into deeper levels. If the query returns no rows, show a clear message such as “This category has no subcategories” rather than an empty page. If this is strictly a two-level design, the child page can instead show the subcategories as terminal items.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Pagination is separate from hierarchy navigation

The phrase “spans across multiple pages” may mean navigating from roots to children, or it may mean splitting a long list into numbered pages. The described click-through behavior is hierarchy navigation; it does not specify page sizes, numbered pagination, or a database strategy for paging results. If a category can contain many children, add pagination to the child query and preserve the selected parent ID in each page link. Keep the parent category context in the URL or route so each page continues to query children of the same parent.

Common implementation mistakes

  • Using the wrong null comparison: SQL uses IS NULL to select root rows.
  • Querying by a category name: Names can be duplicated or changed; use the stable identifier.
  • Interpolating URL input into SQL: Validate the ID and bind it as a parameter in a prepared statement.
  • Assuming every category has children: Handle a successful query with zero results as a normal state.
  • Mixing pagination with parent-child selection: Preserve both the selected parent and the requested page when implementing numbered pagination.

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.

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