Skip to main content
November 30, 2007
Answered

convert asp code to php

  • November 30, 2007
  • 11 replies
  • 1006 views
i am new to php, transferring from asp and would like help in converting some code i have used with asp?

it is a function code to hilight results from a database search.
this is the code in asp

<%

Function HilightResults(sDataValue, sSearchString)
HilightResults = Replace(UCase(sDataValue), UCase(sSearchString), "<span style='color:red;'>" & UCase(sSearchString) & "</span>")
End Function

%>

<td class="WADAResultsTableCell"><%=HilightResults((WADALABEL1.Fields.Item("Item_number").Value), Request("S_Item_number"))%></td>

thanks for your help
Jim Balthrop
JBWebWorks.com
This topic has been closed for replies.
Correct answer Newsgroup_User
jim balthrop wrote:
> when you search with any of the other search text fields, the return for
> column 'mfg_name' is '-1'

I'm working completely in the dark here, because I don't know what
variables you're using to retrieve the data from the database, or what
the raw data looks like. However, I think the following might do what
you want:

<?php
function highlightResults($dataValue, $searchString) {
return preg_replace("/$searchString/i", '<span
style="color:red;">'.strtoupper($searchString).'</span>', $dataValue);
}
?>

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/

11 replies

Inspiring
December 4, 2007
David Powers wrote:

> jim balthrop wrote:
>
>> I just bought PHP SOLUTIONS on Amazon
>
>
> Thanks, Jim. I hope you find it useful.
>
I think he may do so....
Mick
Inspiring
December 4, 2007
jim balthrop wrote:
> I just bought PHP SOLUTIONS on Amazon

Thanks, Jim. I hope you find it useful.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
December 4, 2007
I just bought PHP SOLUTIONS on Amazon

thanks again for your help
Jim Balthrop
Inspiring
December 3, 2007
jim balthrop wrote:
> that did the trick

Hooray!

> one last request - the results highlight the searchstring and put the
> searchstring in CAPS, but the datavalue is not in CAPS.
> where should the strtroupper be placed to accomplish that?

Rewrite the function like this:

function highlightResults($dataValue, $searchString) {
return strtoupper(preg_replace("/$searchString/i", '<span
style="color:red;">'.strtoupper($searchString).'</span>', $dataValue));
}

> What do you want for Christmas?

Lots more people to buy my books! ;-)

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
December 3, 2007
Thank you, David - I really appreciate your help
that did the trick
//used this as the function
<?php
function highlightResults($dataValue, $searchString) {
return preg_replace("/$searchString/i", '<span
style="color:red;">'.strtoupper($searchString).'</span>', $dataValue);
}

//and this is the call function
<?php echo highlightResults($row_WADpr_part["mfg_name"],
stripslashes($_GET["S_mfg_name"])); ?>

changed the ($_GET["to match the search text field"]

one last request - the results highlight the searchstring and put the searchstring in CAPS, but the datavalue is not in CAPS.
where should the strtroupper be placed to accomplish that?

What do you want for Christmas?
thanks again,
Jim Balthrop

Inspiring
December 3, 2007
jim balthrop wrote:
> when i change to the call function code you suggested, i get the proper
> results, but no value prints under the 'mfg_name' column?

Sorry, the return statement was missing from the function. It should
look like this:

function highlightResults($dataValue, $searchString) {
return str_replace(strtoupper($dataValue), strtoupper($searchString),
'<span style="color:red;">'.strtoupper($searchString).'</span>');
}

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
December 3, 2007
thanks again David

still not quite right - here is what i have now

//function code
<?php
function highlightResults($sDataValue, $sSearchString) {
return str_replace(strtoupper($sDataValue), strtoupper($sSearchString), '<span style="color:red;">'.strtoupper($sSearchString).'</span>');
}
?>

//call function highlightResults
<td class="WADAResultsTableCell"><?php echo highlightResults($row_WADpr_part["mfg_name"],stripslashes ($_GET["S_mfg_name"])); ?>

It now returns the input of the search text field highlighted in red, but it does not return all of the data in the 'mfg_name' column.
and when you search with any of the other search text fields, the return for column 'mfg_name' is '-1'

you can see this at
http://www.bellchemicalservice.com/homepage/service_searchparts.php
advance is the name of a manufacturer, so put in 'adva' in the MANUFACTURER box and see the return (i want it to return 'ADVANCE' in the manufacturer column with 'ADVA' highlighted in red).
do a new search, then put in 'buffer' in the MACHINE TYPE box and see the '-1' return in the manufacturer column.

jim

December 3, 2007
<td class="WADAResultsTableCell"><%=HilightResults((WADALABEL1.Fields.Item("Item_number").Value), Request("S_Item_number"))%></td>

the ("S_item_number") is the input name of the text field on the search page.

new development -
i was getting the missing argument 2 message on my testing server
so i put the page on line so you could view it and the value echos without the warning message; but does not highlight.
//this is the call function code that produces that result
<td class="WADAResultsTableCell"><?php echo highlightResults($row_WADApr_parts['mfg_name'].$Value), $_GET['S_mfg_name']; ?></td>


when i change to the call function code you suggested, i get the proper results, but no value prints under the 'mfg_name' column?

<td class="WADAResultsTableCell"><?php echo highlightResults($row_WADApr_parts['mfg_name'],stripslashes($_GET['S_mfg_name'])); ?></td>

to see on line go to
http://www.bellchemicalservice.com/homepage/service_searchparts.php
and put in advance in the MANUFACTURER text box. (NOTE- the strtoupper() does not seem to work; put in ADVancE and it will echo just like that.
also - put in VAC in the machine type and the value returned for 'mfg_name' is -1?

David, thanks for your help
Inspiring
December 2, 2007
On Sun, 2 Dec 2007 01:08:29 +0000 (UTC), "jim balthrop"
<webforumsuser@macromedia.com> wrote:

> here is what i have now
> <?=$highlightResults[($row_WADpr_parts["mfg_name"].$Value),
>REQUEST("S_mfg_name")]?>


Should be:

<?php echo highlightResults($row_WADpr_parts["mfg_name"].$Value,
REQUEST("S_mfg_name")]?>

Gary
Inspiring
December 2, 2007
jim balthrop wrote:
> here is what i have now
> <?=$highlightResults[($row_WADpr_parts["mfg_name"].$Value),
> REQUEST("S_mfg_name")]?>

$ is used to indicate a variable. highlightResults() is a function, not
a variable. Parentheses are used for functions; square brackets for
arrays. You have also got a weird combination of pseudo-PHP and ASP
styles in there. At a guess, what you're trying to write is this:

<?= highlightResults($row_WADpr_part["mfg_name"],
$_REQUEST["S_mfg_name"]); ?>

However, in the interests of portability, I would suggest using the full
PHP opening tag with echo instead of the shorthand <?=, which isn't
supported on all servers:

<?php echo highlightResults($row_WADpr_part["mfg_name"],
$_REQUEST["S_mfg_name"]); ?>

$_REQUEST is a superglobal array that contains all the variables from
GET (passed through a URL query string), POST (from a form sent using
the post method), and cookies. If the information is always coming from
the same source, it's better to use the dedicated superglobal instead of
$_REQUEST. Replace $_REQUEST with $_GET for query string variables, or
$_POST for form variables.

A possible gotcha is a configuration setting known as "magic quotes".
Although magic quotes are now generally considered to be a bad thing,
and are due to be removed from the next version of PHP, most hosting
companies have magic quotes switched on. If magic quotes are switched
on, and the values passed through a query string or form are likely to
contain single or double quotes, you need to pass such values to a
function called stripslashes(). If that's the case, the code for a
variable passed through a query string becomes this:

<?php echo highlightResults($row_WADpr_part["mfg_name"],
stripslashes($_GET["S_mfg_name"])); ?>

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
December 2, 2007
thanks for your help David

here is where i am now with the following code
//function code
<?php

function highlightResults($dataValue, $searchString) { str_replace(strtoupper($dataValue), strtoupper($searchString), '<span style="color:red;">'.strtoupper($searchString).'</span>');
}
?>

//and call function code
<td class="WADAResultsTableCell"><?php echo highlightResults($row_WADApr_parts["mfg_name"].$Value),$_GET["S_mfg_name"]; ?></td>

and i get this error message
Warning: Missing argument 2 for highlightResults(), called in C:\wamp\www\bellchemicalservice\pr_parts_Results.php on line 378 and defined in C:\wamp\www\bellchemicalservice\pr_parts_Results.php on line 184



Jim
December 2, 2007
thanks David

here is what i have now
<?=$highlightResults[($row_WADpr_parts["mfg_name"].$Value), REQUEST("S_mfg_name")]?>

i get syntax error
Parse error: syntax error, unexpected ',', expecting ']' in C:\wamp\www\bellchemicalservice\pr_parts_Results.php on line 376