If operators

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

If operators

Post 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?
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

Re: If operators

Post 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);
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: If operators

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: If operators

Post by Benjamin »

Sounds like a job for stristr()

http://us.php.net/manual/en/function.stristr.php
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

Re: If operators

Post 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.
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

Re: If operators

Post 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.
Post Reply