Page 1 of 1

If operators

Posted: Mon Feb 18, 2008 4:34 pm
by micknc
I need an if statement that would be true if a field only contained the variable and not equaled it.

I have a table with sales reps and their preferred method of contact. If it is emailed it always has an E in the string, faxed is F and mailed is P (for print it). The problem is that (for various reasons) there could be several combinations to the string:

1PI Account that was generated through internet sales that wants mail
1PH House account that prefers mail
2FI Internet account that wants faxes
2FH House account that wants faxes

There are dozens of these but always with E, P, F. Different sales reps get different numbers in front and the last number is the place the sale was generated.

Now the if statement would be something like:

Code: Select all

 
if  ($rowcust['REP_ID'] = "E"){
             $email=$rowcust['E_MAIL'];}
elseif ($rowcust['REP_ID'] = "F"){
             $email=$rowcust['FAX'];}
else {$email=$rowcust['PRINT'];}
 
Except "=" will not work. Is there a "contains" operator. I have been searching but I haven't found anything yet. I know there is an "or" operator but that seems like a poor solution as these strings could change with new additions.

A little addition here:
I am always looking at the second letter of the string so that may be a way to do this. Format it so that the first and last letter are striped and then compare whats left?

Re: If operators

Posted: Mon Feb 18, 2008 5:02 pm
by micknc
Found a code that will strip the first and last off:

Code: Select all

 
        $rep_id = substr(trim($rep_id_raw), 1, -1);
 

Re: If operators

Posted: Mon Feb 18, 2008 5:09 pm
by califdon
Are you perhaps looking for the LIKE operator, with % wildcard? http://www.htmlite.com/mysql011.php

If you want to dig out more complex string elements, you might need regular expressions. http://dev.mysql.com/doc/refman/5.0/en/regexp.html

Re: If operators

Posted: Mon Feb 18, 2008 5:26 pm
by Benjamin
Sounds like a job for stristr()

http://us.php.net/manual/en/function.stristr.php

Re: If operators

Posted: Mon Feb 18, 2008 6:12 pm
by micknc
I tried the % but couldn't get anything to fly.

I finally used substr and it is working great. Now I just hope they don't change the format on me one day.

Re: If operators

Posted: Mon Feb 18, 2008 6:15 pm
by micknc
On a related note I am now trying to format the fax correctly so that our fax server will accept it. Basically the server wants something like 'XXX-XXX-XXXX'<faxserver@ourdomain.com>
It was also in the wrong format so I used the same deal and I have:

Code: Select all

 
        // remove last 9 characters from the string leaving the area code
        $area_code = substr(trim($fax_raw), 0, -9);
        // make the last 8 digits for later
        $seven_digits = substr(trim($fax_raw), 4, 8);
        // put the area code and last digits together with the correct format
        $fax=("'" . $area_code . "-" . $seven_digits . "'" . "<fax@domain.com>");
 
If I keep the < tags the email disappears completely. I need to find a way to include the < and > in the output to the email.