Question

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.

Était-ce utile?

La solution

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!

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top