Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: replace dilemma

  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts

    replace dilemma

    Hi,

    I'm writing a system for articles. In mysql, each article has title and content.
    the index page shows a link to all articles, and the href value is the title or the article, with some fixes. For example:
    "first article" => <a href="first-article">first article</a>
    "hello world article" => <a href="hello-world-article">hello world article</a>
    That's easy: in the index I'm using str_replace for replacing spaces to hyphen, and in the page shows the article (htaccess refers to this page) simply replacing hyphen to space, and showing the article with this title.

    My problem is that titles can include characters like "?", "!", ",", "-" and many more. Of course, I can link to the article without these characters, something like this:
    "first - article?" => <a href="first-article">first - article?</a>
    But, I have no idea how I can convert first-article back to first - article? in the article page (in order to show the correct article).

    Is there an elegant solution to this problem? I want to avoid adding another column to my database with the href-value.

    Thanks a lot.

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    If you are storing the title and content in the database why don't you just use the index value in your link?

    Index your SQL table and pull out the index and article title and store in an array with the index of the array as the index of the article.

    eg:

    PHP Code:
    $aContents = array();
    foreach (
    $aContents as $key=>$value){
    echo 
    '<a href="/articles/?articleID='.$key.'">'.$value.'</a><br />';

    Then on your article page just GET['articleID'] and query DB for the title and content where articleID = $_GET['articleID'].

    Make sense?
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • #3
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Make sense?
    No, not really.
    I want to keep the href value as it is now ("first-article-title"), not with ?id=XX. Otherwise, I wouldn't ask the question.

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    You asked for an elegant solution.

    What I have given you is an elegant solution. It allows you to create a single php page that will display each article depending on the parameters provided to it.

    This removes the need to str_replace anything, cuts down on overheads and page creation and looks a whole lot better than having an href with lots of slashes, underscores and whatnot.

    Why do you need to use the full title of the article in a link? Is there a specific reason for doing it like this?
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • #5
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This removes the need to [...] cuts down on overheads and page creation
    My friend, I have one single page to display all articles, and all URLs are directed to this page, using htaccess.

    Is there a specific reason for doing it like this?
    It has many advantages over the method you mentioned. You never came across websites using this method?

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Of course I have but it is not elegant as you so wished it to be.
    Why ask for help and then shoot down or refuse to try anything suggested?

    Hope you get what you need but I will no longer contribute to this thread.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • #7
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why ask for help and then shoot down or refuse to try anything suggested?
    I'm sorry it came out like this, but I asked for a solution in a specific problem, and your solution was to move to a different method, and that's not what I asked for.

    Lets say my watch is a bit late, or it's has a scratch. Your solution is to buy a new watch, while I can simply change the batteries or take it to someone who can polish it. I like my current watch, and I want to keep using it
    Last edited by b_hole; 07-03-2007 at 02:44 PM.

  • #8
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    urlencode was designed to do this, but it's not incredibly human-readable. You can do some combination, too, where for example you replace all spaces with dashes and let urlencode mangle the punctuation, which would end up being pretty workable.

    You could also add a virtual column to your database using a view with your title with all non-alphanumerics replaced with dashes, index on it, and use that to bring up your content, thereby eliminating the need to convert back to the exact title. (Just to some dashed approximation of it, that's nonetheless unlikely to repeat. You can enforce uniqueness with a trigger that selects on the view for colliding keys on insert) All this stuff has database-manager-specific implementation.
    Last edited by ralph l mayo; 07-03-2007 at 02:31 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •