PHP echo conditional statement?

I have a PHP echo statement that shows up like this and displays a value from a MySQL database::

<?php echo($row['FEATURE_CODES']); ?>

Open in new window


How do I rewrite this PHP statement where if it displays one of the following it shows text instead of the code?

R01 displays Out of city limits
R02 displays Rural property
R03 displays Located in subdvision
R04 displays Eastern exposure
R05 displays Northeastern exposure
R06 displays Northern exposure
R07 displays Northwestern exposure
R08 displays Southeastern exposure
R09 displays Southern exposure
R10 displays Southwestern exposure
R11displays Western exposure
R12 displays Located in historical district



pda4meAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

larsrohrCommented:
Sounds like you're looking for a simple 'switch' statement:
 
<?php
switch ($row['FEATURE_CODES']) {
  case "R01":
        echo "Out of city limits";
        break;
  case "R02":
        echo "Rural property";
        break;
  case "R03":
        echo "Located in subdvision";
        break;
  case "R04":
        echo "Eastern exposure";
        break;
  case "R05":
        echo "Northeastern exposure";
        break;
  case "R06":
        echo "Northern exposure";
        break;
  case "R07":
        echo "Northwestern exposure";
        break;
  case "R08":
        echo "Southeastern exposure";
        break;
  case "R09":
        echo "Southern exposure";
        break;
  case "R10":
        echo "Southwestern exposure";
        break;
  case "R11":
        echo "Western exposure";
        break;
  case "R12":
        echo "Located in historical district";
        break;
}
?>

Open in new window

0
NorieVBA ExpertCommented:
Is the text you want to display for each code a field in query you've run?

Or can is there a table in the database that links the code to the text you want to display?
0
pda4meAuthor Commented:
i tried to replace the echo statement with the switch and it displays nothing?  What am I doing wrong.

Imnorie: Yes, its a value returned from a database.  There is no table to link the code to the text.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

larsrohrCommented:
Perhaps you should add a final default case check, e.g.:

  case "R12":
        echo "Located in historical district";
        break;
  default:
        echo "Match not found for: " . $row['FEATURE_CODES'];

}
0
pda4meAuthor Commented:
I just realized that the data has multiple values in the column separated by a comma with other codes as well...how do I match it with multiple values.
0
pda4meAuthor Commented:
example:  B01,R12,H98
0
larsrohrCommented:
Ah, then you'll need to bust those codes out separately, and then you can substitute the relevant text for each of them.
And you'll want to figure out how to display multiple items (separate lines, or commas between, ...)

Something like:

 
<?php
$mycodes = explode(",", $row['FEATURE_CODES']);
$mysize = count($mycodes);
$item_num = 0;

foreach($mycodes as $code) {
 switch ($code) {
  case "R01":
        echo "Out of city limits";
        break;

  // more cases here ...

  case "R12":
        echo "Located in historical district";
        break;
 }  //end switch

 $item_num += 1;
 if ($item_num < $mysize) echo ", ";

}  //end for loop
?>

Open in new window



It sounds like you may have lots of codes to deal with.  As imnorie sort of suggested, having all the text in another table would be helpful, so you don't have to have it all in the code here.
0
pda4meAuthor Commented:
Close!  How do I get rid of the  commas, this is the output?

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Eastern exposure, , , , , , , , , , , , , , , ,
0
Sandeep KothariProject LeadCommented:
create an array ... with key as code and value as display text .. and all will work fine...'

something like this...
<?php 
$arr =array("R01"=> "displays Out of city limits","R02"=> "displays Rural property");
$c = $row['FEATURE_CODES'];
echo $arr[$c];
?>

Open in new window

0
larsrohrCommented:
As for the commas -- the code I provided should put a comma between the text output for each of the feature codes, but you must have first provided text corresponding to every possible feature code.  Do you have lots of feature codes for this item?  Are there lots of feature codes to be prepared for?
You've suggested text for feature codes R01 through R12, but you've also indicated that codes B01 and H98 can come up.

Using kshna's suggestion of starting with an array of your feature codes, the following code snippet may help you.
It is easy to add more codes to the array, and now we are testing for empty entries (no corresponding feature code found in the array), and keeping track of whether a comma is needed between outputs.

Getting more creative in your output may be best left to you.  :-)
You can find lots of info at http://www.php.net/manual/en/langref.php
<?php
$mycodes = explode(",", $row['FEATURE_CODES']);

$arr = array(   "R01" => "Out of city limits",
                "R02" => "Rural property",
                "R03" => "Located in subdvision",
                "R04" => "Eastern exposure",
                "R05" => "Northeastern exposure",
                "R06" => "Northern exposure",
                "R07" => "Northwestern exposure",
                "R08" => "Southeastern exposure",
                "R09" => "Southern exposure",
                "R10" => "Southwestern exposure",
                "R11" => "Western exposure",
                "R12" => "Located in historical district"
        );

$printedsomething=0;

foreach($mycodes as $code) {
 if ($arr[$code] != '' ) {
   if ($printedsomething==1)  echo ", " . $arr[$code];
    else  echo $arr[$code];
   $printedsomething=1;
 }
}  //end for loop
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mattibuttCommented:
Simple assign each variable a value and call them when the condition meet
0
pda4meAuthor Commented:
Thanks Lars, AWESOME HELP!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.