Manually Constructing Links

12 posts by 3 authors in: Forums > CMS Builder
Last Post: May 10, 2010   (RSS)

By mark99 - May 4, 2010 - edited: May 4, 2010

Quick question, I need to link from one viewer detail page to another.

In other words I need to construct a link on this page..


That takes me to this page (same title and record number but the two DETAIL pages interface to different category/fields and templates).


The problem is the NAME-OF-CAT-1 part, well doing the NUM (record number 1) is easy enough but I cannot find an output for Name-of-Cat with the DASHES between each word. It's easy to do this if the title is just a single word/name but once you add space for multiple word titles then you cannot use a 'title' field call because it has spaces rather than dashes between the words.

So right now all I can make is links like NAME OF CAT-1 when what I want is NAME-OF-CAT-1.

Re: [mark99] Manually Constructing Links

By gkornbluth - May 4, 2010

Hi Mark99

If I understand you correctly, here's an excerpt from my CMSB Cookbook that should help;

Sometimes you can run into problems when there’s a space, a dash or underscore between words in a field value. I ran into this recently with an implementation of Lightbox.

My client wanted to use a 2 word value for a field and Lightbox wanted to see a single word with no spaces. If I wasn’t displaying this value in other places on the page I could have put a dash or underscore in the value instead of a space.

I chose to use a dash to make Lightbox happy and to replace the dash with a space where it appeared on the page.

Here’s how for a multi record editor:(thanks to Chris at Interactive Tools for the code syntax)

Just insert the line in red into your code.

<?php foreach ($your_tableRecords as $record): ?>

<?PHP $record['your_field'] = preg_replace("/[-]/", "&nbsp;", $record['your_field'] ); ?>

<?php echo $record['your_field] ?>

<?php endforeach; ?>

Chris explained:

/[-]/ means "match a single character in the set [-]".
preg_replace() will repeat the match as many times as it can.

It’s also possible to match more than one character in the set by adding the additional characters as shown below. Changing the code above to this would replace any occurrence of a dash, an underscore, an r or an x with a space. The replacement will be case sensitive.

<?PHP $record['your_field'] = preg_replace("/[-_rx]/”, "&nbsp;", $record['your_field'] ); ?>

To make the replacement case insensitive, just ad an “i” to the end of the expression, like this:

<?PHP $record['your_field'] = preg_replace("/[-_rx]/i”, "&nbsp;", $record['your_field'] ); ?>

If you wanted to replace 2 different characters with 2 different ones, you’d do it by adding a second line of “preg_replace” code like this:

<?PHP $record['your_field'] = preg_replace("/[-]/", "&nbsp;", $record['your_field'] ); ?>
<?PHP $record['your_field'] = preg_replace("/[_]/", "*", $record['your_field'] ); ?>

The above code will replace any occurrence of a dash ("-") in the string with a space (" "). Then it will replace any occurrence of an underscore ("_") with an asterisk ("*").

For a single record editor, the code syntax would be.

<?PHP $your_tableRecord['your_field'] = preg_replace("/[-_rx]/”, "&nbsp;", $your_tableRecord['your_field'] ); ?>

If you want to replace a complete expression like <br> with another character then remove the forward slashes around the expression

So This:

<?PHP $your_tableRecord['your_field'] = preg_replace("/[-_rx]/”, "&nbsp;", $your_tableRecord['your_field'] ); ?>

Would become this:

<?PHP $your_tableRecord['your_field'] = preg_replace("[-_rx]”, "&nbsp;", $your_tableRecord['your_field'] ); ?>

According to Dave Edis, there are many other uses for “regular expressions”. He commented: “Basically regular expressions are ways of describing patterns. They're used to matching, and replacing content. So this is useful for validating input (such as determining if an url, credit card number, phone number, or email is in the right format), replacing content (stripping html tags, adding extra tags or attributes, etc, or parsing out data (matching all the email addresses in a web page, parsing a CVS file and importing the data, or extracting some data (such as the current temp from a weather web page to use somewhere else).

In PHP all the regular expression functions start with preg_ and in the CMS Builder code base we use those functions in over 200 places. So they come in quite handy. If your web page editor software has a "Find in Files" or search multiple files feature you can search the CMSB code for "preg_" and see some of the ways we use it.”

You can see a list of the PHP functions here:

Here’s a link to a regex cheat sheet:

Hope that works for you.


Jerry Kornbluth
The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at:

Re: [mark99] Manually Constructing Links

By mark99 - May 6, 2010

Ah this worked..

preg_replace("/[ ]/", "-",

Re: [mark99] Manually Constructing Links

By gkornbluth - May 6, 2010

Glad you got it going.

The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at:

Re: [gkornbluth] Manually Constructing Links

By mark99 - May 6, 2010

Yep, of course now I need to figure out how to add that part into my links.

$test1 = $big_catsRecord['title'] = preg_replace("/[ ]/", "-", $big_catsRecord['title']);
$test2 = $big_catsRecord['num'];

$my_output = str_replace("Big Cats", '<a href="**************/Big_Cat_Detail.php?">Big Cats</a>', $my_content);

I obviously need to stick $test1 and $test2 after ..php? in the ahref.

Hmm I'll probably also need to define an array to handle outputs with multiple categories and str_replaces [crazy] .

Re: [mark99] Manually Constructing Links

By gkornbluth - May 6, 2010

It might be easier than you think.

Here's another excerpt from my CMSB Cookbook that may help to get you going;

Here’s how to make sure that your URL’s work, even if the person entering the information doesn’t enter http://

Note: The solution in this recipe is not case sensitive. This means that if you’ve entered HTTP:// , or Http:// the code will still work.

In this example the field containing the URL is called web_site_link_url
and the field containing the link text is called web_site_link_text

If a URL doesn’t start with http:// the link will display incorrectly and won’t work. The if statement checks to see if the URL starts with http:// and if not, it adds that to the beginning of the URL

So, for a multi-record list viewer, you’d put the if statement before the link code in the viewer like this:
<?PHP if (!preg_match("/^http:\/\//i", $record['web_site_link_url'])) {
$record['web_site_link_url'] = "http://" . $record['web_site_link_url']; } ?>

<?PHP endif ?>

<a href="<?PHP echo $record['web_site_link_url'] ?>"><?PHP echo $record['web_site_link_text'] ?></a>

And for a detail viewer:

<?PHP if (!preg_match("/^http:\/\//i", $yourRecord['web_site_link_url'])) {
$yourRecord['web_site_link_url'] = "http://" . $yourRecord['web_site_link_url']; } ?>

<a href="<?php echo $yourRecord['web_site_link_url'] ?>"><?php echo $yourRecord['web_site_link_text'] ?></a>


Jerry Kornbluth
The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at:

Re: [gkornbluth] Manually Constructing Links

By mark99 - May 6, 2010 - edited: May 6, 2010

I'm not sure that's quite what I'm looking for.

You see I have a DETAIL VIEWER page for each of my categories..




Now on each of these individual pages I output a list of related categories e.g. "Big Cats, Small Cats" (exactly like that, without the speechmarks.. obviously).

So to link with each of these what I've been asking for above is to construct a URL that will allow me to add links like this..


[url ""]http:/big_cats_detail.php?The-Minx-1[/#0066cc][/url]

With your help I can now convert the TITLE field into "The-Minx" and then adding "-1" (i.e. from the record NUM field) should allow me to complete an automatic URL once I figure out how to construct the TITLE and NUM into a single URL after the big/small_cats_detail.php? part (see above two examples). So I cannot use any CMSB fields except the converted TITLE and of course NUM. Now I just need to figure out how to add those two bits on to the end of my URL and then find out how to do a str_replace for several different category titles to automatically link them with the modified URLs.

In other words the end result would be "Big Cats, Small Cats" displaying with automatic links to the related URLs above.

Re: [mark99] Manually Constructing Links

By mark99 - May 6, 2010

Made some progress by using this:

$urltest1 = "<a href=[url ""][/#0066cc][/url]"."$numx".">"."Testing"."</a>";
$urltest2 = "<a href=[url ""][/#0066cc][/url]"."$numx".">"."Testing2"."</a>";
$my_output = str_replace("Testing", "$urltest1", $my_content);

Of course the problem with this is that I can only use $my_output once, again meaning I need an array for "Testing","Testing2" but I have no idea how to plug one into this.

Re: [mark99] Manually Constructing Links

By Jason - May 7, 2010


So, if I understand your problem correctly, you load a page, ex: cat_detail.php?NAME-OF-CAT-1
and on that page you want to construct a link to

Is that right?

It looks there may be an easier way to do this, since all you're trying to do is append a query string (everything after the "?" ) to the end of the another url.

Try something like this:

<a href="big_cat_detail.php?<?php echo @$_SERVER['QUERY_STRING']?>">Big Cat Detail</a>

This will take the string "NAME-OF-CAT-1" (or whatever the current query string happens to be) and append it onto the end of our new link (big_cat_detail.php?NAME-OF-CAT-1). All of the dashes will still be in place.

I hope that helps.
Jason Sauchuk - Project Manager

Hire me! Save time by getting our experts to help with your project.