It is the client (e.g. browser or search engine robot) that resolves relative links on the pages it displays in order to produce the canonical URLs that it requests when you click on one of those links.
Your php is publishing a page-relative link. The browser looking at the page already thinks that it is looking at a page at example.com/backgroundcategory/something (look at the browser address bar to confirm). So, when it sees a relative link of <href="backgroundcategory/somethingelse"> on that page and you click on that link, the browser strips off the "currently-being-viewed-page path" ("something") from the location showing in its address bar, and then adds the contents of the relative link, resulting in a requested URL of /backgroundcategory/backgroundcategory/somethingelse
Each time you click another relative link on a page previously requested by a relative link, you end up going another "/backgroundcategory" level deeper.
The usual solution is to use a server-relative link, instead of a page-relative link:
echo "<a href=\"/backgroundscategory/$categorylink\" class=\"backgroundsfg\">$categorylink</a><br />";
When you do, this, the leading slash tells the browser to discard everything except the protocol and domain in its address bar, and then add the server-relative path to that in order to resolve the canonical URL.
(You could also echo-out a full, canonical URL of "http://example.com/backgroundcategory/$categorylink" if you wanted to. In that case, the browser replaces the entire URL in its address bar.)
See if that helps...