Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Correct syntax for if/then using LIKE 1

Status
Not open for further replies.

revone

Technical User
Mar 10, 2005
3
US
I have an html table with repeating rows in which I want to display a checkmark if the record contains "%clickserve%" in the field $row_rs_link['Intro'] and nothing if it does not. The code works as expected if I change LIKE to == or <>, but php throws a unexpected T_STRING error when I use LIKE. What is the correct syntax?

Here's my code:

Code:
<?php
if($row_rs_recipe['Recipe_Intro'] LIKE "%clickserve%") echo '<img name="Checkmark" src="../images/checkmark.gif" alt="Yes">';
else echo '<img name="Spacer" src="../images/spacer.gif" alt="No">';
?>

Thanks.
 
'LIKE' is used for MySQL comparisions, not PHP. You probably want the strpos() function.
Code:
if (strpos($row_rs_recipe['Recipe_Intro'],'"%clickserve%') !== FALSE) ...

Ken
 
Thanks! This worked like a charm.

Code:
<?php
if(strpos ($row_rs_recipe['Recipe_Intro'], "clickserve") !== FALSE) echo '<img name="Checkmark" src="../images/checkmark.gif" alt="Yes">';
else echo '<img name="Spacer" src="../images/spacer.gif" alt="No">';
?>

Now, how do I expand this to look for "clickserve" in additional fields? I tried this to no avail...

Code:
<?php
if(strpos (($row_rs_recipe['Recipe_Intro'] OR $row_rs_recipe['Recipe_Ing'] OR $row_rs_recipe['Recipe_Inst']), "clickserve") !== FALSE) echo '<img name="Checkmark" src="../images/checkmark.gif" alt="Yes">';
else echo '<img name="Spacer" src="../images/spacer.gif" alt="No">';
?>

This gives no checkmarks. I suspect I am using the mysql term OR when I should be using something else for php.

Suggestions?
 
You have to 'OR' the results of the strpos function.
Code:
<?php
if ((strpos($row_rs_recipe['Recipe_Intro'],'clickserve') !== FALSE) || (strpos($row_rs_recipe['Recipe_Ing'],'clickserve') !== FALSE) || (strpos($row_rs_recipe['Recipe_Inst'],'clickserve') !== FALSE))
     echo '<img name="Checkmark" src="../images/checkmark.gif" alt="Yes">';
else echo '<img name="Spacer" src="../images/spacer.gif" alt="No">';
?>

I think you can use the work OR in PHP, but it's fairly standard to use the '||' symbol for the 'or' operator. The '&&' is used for the 'and' operator.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top