简体   繁体   English

除了最后一个之外,用逗号显示天数

[英]Have days display with commas except for the last one

I have the following if statements that gather up all the days that were selected before it becomes displayed. 我有以下if语句,它们收集在显示之前选择的所有日期。

$daysUsed = "";

if($this->dayweeksunsession==1){
    $daysUsed .= "Su ";
}

if($this->dayweekmonsession==1){
    $daysUsed .=  "M ";
}

if($this->dayweektuessession==1){
    $daysUsed .=  "T ";
}

if($this->dayweekwedsession==1){
    $daysUsed .=  "W ";
}

if($this->dayweekthurssession==1){
    $daysUsed .=  "Th ";
}

if($this->dayweekfrisession==1){
    $daysUsed .=  "F ";
}

if($this->dayweeksatsession==1){
    $daysUsed .=  "Sa ";
}

if($daysUsed !=="") {
    echo "</span><br/>Days of the Week: <span class='BoxReviewOutput'>";
    echo $daysUsed;
}

My question here is how can I make this so that commas will be displayed for each day of the week that was chosen in the session except for the last one. 我的问题是如何才能这样做,以便在会话中选择的每周的每一天显示逗号,除了最后一个。

For example: Sunday and Tuesday were chosen. 例如:周日和周二被选中。 So it would be displayed "Su, T" 所以会显示“Su,T”

Thanks in advance! 提前致谢!

Use implode() 使用implode()

$days = array("Su", "M", ....., "Sa");
echo implode(",", $days);

In your ifs add a commma: 在你的ifs中添加一个commma:

$daysUsed = "Whatever, ";

Then before you output the final string: 然后在输出最终字符串之前:

$daysUsed = substr($daysUsed, 0, -2);

EDIT: -1 needs to be -2, to account for the spacing between days. 编辑: -1需要为-2,以计算天数之间的间距。

i'd use an array and implode it but in your case i'd need to use this 我会使用一个阵列并将其内爆,但在你的情况下,我需要使用它

echo substr($daysUsed,0,-1)

in this case I remove the last character from the string 在这种情况下,我从字符串中删除最后一个字符

$daysUsed = trim( $daysUsed, "," ); $ daysUsed = trim($ daysUsed,“,”);

trim() is by far the better option - minimal function, does what you want for your intended purpose. trim()是迄今为止更好的选择 - 最小功能,为您的预期目的做你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM