[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP echo conditional statement?

Posted on 2011-10-16
12
Medium Priority
?
441 Views
Last Modified: 2012-05-12
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



0
Comment
Question by:pda4me
12 Comments
 
LVL 12

Expert Comment

by:larsrohr
ID: 36976859
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
 
LVL 35

Expert Comment

by:Norie
ID: 36976926
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
 

Author Comment

by:pda4me
ID: 36977216
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 12

Expert Comment

by:larsrohr
ID: 36977273
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
 

Author Comment

by:pda4me
ID: 36977344
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
 

Author Comment

by:pda4me
ID: 36977354
example:  B01,R12,H98
0
 
LVL 12

Expert Comment

by:larsrohr
ID: 36977413
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
 

Author Comment

by:pda4me
ID: 36977455
Close!  How do I get rid of the  commas, this is the output?

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Eastern exposure, , , , , , , , , , , , , , , ,
0
 
LVL 7

Expert Comment

by:Sandeep Kothari
ID: 36977744
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
 
LVL 12

Accepted Solution

by:
larsrohr earned 2000 total points
ID: 36977848
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
 
LVL 11

Expert Comment

by:mattibutt
ID: 36978149
Simple assign each variable a value and call them when the condition meet
0
 

Author Closing Comment

by:pda4me
ID: 36979809
Thanks Lars, AWESOME HELP!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question