1

Hi there in my database I have 3 columns, is_contract, is_permenant and is_temporary. Within these columns there is either a Y or N value.

I am using these columns to echo onto the page what kind of work someone is looking for, my problem is that the user can be looking for more than one type of work, I am currently running 3 if statements to determine what to echo to the page, however I am struggling to add a comma if more than one of the statemnts returns as true, below is my code so far,

<?php
    if($rslt['is_contract'] == 'Y') {
        echo "Contract ";
}
    if($rslt['is_permanent'] == 'Y') {
        echo "Permanent ";
}
if($rslt['is_temporary'] == 'Y') {
    echo "Temporary";
}
?>
1
  • if is a conditional statement and not a loop. a loop is something completely different Commented Apr 16, 2010 at 9:11

2 Answers 2

5
<?php
    $out=array();
    if($rslt['is_contract'] == 'Y') $out[]="Contract";
    if($rslt['is_permanent'] == 'Y') $out[]="Permanent";
    if($rslt['is_temporary'] == 'Y') $out[]="Temporary";
    echo implode(", ",$out);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

That's such a better way than my count the terms then work out the number of commas, then sub them in method...
0

Either you can use like this in simple way

if($rslt['is_contract'] == 'Y') {
        echo "Contract ";
}
    if($rslt['is_permanent'] == 'Y') {
      if($rslt['is_contract'] == 'Y') {
        echo ", ";
      }
        echo "Permanent ";
      if($rslt['is_temporary'] == 'Y') {
        echo ", ";
      }
}
if($rslt['is_temporary'] == 'Y') {
    if($rslt['is_contract'] == 'Y' && $rslt['is_permanent'] != 'Y') {
        echo ", ";
      }
    echo "Temporary";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.