I am implementing a snippet-based content management system into my current project. Snippets can be associated with a page either by exact match of the full url falling back to a wildcard match on a partial url or finally a default snippet.
To implement this I have a created table of page associations with a wildcard flag, the system first checks the current url against the non-wildcard associations and if it doesn't find a match it checks against the partial url's where the wildcard flag is set.
In order to achieve this I am getting all the partial url's from the database and putting them into an array then walking the array to check for a match against the current url:
protected function _check_wildcard($url = NULL)
{
if($url)
{
$q = $this->db->where('wildcard' ,'Y')
->from('content')
->get();
$wildcards = $q->result_array();
foreach($wildcards AS $wildcard)
{
if(strpos($url,$wildcard['url']) !== FALSE )
{
return $wildcard['snippet_id'];
}
}
}
else
{
return NULL;
}
}
Can anyone suggest a better way to do this - preferably one that doesn't involve having to constantly download the full list of all the wildcards each time I load a page as I am afraid that this will have a negative effect on the scalability of the system down the line?
You could query your database directly instead of loading all the wildcards. Assuming MySQL, this query checks for the wildcard URL in the given URL string. Ex :
// Keeping your current wildcard = 'Y' condition and adding the wildcard check.
$where = "wildcard = 'Y' AND INSTR('" . $url . "', url) > 0 ";
$this->db->where($where)->from('content')->get();
You can check for your database's documentation to see if it supports INSTR or a similar function.
If this query returns no rows, you can return NULL
and if it has a row, you can return the first snippet_id
.