Search code examples
phpcodeignitermod-rewriteroutespyrocms

How do I use the category name rather than the category id in my url?


I am using the CodeIgniter framework and am having trouble figuring out how to get my urls to display the category name but still have my queries reference the category id.

I have a controller set up called jobs. The structure of the url is "example.com/jobs/listings/category_id" example: "example.com/jobs/listings/3" where 3 would be the category id for "software".

I'd rather the url say "example.com/jobs/listings/category_name" example: "example.com/jobs/listings/software".

The problem I'm running into is that the "jobs" table holds stores the category_id that the job belongs to and not the category_name. I have a separate table that holds just the category id's and their names. I'm not sure how to A.) Write my query and B.) Make an SEO friendly url structure.

My current query looks something like "SELECT * FROM jobs WHERE cat_id = 3;

Also, I'm not sure if I should be using mod_rewrite in an .htaccess file or if I should be using the routes.php file in CodeIgniter.

Any help would be greatly appreciated!

-Thanks


Solution

  • Just join to your other table.

    SELECT * FROM jobs JOIN categories ON jobs.cat_id = categories.id WHERE categories.name = ?;
    

    Make sure to put a unique key on the category name, if you don't have one already.

    mod_rewrite is a poor solution to app routing and wouldn't help much here anyway. Stick to whatever your framework provides.