...

View Full Version : replace dilemma



b_hole
07-03-2007, 10:19 AM
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.

daemonkin
07-03-2007, 12:25 PM
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:



$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?

b_hole
07-03-2007, 12:43 PM
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.

daemonkin
07-03-2007, 01:20 PM
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?

b_hole
07-03-2007, 01:46 PM
This removes the need to [...] cuts down on overheads and page creationMy 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?

daemonkin
07-03-2007, 02:04 PM
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.

b_hole
07-03-2007, 02:13 PM
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 :)

ralph l mayo
07-03-2007, 02:29 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum