当前,我正在显示用户ID,并用逗号定界符在一行中显示,问题是逗号之间有多余的空格
这样显示 1、6、10
应该是这样 1,6,10
<div class="students">
<?php foreach ( $students as $student ) {
$process = '';
$result = 0;
$student = learn_press_get_user( $student->ID );
$data = $student->get_course_data($course->get_id());
$status = $student->get_course_status($course->get_id());
$result = $data->get_results();
?>
<?php if ( $status ) {
$process = ( $status == 'finished' ) ? 'finished' : 'in-progress';
} ?>
<?php echo $student->get_id(); ?>,
<?php } ?>
</div>
答案 0 :(得分:1)
<div class="students">
<?php
foreach ( $students as $student ) {
$process = '';
$result = 0;
$student = learn_press_get_user( $student->ID );
$data = $student->get_course_data($course->get_id());
$status = $student->get_course_status($course->get_id());
$result = $data->get_results();
if ( $status ) {
$process = ( $status == 'finished' ) ? 'finished' : 'in-progress';
}
echo trim($student->get_id()) . ',';
}
?>
</div>
答案 1 :(得分:1)
使用implode
<div class="students">
<?php
$student_ids = array();
foreach ( $students as $student ) {
$process = '';
$result = 0;
$student = learn_press_get_user( $student->ID );
$data = $student->get_course_data($course->get_id());
$status = $student->get_course_status($course->get_id());
$result = $data->get_results();
if ( $status ) {
$process = ( $status == 'finished' ) ? 'finished' : 'in-progress';
}
$student_ids[] = $student->get_id(); // store all student id in array
}
echo implode(',', $student_ids);
?>
</div>
答案 2 :(得分:0)
最后div结束时的php标记,在其前面有一个空格。
<?php } ?>
尝试在一个PHP代码段中编写循环。