How do I link to an Anchor in an article?

In this board you can talk about general questions about phpMyFAQ

Moderator: Thorsten

rickliveops
Posts: 61
Joined: Tue Mar 27, 2007 8:57 pm
Location: Palo Alto, California
Contact:

How do I link to an Anchor in an article?

Post by rickliveops »

I want to link to a specific subsection of an article. How do I do this? The normal html anchor doesn't seem to work.

The TinyMCE editor allows me to create an anchor tag in my article content. E.g. <a name="options" title="options"></a>

The editor also provides a way to link to the anchor, generating code like this:

<a href="/#options">Options</a>

But the link resulting on the article page fails, since it's trying to link to "/#options". (I've removed the leading "/" in the href but to no avail).

I've tried adding &anchor=options to the query string (e.g. index.php?action=artikel&cat=4&id=23&artlang=en&anchor=options) but that doesn't do anything.
Thorsten
Posts: 15560
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

isn't it working in HTML mode?

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
rickliveops
Posts: 61
Joined: Tue Mar 27, 2007 8:57 pm
Location: Palo Alto, California
Contact:

Post by rickliveops »

No, it doesn't work because the anchor resolves as /index.php#anchor, which of course, doesn't exist. The anchor is part of the article. Try this simple test and let me know if it works for you:

Create a new article, call it "anchor test".
Paste this code into the HTML window of the article -
<p><a href="#anchor">Link</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a name="anchor" id="anchor"></a>anchor </p>

Save the article.
View the article in the user interface.
Click the Link.
If the page scrolls to the anchor, then something's wrong with my installation. If you get a Forbidden message, then you are seeing what I see.

(The code above works fine in a normal html page)
Thorsten
Posts: 15560
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

right... it's not working... you need the full path there... maybe I should implement an interface for this...

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
rickliveops
Posts: 61
Joined: Tue Mar 27, 2007 8:57 pm
Location: Palo Alto, California
Contact:

Post by rickliveops »

Oh! I see now. I had been trying index.php#anchor?... but I realize the anchor can be appended to the end of the query string, like this:

index.php?action=artikel&cat=1&id=28&artlang=en#anchor

I had no idea that was legal syntax.

UI for this would be nice. Or at least, documentation.

Thanks for the help.

Rick
Onur
Posts: 6
Joined: Fri Jun 22, 2007 6:52 pm

Post by Onur »

I am having the very same problem. Hope you will be able to fix it, Thorsten.
cbscsr
Posts: 1
Joined: Mon Sep 17, 2007 8:15 am

Post by cbscsr »

i tried that this was not work on HTML.
johanna
Posts: 23
Joined: Mon Jun 08, 2009 1:09 pm
Location: Finland

Re: How do I link to an Anchor in an article?

Post by johanna »

I managed to do anchors with anchor tool. That way all links to same article are working nicely - until I update the article. After that all anchors lost their path to article. See links from user interface source:
After creating an anchor: /phpmyfaq/index.php?action=artikel&lang=fi&id=9#anchor
After updating the acticle again: /#anchor

And now, obviously, the right article cannot be shown.

I done a little search through the code and found something. On /artikle.php there is this line:
$content = str_replace('href="/#','href="index.php?action=artikel&lang='.$LANGCODE.'&id='.$id.'#', $fixedContent);
If I write die($content); after that, I get links right (url is /phpmyfaq/index.php?action=artikel&lang=fi&id=9#anchor). Links remain right even if I call die() and display $content in the very last line of the file. But when links are displayed in user interface they are broken.

Am I doing some mistake or does someone else have this kind of error also?
Thorsten
Posts: 15560
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Re: How do I link to an Anchor in an article?

Post by Thorsten »

Hi,

it seems there's a bug in the editor component. I'll try to implement a patch.

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
johanna
Posts: 23
Joined: Mon Jun 08, 2009 1:09 pm
Location: Finland

Re: How do I link to an Anchor in an article?

Post by johanna »

I still can't create anchors that would stay after updating... Have you found anything to fix this? Anyway, thanks for your concern, Thorsten! (:

Edit June 30, 2009:
I've done a little search... Anchor thing was a bug in TinyMCE before version 3 or so. I downloaded phpMyFAQ RC2 and tested it. Now the editor doesn't add any slashes before the anchor. Though, anchors didn't work before I altered /artikel.php a bit on line 138.

Find this:

Code: Select all

$content = str_replace('href="/#','href="index.php?action=artikel&lang='.$LANGCODE.'&id='.$record_id.'#', $fixedContent);
Replace with this:

Code: Select all

$content = str_replace('href="#','href="index.php?action=artikel&lang='.$LANGCODE.'&id='.$record_id.'#', $fixedContent);
So now slahs in the search string!
Thorsten
Posts: 15560
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Re: How do I link to an Anchor in an article?

Post by Thorsten »

Hi,

is this fix for version 2.5?

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
johanna
Posts: 23
Joined: Mon Jun 08, 2009 1:09 pm
Location: Finland

Re: How do I link to an Anchor in an article?

Post by johanna »

Yes, it is.
Thorsten
Posts: 15560
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Re: How do I link to an Anchor in an article?

Post by Thorsten »

Hi,

thanks, it's fixed in SVN for the RC3 release.

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
johanna
Posts: 23
Joined: Mon Jun 08, 2009 1:09 pm
Location: Finland

Re: How do I link to an Anchor in an article?

Post by johanna »

Thanks a lot, Thorsten! (:
Lidio
Posts: 25
Joined: Mon Dec 10, 2007 9:26 pm

Re: How do I link to an Anchor in an article?

Post by Lidio »

Hi Thorsten,

I believe the above fix for 2.5 and later works when one disables "Activate mod_rewrite support?"

The issue seems to be as follows (or at least as best as I understand it):

in artikel.php

Code: Select all

$fixedContent = str_replace('href="#',
    sprintf('href="index.php?action=artikel&lang=%s&cat=%d&id=%d&artlang=%s#',
        $LANGCODE,
        $currentCategory,
        $faq->faqRecord['id'],
        $LANGCODE),
    $content);
changes the "#AnchorName" to "index.php?action=artikel&lang=en&cat=NN&id=NN&artlang=LL#AnchorName"

and later

Code: Select all

            $newFaqPath       = $oLink->toString();
            $fixedContent     = str_replace($_url, $newFaqPath, $fixedContent);

replaces the url to be SEO friendly. However, the parsing in Link.php breaks the url by the & and then into key / value pairs in the function getHttpGetParameters(). This results in the key artlang having the value LL#AnchorName which includes the in page anchor reference name. When it builds the url to be SEO friendly in the function toString the code

Code: Select all

                        case PMF_LINK_GET_ACTION_ARTIKEL:
                            $url .= PMF_LINK_CONTENT.$getParams[PMF_LINK_GET_CATEGORY].PMF_LINK_HTML_SLASH.$getParams[PMF_LINK_GET_ID].PMF_LINK_HTML_SLASH.$getParams[PMF_LINK_GET_ARTLANG].PMF_LINK_SLASH.$this->getSEOItemTitle().PMF_LINK_HTML_EXTENSION;
                            if (isset($getParams[PMF_LINK_GET_HIGHLIGHT])) {
                                $url .= PMF_LINK_SEARCHPART_SEPARATOR.PMF_LINK_GET_HIGHLIGHT.'='.$getParams[PMF_LINK_GET_HIGHLIGHT];
                            }
                            break;


does not make use of artlang thus dropping the #AnchorName from the url.

Sorry I don't have a patch for you but I hope this helps.

Lidio.
Post Reply