0

Here is my code,

if ($_SESSION["crossfit"]=="Y") { if ($_SESSION["tr_duration1"]==1) { $dr1="1 Month"; } else {
if ($_SESSION["tr_duration1"]==12) { $dr1="1 Year"; } else
   { $dr1=$_SESSION["tr_duration1"] . ' Months'; }} $c1='Crossfit - ' . $dr1;}

if ($_SESSION["muaythai"]=="Y") { if ($_SESSION["tr_duration2"]==1) { $dr2="1 Month"; } else {
if ($_SESSION["tr_duration2"]==12) { $dr2="1 Year"; } else
   { $dr2=$_SESSION["tr_duration2"] . ' Months'; }} $c2=', Muaythai - ' . $dr2;}

if ($_SESSION["mma"]=="Y") { if ($_SESSION["tr_duration3"]==1) { $dr3="1 Month"; } else {
if ($_SESSION["tr_duration1"]==12) { $dr3="1 Year"; } else
   { $dr3=$_SESSION["tr_duration3"] . ' Months'; }} $c3=', MMA - ' . $dr3;}

if ($_SESSION["gymnastics"]=="Y") { if ($_SESSION["tr_duration4"]==1) { $dr4="1 Month"; } else {
if ($_SESSION["tr_duration4"]==12) { $dr4="1 Year"; } else
   { $dr4=$_SESSION["tr_duration4"] . ' Months'; }} $c4=', Gymnastics - ' . $dr4;}

$for = $c1 . $c2 . $c3 . $c4 . '.';

At least one of the programs will be 'Y' or all of them.

What I want is that $c1 $c2 $c3 $c4 come out separated by ',' and ends with '.' so it becomes readable.

My problem is if I put

$for = $c1 . $c2 . $c3 . $c4 . '.'; and the ',' with $c1 and select programs 2 and 3

I get

$for = , Muaythai - 3 Months, MMA - 12 Months.

of course I want,

$for = Muaythai - 3 Months, MMA - 12 Months.

If I put ',' in the $for directly and remove from $c1 if 2nd and 3rd programs are selected

$for = $c1 . ',' . $c2 . ',' . $c3 . ',' . $c4 . '.'

I get

$for = ,Muaythai - 3 Months, MMA - 12 Months,.

I want

$for = Muaythai - 3 Months, MMA - 12 Months.

I have also tried putting nesting so many isset's but haven't succeeded. I have also tried by making a 'for' loop but ended up with more syntax errors than output errors

It works fine only if I select only the first program or all the 4 programs.

1
  • use a single string and concate all values to it. Commented May 6, 2014 at 9:41

2 Answers 2

3

Try something like this:

$tmp = array($c1,$c2,$c3,$c4);
$filtered = array_filter($tmp);
$for = implode(",", $filtered).".";

This will strip out any $c_ variables that aren't populated, and separate them with commas.


EDIT: I have reivewed your code, and would like to suggest this optimised version:

$keys = array("Crossfit","Muaythai","MMA","Gymnastics");
$result = array();
foreach($keys as $i=>$name) {
    if( $_SESSION[strtolower($name)] == "Y") {
        $duration = $_SESSION['tr_duration'.($i+1)];
        switch($duration) {
            case 1:  $dur = "1 Month"; break;
            case 12: $dur = "1 Year";  break;
            default: $dur = $duration." Months";
        }
        $result[] = $name." - ".$dur;
    }
}
if( !$result) $for = "Nothing selected!";
else $for = implode(", ",$result).".";

If you need help understand what's going on here, feel free to ask!

Sign up to request clarification or add additional context in comments.

6 Comments

That was awesome.. however I didn't understand anything what you did, because I am new in PHP so I will go and read the documentation. Thanks a lot.
No problem :) php.net/array-filter should help. Basically, by not passing a callback function, it just checks if they are truthy. php.net/implode is invaluable.
@huz_akh: The first statement stores all the variables $c1 through $c4 into an array named $tmp. array_filter() function then removes empty values. implode() joins the pieces in the array to a single string delimited by commas.
@huz_akh I've edited my answer to include a reviewed version of your code. Take a look!
@NiettheDarkAbsol Thanks for the code, I have used it several times in my program. It helped me strip down my entire program code from 1500 lines to 600 lines ..
|
0

you can do it using single variable and concate values to it.

try like this:

for = "";
if ($_SESSION["crossfit"] == "Y") {
    if ($_SESSION["tr_duration1"] == 1) {
        $dr1 = "1 Month";
    } else {
        if ($_SESSION["tr_duration1"] == 12) {
            $dr1 = "1 Year";
        } else {
            $dr1 = $_SESSION["tr_duration1"] . ' Months';
        }
    }
    $for .= 'Crossfit - ' . $dr1;
}

if ($_SESSION["muaythai"] == "Y") {
    if ($_SESSION["tr_duration2"] == 1) {
        $dr2 = "1 Month";
    } else {
        if ($_SESSION["tr_duration2"] == 12) {
            $dr2 = "1 Year";
        } else {
            $dr2 = $_SESSION["tr_duration2"] . ' Months';
        }
    }
    $for .= empty($for) ? 'Muaythai - ' . $dr2 : ', Muaythai - ' . $dr2;
}

if ($_SESSION["mma"] == "Y") {
    if ($_SESSION["tr_duration3"] == 1) {
        $dr3 = "1 Month";
    } else {
        if ($_SESSION["tr_duration1"] == 12) {
            $dr3 = "1 Year";
        } else {
            $dr3 = $_SESSION["tr_duration3"] . ' Months';
        }
    }
    $for .= empty($for) ? 'MMA - ' . $dr3 : ', MMA - ' . $dr3;
}

if ($_SESSION["gymnastics"] == "Y") {
    if ($_SESSION["tr_duration4"] == 1) {
        $dr4 = "1 Month";
    } else {
        if ($_SESSION["tr_duration4"] == 12) {
            $dr4 = "1 Year";
        } else {
            $dr4 = $_SESSION["tr_duration4"] . ' Months';
        }
    }
    $for .= empty($for) ? 'Gymnastics - ' . $dr4 : ', Gymnastics - ' . $dr4;
    $c4 = ', Gymnastics - ' . $dr4;
}

you should use switch case instead of many if else.

1 Comment

I had tried a similar approach except for the 'empty($for) ?' thing.. perhaps that's why my code had failed.

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.