Reputation: 478
i'm very newbie trying to finish a job, so let me explain the issue here:
i have a chasback website, there are retailers on database, my links are :
a href="/view_retailer.php?rid=<?php echo $tops_row['retailer_id']; ?>"
The php page that show the content have:
if (isset($_GET['rid']) && is_numeric($_GET['rid']))
{
$retailer_id = (int)$_GET['rid'];
}
else
{
header ("Location: index.php");
exit();
}
That display on my URL: /view_retailer?rid=8
I want to change that to /view_retailer?rid=retailer-title
I can't figure why its not working, i have on my DB the columns retailer_id and title.
Then i need to change from /view_retailer?rid=retailer-title to /loja/retailer-title through mod_rewrite.
How can i reach there? thanks for helping me!
Upvotes: 0
Views: 121
Reputation: 1888
In the retailers table add column named "slug" or something like that. Then you have to modify you query thus you are not looking for id but certain string (slug). Make sure that your slugs contains only signs proper for web addresses: no spaces, non-latin letters etc.
When it comes to code, it may look something like that:
if (!empty($_GET['title']))
{
$title = someSanitizeFunc($_GET['title']);
$retailer = db()->queryOne("SELECT id FROM retailers where slug = '".$title."');
}
It's just example, you have to adjust it to your cms or whatever you use.
Upvotes: 0
Reputation: 2046
How about using 3 tables in your DB?
retailer_id
retailer_name
and retailer_url_name
values (example)
43
The Awesome Supermarket
and awesome-supermarket
Check $_GET['rid']
with a regex pattern to check for letters and the -
character and match against retailer_url_name
in the DB?
// check for letters and -
if (preg_match('/^[a-z\-]+$/', $_GET['rid']) {
// do stuff here
} else {
// invalid ID
}
Upvotes: 0
Reputation: 4305
I can answer the first bit
a href="/view_retailer.php?rid=<?php echo $tops_row['retailer_id']; ?>"
Needs to be:
<a href="/view_retailer.php?rtitle=<?php echo $tops_row['retailer_title']; ?>"
And
if (isset($_GET['rtitle']) && is_string($_GET['rtitle']))
{
$retailer_title = (string)$_GET['rtitle'];
}
else
{
header ("Location: index.php");
exit();
}
You should also sanitise anything coming from GET/POST to make sure it is safe if you intent to query the database with it.
As for the mod_rewrite. In very rubbish at that so I would not be able to help with that.
Upvotes: 2