Question

The goal

Select data from database using Name-Of-Product instead of its id.

The problem

My application allows the user to compare product prices. To make some comparison I need to access myapp.com/Product/Compare?Id=1. I'm upgrading my application and now I want to access myapp.com/Product/Xbox-360 to make the comparison.

How can I select the sluggified word from the database instead of its id? Do I have to unsluggify the string?

Can someone enlighten me?

What I've already tried

I even do not know how to start. In other words, I didn't try nothing. Oh... Actually, if necessary, I already created a method that can sluggify some string, but I don't think it is necessary to post here.

SQL Spotlight

My query to produce the list of products to compare is simple:

Select product.Name,
       marketProduct.Price
From app_products As product
Join app_marketProducts As marketProduct
Where product.Id = 1
Was it helpful?

Solution

You essentially have two choices:

  • You can store the slug in a new column within your database.

  • You can add the primary key (the ID) to your URL in some way:

    a) Directly to the slug (e.g. /42-My-Slug)

    b) To another section of the URL (e.g. /42/My-Slug). This seems to be the best practice, e.g. StackOverflow uses it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top