This topic is locked
[SOLVED]

 How to prepare a search string?

12/4/2012 3:47:17 PM
PHPRunner General questions
S
shep author

Using the custom view on a field, I prepare the text as a hyper link to another record on another page. This works great except in the cases where the text contains special characters (a + in my particular case) where it fails to find the linked record. I suspect other special characters might also have an influence. For example, the custom code resolves to:
bla-bla/Defaults_list.php?q=(sDefaultDescription~equals~Swipe Bingo EGM+Server)
The linked record has as its text in the sDefaultDescription column "Swipe Bingo EGM+Server". If I replace that with "Swipe Bingo EGM and Server", the search succeeds as it does with all other text not containing a plus sign. I conclude the search function is interpreting the '+' to mean something other than plain text. My question is how should I prepare the text in the link?
If I enclose the text in double quotes as in ?q=(sDefaultDescription~equals~"Swipe Bingo EGM+Server"), it gets omitted entirely in the hyperlink (i.e. all that shows is ?q=(sDefaultDescription~equals~). If I enclose the text in single quotes as in ?q=(sDefaultDescription~equals~'Swipe Bingo EGM+Server'), it fails to find the target and I surmise it is because the single quotes are assumed part of the text. Suggestions?
Thanks in advance.

Sergey Kornilov admin 12/4/2012

Special characters in URL should be encoded. Here are a few examples: http://www.w3schools.com/tags/ref_urlencode.asp
My suggestion is to run those searches manually first to see what search URL they build. You can copy and paste URLs to your app then.

S
shep author 12/4/2012

Is there a function in php to do this? The text is arbitrary user input. I.e. my custom view code is mostly:
$value = formLink($value,"~equals~");
what I believe I need is:
$value = formLink(escape_HTML_Function($value),"~equals~");

S
shep author 12/4/2012

Never mind. I figured it out. Here is my function to create a link:



// pageBase = name of top level

// mastersPage = name of page to go to

// key = name of column to search

// compare = text of compare

// sMac = text to search for

// text = text to display in the link

function LinkToVer62($pageBase,$mastersPage,$key,$compare,$sMac,$text)

{

if ( $_SESSION["REQUEST_URI"] != NULL )

$prefix = $_SESSION["REQUEST_URI"] . $mastersPage;

else

{

$prefix = "http://".$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT";];

$prefix .= "/" . $pageBase . "/" . $mastersPage;

}

$prefix = '<a href="' . htmlspecialchars( $prefix . "?q=(" . urlencode($key . $compare . $sMac)) . ')">';

return $prefix . $text . "</a>";

}


For example, the custom view I use with it (in dozens of places in my application):



global $sys_dbname; // Set at the user's login. Is the home page for the app.

$key = "sDefaultDescription"; // These two entries are unique to each custom view.

$page = "Defaults_list.php";

$value = LinkToVer62($sys_dbname,$page,$key,"~equals~",$value,$value)