I have a page where I pull a specific row from the database where the name equals what I put in the URL, but I want it SEO friendly.
So say I have “David’s Print” in the database.
I want to be able to pull it from the database by putting “davids-print” in the URL
I’m using Codeigniter as my framework.
If I need to explain better, let me know
Currently I’m using
public function item($name)
{
$this->data['item'] = $this->db->get_where('items', array('name' => str_replace("-", " ", $name)))->result_array();
$this->layouts->view('databases/items/single', $this->data);
}
But of course that is only for spaces
It’s the best to store SEO-friendly site id’s or slugs, as they are called, next to the actual title of your item. It would be hard to convert the slug back to the original title since certain characters are lost.
First add the
slugfield to youritemstable and make it unique so that there is no ambiguity (so each item has its own slug).The slug is then generated whenever an item is inserted or updated, for example:
If the DB throws an exception because of the “unique” constraint you can try to insert the record multiple times and just add a number to the slug.
Then you can easily search for the item using the slug (
$namecontains the url-part likedavids-print):Thanks to Madmartigan for the suggestion to use
url_titlewhich comes with CI.