Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:59:43+00:00 2026-05-31T09:59:43+00:00

I’m coding a little learning CMS project and I’ve hit a brick wall that’s

  • 0

I’m coding a little learning CMS project and I’ve hit a brick wall that’s stopping me to complete the next step. I know I should be taking KISS (Keep It Simple, Stupid) into account, but I think it would nice, to be able to group pages hierarchicaly.

The problem is that I want page [root]->fruits->tropical->bananas to be accessible only from this url: http://localhost/cms/fruits/tropical/bananas/. What I came up with until now is that cms table has a parent field that points to its parent. The question is: How to parse uri adress and select a row from DB with as few queries/efficiently as possible?

Table structure:
Id
Slug
...
...
...
ParentId

All help and advice is kindly accepted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:59:45+00:00Added an answer on May 31, 2026 at 9:59 am

    Here is the table structure that I used for testing this –

    CREATE TABLE  `test`.`pages` (
        `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        `slug` varchar(45) NOT NULL,
        `title` varchar(45) NOT NULL,
        `content` text NOT NULL,
        `parent_id` int(10) unsigned DEFAULT NULL,
        PRIMARY KEY (`id`),
        UNIQUE KEY `UQ_page_parent_id_slug` (`parent_id`,`slug`),
        CONSTRAINT `FK_page_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `pages` (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    Note the unique key on (parent_id, slug). This is key to getting the best performance from the following query. I tested this with 50k rows and it still returned in less than 1ms for a five slug path – /cms/slug-1/slug-2/slug-3/slug-4/slug-5/

    Here is the PHP code that I have come up with to build a suitable query –

    <?php
    
    // I will assume the rest of the url has already been stripped away
    $url = '/fruits/tropical/bananas/';
    
    // lets just make sure we don't have any leading or trailing /
    $url = trim($url, '/');
    
    // now let's split the remaining string based on the /
    $aUrl = explode('/', $url);
    
    /**
    * Now let's build the query to retrieve this
    */
    
    // this array stores the values to be bound to the query at the end
    $aParams = array();
    
    $field_list = 'SELECT p1.* ';
    $tables = 'FROM pages p1 ';
    $where = "WHERE p1.parent_id IS NULL AND p1.slug = ? ";
    
    // this array stores the values to be bound to the query at the end
    $aParams[] = $aUrl[0];
    
    // if we have more than one element in our array we need to add to the query
    $count = count($aUrl);
    
    for ($i = 1; $i < $count; $i++) {
    
        // add another table to our query
        $table_alias = 'p' . ($i + 1);
        $prev_table_alias = 'p' . $i;
        $tables .= "INNER JOIN pages $table_alias ON {$prev_table_alias}.id = {$table_alias}.parent_id ";
    
        // add to where clause
        $where .= "AND {$table_alias}.slug = ? ";
        $aParams[] = $aUrl[$i];
    
        // overwrite the content of $field_list each time so we
        // only retrieve the data for the actual page requested
        $field_list = "SELECT {$table_alias}.* ";
    
    }
    
    $sql = $field_list . $tables . $where;
    
    $result = $this->db->query($sql, $aParams);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.