mllamazares
mllamazares

Reputation: 8166

Is this a correct practice to generate friendly URLs?

I'm generating url of AJAX call like I explain here. (Please take a look of this post, is necesary to understand the actual).

Some url has special chars so would be something like:

http://myweb.com/ni%C3%B1o/pull%26bear/jacket%20shirt%20pants%20underwear%20hat/23

And I want to create friendly URLs removing some chars and limiting the third parameter to have only three words maximum. The result will be like this:

http://myweb.com/nino/pullbear/jacket-shirt-pants/23

So the php will be able to know what parameters I'm passing I added a new colum in my database for each VARCHAR with the pretty url conversion. I explain, something like this:

ID  SEX   SEX_PRETTY  BAND       BRAND_PRETTY  DESCRIPTION                       DESCRIPTION_PRETTY   AGE
1   NIÑO  NINO        PULL&BEAR  PULLBEAR      JACKET SHIRT PANTS UNDERWEAR HAT  JACKET-SHIRT-PANTS   10
2   NIÑA  NINA        ZARA       ZARA          JEANS BIKINI DRESS SWEATER        JEANS-BIKINI-DRESS   13

The AJAX calls the php, and in the php I do a pretty colum to normal colum conversion in the database.

I think this is the only way to do what I want, isn't it? Any help or advice would be appreciated, and if you need more info, let me know and I'll edit the post.

Upvotes: 0

Views: 75

Answers (1)

Hugo Delsing
Hugo Delsing

Reputation: 14173

What you are doing is correct. The values from the URL are just parameters, so you can use them to select rows from the database. When you parse your URL http://myweb.com/nino/pullbear/jacket-shirt-pants/23 it would return something like

<?
$sex = 'nino';
$brand = 'pullbear';
$description = 'jacket-shirt-pants';
$age = '23';
?>

Then you can use that information to select something from the database. For simplicity I will concat the query, but make sure you do some validation or use parameterized queries

<?
//WARNING: POSSIBLE UNSAFE EXAMPLE, DONT USE DIRECTLY IN PRODUCTION

$query = "SELECT * FROM Table WHERE SEX_PRETTY='$sex' AND BRAND_PRETTY='$brand' AND DESCRIPTION_PRETTY='$description' AND AGE='$age'";

//now in your results you can use the `SEX`, `BRAND` and `DESCRIPION` columns to show information
?>

Keep in mind that if you want to uniquely identify a single product it would be best to create a UNIQUE INDEX on the combined columns for SEX_PRETTY, BRAND_PRETTY,DESCRIPTION_PRETTY AND AGE.

Also since you are talking about using JSON keep in mind that not all special charactors are allowed, so you might need to encode them in the output. Check json-character-encoding

Upvotes: 2

Related Questions