开发者

how to use mysql implode while loop

$mysqli->query("SELECT b.t_follow_up, b.t_consultation, b.t_acid_peel, b.t_wrinkle_name, b.t_dermal_name, b.status, b.date_book, b.min_spent, b.b_ref, m.f_name, m.l_name, d.doctor_name, MIN(t.times)
FROM bookslot b
LEFT JOIN timeslot t ON b.id_timeslot = t.id_timeslot
LEFT JOIN member m ON b.id_member = m.id_member
LEFT JOIN doctor d ON b.id_doctor = d.id_doctor

while($r = $q->fetch_array(MYSQLI_ASSOC)) :
开发者_Go百科    echo '. $r['t_consultation'] . ' ' . $r['t_follow_up'] . ' ' . $r['t_acid_peel'] . ' ' . $r['t_dermal_name'] . ' '  . $r['t_wrinkle_name'] ';
endwhile;

WHILE LOOP RESULT:

i can put comma in while loop manually!  the problem when there is no table treatment is empty, comma will still there
consultation follow_up acid peel dermal

i heard i can use implode. how do i implement it? EXPECTED RESULT:

if echoing the treatments:
consultation, acid peel, wrinkle name, dermal name


You can put all values in an array and remove empty elements:

function isNotEmpty($element) {
    return strlen($element) > 0;
}

while($r = $q->fetch_array(MYSQLI_ASSOC)) :
    $arr = array($r['t_consultation'], $r['t_follow_up'], $r['t_acid_peel'], $r['t_dermal_name'], $r['t_wrinkle_name']);
    echo implode(', ', array_filter($arr, 'isNotEmpty'));
endwhile;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜