如何使用 mysql implode while 循环

发布于 2024-10-19 00:04:39 字数 937 浏览 1 评论 0原文

$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)) :
    echo '. $r['t_consultation'] . ' ' . $r['t_follow_up'] . ' ' . $r['t_acid_peel'] . ' ' . $r['t_dermal_name'] . ' '  . $r['t_wrinkle_name'] ';
endwhile;

WHILE 循环结果:

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

我听说我可以使用内爆。我该如何实施?预期结果:

if echoing the treatments:
consultation, acid peel, wrinkle name, dermal name
$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)) :
    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2024-10-26 00:04:39

您可以将所有值放入数组中并删除空元素:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文