Helper only works once.

0

I have a display helper that works perfectly - but only once per page.

If I use it on a detail page, no problems.

However if I use it on a list page, it only works the first time and then the rest of the page fails to generate.

Here's the code, any pointers greatly appreciated.

<pre> <?php

/* * URL Encoder script. * Version 1.0 - February 2002 * Version 1.1 - December 2007 - added encodelink_delayed - a delayed action on decoding * Version 1.2 - August 2008 - updated encodelink_delayed to allow it to be used multiple times in a page * (c) 2002-2008, Paul Gregg <pgregg@pgregg.com> * http://www.pgregg.com */

Function transpose($str) { # function takes the string and swaps the order of each group of 2 chars $len = strlen($str); $ret = ""; for ($i=0; $i<$len; $i=$i+2) { if ($i+1 == $len) $ret .= substr($str, $i, 1); else $ret .= sprintf("%s%s", substr($str, $i+1, 1), substr($str, $i, 1)); } return $ret; }

Function escapeencode ($str) { $ret = ""; $arr = unpack("C*", $str); foreach ($arr as $char) $ret .= sprintf("%%%X", $char); return $ret; }

Function encodehash($href, $text) { $prepend = ""; if (preg_match("/^mailto:/", $href)) { $href = preg_replace("/^mailto:/", "", $href); $prepend = "mailto:"; } if (preg_match("/^http:\/\//", $href)) { $href = preg_replace("/^http:\/\//", "", $href); list($server,$url) = split("/", $href, 2); $href = $url; $prepend = "http://$server/"; } $UserCode = sprintf("<a href=\"%s%s\">%s</a>", $prepend, escapeencode($href), $text); return $UserCode; }

Function encodelink($href, $text) { $code = sprintf("var s='%s';var r='';for(var i=0;i<s.length;i++,i++){r=r+s.substring(i+1,i+2)+s.substring(i,i+1)}document.write('<a href=\"'+r+'\">%s</a>');", transpose($href), $text); $UserCode = sprintf("%s%s%s", "<SCRIPT type=\"text/javascript\">eval(unescape('", escapeencode($code), "'))</SCRIPT>"); return $UserCode; }

Function encodelink_delayed($href, $text) { static $usecount = 0; $usecount++; $code = sprintf("function pgregg_transpose%d(h) {var s='%s';var r='';for(var i=0;i<s.length;i++,i++){r=r+s.substring(i+1,i+2)+s.substring(i,i+1)}h.href=r;}document.write('<a href=\"#\" onMouseOver=\"javascript:pgregg_transpose%d(this)\" onFocus=\"javascript:pgregg_transpose%d(this)\">%s</a>');", $usecount, transpose($href), $usecount, $usecount, $text); $UserCode = sprintf("%s%s%s", "<SCRIPT type=\"text/javascript\">eval(unescape('", escapeencode($code), "'))</SCRIPT>"); return $UserCode; }

$encoded = encodelink('mailto:'.$value, $value);

echo $encoded;

?> </pre>

asked Feb 6 '10 at 3:58

jackson

33

add comment
enter at least 15 characters

3 Answers

0

The script is probably throwing an error because you're trying to define those functions more than once.

Wrap your functions with something like <pre>if ( !function_exists('transpose') ) { ... } </pre>

answered Feb 6 '10 at 2:27

tokyograph

41

add comment
enter at least 15 characters
0

Yeah, tokyoograph, that is exactly the problem. Each time the PHP runs it remembers any functions already defined, so after this has run once it will error out and output nothing.

answered Feb 6 '10 at 2:29

sc0ttkclark

2936

add comment
enter at least 15 characters
0

Awesome, you guys rock.

answered Feb 6 '10 at 3:58

jackson

33

add comment
enter at least 15 characters