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..

cat_detail.php?NAME-OF-CAT-1

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

big_cat_detail.php?NAME-OF-CAT-1

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 http://www.thecmsbcookbook.com that should help;

REPLACING ONE CHARACTER WITH ANOTHER USING “REGULAR EXPRESSIONS”
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:

http://ca3.php.net/manual/en/ref.pcre.php

Here’s a link to a regex cheat sheet:

http://www.bitcetera.com/en/techblog/2008/04/01/regex-in-a-nutshell/

Hope that works for you.

Best,

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: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Manually Constructing Links

By mark99 - May 6, 2010

I tried this on my Detail Viewer page and it just returned the title with a space as normal:

<?PHP
$big_catsRecord['title'] = preg_replace("/[-]/", "&nbsp;", $big_catsRecord['title'] );
echo $big_catsRecord['title'];
?>

My title was (example) Super Minx and the result: Super Minx , not Super-Minx as I had hoped.

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.

Jerry
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: http://www.thecmsbcookbook.com/trial.php

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 http://www.thecmsbcookbook.com that may help to get you going;

DISPLAY LINKS ON YOUR WEB PAGE WITHOUT ERRORS
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>


Best,

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: http://www.thecmsbcookbook.com/trial.php

Re: [mark99] Manually Constructing Links

By mark99 - May 6, 2010

Made some progress by using this:

$urltest1 = "<a href=[url "http://www.bleh.co.uk/ISP_Detail.php?num-"]http://www.bleh.co.uk/ISP_Detail.php?num-[/#0066cc][/url]"."$numx".">"."Testing"."</a>";
$urltest2 = "<a href=[url "http://www.bleh.co.uk/ISP_Detail.php?num-"]http://www.bleh.co.uk/ISP_Detail.php?num-[/#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

Hi,

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
big_cat_detail.php?NAME-OF-CAT-1.

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
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/