Are you using codeigniter for this development? This answer below is based on
the web application framework from codeigniter. So here goes,
The problem is that I want page [root]->fruits->tropical->bananas
to
be accessible only from this url:
http://localhost/cms/fruits/tropical/bananas/
.
So create a function in your controller with function name fruits and with two parameters? For example
class Cms extends CI_Controller {
...
...
...
public function __construct() {
$this->load->model('cms_model');
}
public function fruits($tropical, $bananas) {
$string = $this->cms_model->getPage($tropical, $bananas);
// load the view you want.
$this->load->view('');
}
...
...
...
}
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 cms:
Id
Slug
ParentId
Table cms_parent:
Id
Let's describe with two example table shown above, cms table and cms parent table. You did not specify exactly in your question on what is your query want or the query result return. So below is my guess based on your question description that is, query two table by joining them using the common key and then apply the condition where.
// select * from cms t1 join cms_parent t2 on t1.ParentId = t2.Id where t1.Id = '' and t2.ParentId = 'level1';
public function getPage($level0, $level1) {
$this->db->select('*');
$this->db->from('cms');
$this->db->join('cms_parent', 'cms.ParentId = cms_parent.Id');
$this->db->where('cms.Id', $level0);
$this->db->where('cms.ParentId', $level1);
$query = $this->db->get();
// return one row from database.
return $query->row();
}