這裏是我的代碼,PHP - 字符串連接 - 通過循環
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 . '.';
至少節目之一將是「Y」或全部。
我想要的是$ c1 $ c2 $ c3 $ c4出來用','分隔並以'。'結尾。所以它變得可讀。
我的問題是,如果我把
$for = $c1 . $c2 . $c3 . $c4 . '.';
和 '' 以$ c1和選擇方案2和3
我得到
$for = , Muaythai - 3 Months, MMA - 12 Months.
當然我想的
,
$for = Muaythai - 3 Months, MMA - 12 Months.
如果我把','直接放在$中,如果第二和第三個程序從$ c1中刪除MS選擇
$for = $c1 . ',' . $c2 . ',' . $c3 . ',' . $c4 . '.'
我得到
$for = ,Muaythai - 3 Months, MMA - 12 Months,.
我想
$for = Muaythai - 3 Months, MMA - 12 Months.
我也試圖把築巢這麼多isset的,但都沒有成功。 我也嘗試了一個'for'循環,但最終出現了比輸出錯誤更多的語法錯誤
只有當我只選擇第一個程序或所有4個程序時,它才能正常工作。
使用單個字符串並將所有值鏈接到它。 –