Re: Problem with mod_rewrite and replacing spaces in URL



David wrote:
Sent this to alt.php a couple of days back, but doesn't look like I'll
get an answer, so trying here.

I'm trying to convert a script to use friendly URLs, I've done this
before, but my PHP skills are quite basic so far, far from proficient
at this.

<snip>


One thing I immediately noticed: your text2url function lowercases everything, but your Britney Spears example is capitalized.

At a more fundamental level, how are you using the URL to lookup the artists themselves? This is where the problem lies. You are going to have to do the same transformation to the artist name when looking up the artist that you did when creating the friendly URL.

For example, here is a simple scheme which lowercases the URL and replaces spaces with dashes, and the corresponding database lookup.

..htaccess:
-----------------------------------------
RewriteRule ^artists/([^/]+) /artists.php?artistName=$1
-----------------------------------------


URL creation:
-------------------------------------------
$url = "/artists/" . strtolower(str_replace(" ", "-", $artistName));
-------------------------------------------


Artist lookup (assumes PDO/PGSQL):
------------------------------------
$artist_query = $pdo->prepare("select * from artists where LOWER(REPLACE(artist_name, ' ', '-')) = :artistName");

$artist_query->bindParam(":artistName", $_GET["artistName"]);
$artist_query->execute();
$artist = $artist_query->fetch();
-----------------------------

This code will obviously not work verbatim; it's just an example of how you need perform the same transformation on artist names when you're looking them up, or the comparison won't work. Notice how in my SQL query I'm comparing the passed-in artist name (in the form of "britney-spears") with the artist name in the database (in the form of "Britney Spears") only AFTER I transform the database version to the proper format - LOWER(REPLACE(...)) transforms "Britney Spears" to "britney-spears", making the comparison successful.

Jeremy
.



Relevant Pages

  • Re: Problem with mod_rewrite and replacing spaces in URL
    ... artists themselves? ... and the corresponding database lookup. ... I don't understand why the function fails? ... "Britney Spears") only AFTER I transform the database version to the ...
    (comp.lang.php)
  • HOW DO i MAKE A LIST FOR SONGS?
    ... I don't have a database application. ... >different artists. ... >identifier to each instance of the song. ... if your search criteria is the ...
    (microsoft.public.excel.worksheet.functions)
  • Re: [PHP] MP3 Ripping
    ... But what if the CD has music from different artists? ... and letting people using a simple interface select some songs. ... A database is there to hold your data, and basically display it in a format ... Just would be nice if there was something in PHP. ...
    (php.general)
  • Whats the best way to set this up?
    ... task of redoing the entire mailing list and database ... artists, one for curators, one for mailing list, etc. ... But this means that when a new entry needs to be entered, ... suddenly decides to become a member, ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Whats the best way to set this up?
    ... >different tables within a single database, ... >artists, one for curators, one for mailing list, etc. ... Membership: each record in this table has a PersonID ...
    (microsoft.public.access.tablesdbdesign)