php内爆函数空数组不工作

dy1byipe  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(283)

我尝试从查询中获取数组中的值,并使用内爆将数组值转换为字符串,直到它正常工作为止,
在那之后,我在选择框的另一个where条件查询中传递了内爆变量,所以现在发生的是,如果数组是空的,那么选择框正在消失,有人能帮我解决这个问题吗。。
这是我的密码

<select name="course1id" class="form-control" required> 
                            <?php
                            $r = array();
                            $query = "SELECT course1id,course2id FROM mdl_course_relation";
                            $result = $DB->get_records_sql($query);
                            foreach ($result as $res) {

                                $r[] = $res->course1id;
                                $r[] = $res->course2id;

                            }

                            $courseids = implode(",", $r);
                            //$where = '';

                            //if (sizeof($r))

                                //$where .= 'AND id NOT IN($courseids)';

                            ?> 

                            <?php
                            $sql = "select id,fullname from {course} WHERE id!=1 AND id NOT IN($courseids)";

                            $courses = $DB->get_records_sql($sql);
                            ?> 

                            <option value="">Choose Course</option> 
                            <?php
                            if (sizeof($courses)): foreach ($courses as $row):
                                    $coursename = $row->fullname;
                                    ?> 
                                    <option value="<?php echo $row->id . '|' . $row->fullname; ?>"><?php echo $row->fullname; ?></option> 
                                    <?php
                                endforeach;
                            endif;

                            ?> 
                        </select>
k4emjkb1

k4emjkb11#

您需要检查空数组
像这样更新代码

$courseids = implode(",", $r);
$where = '';

if (count($r)>0)
{
    $where = "AND id NOT IN($courseids)";
}
$sql = "select id,fullname from {course} WHERE id!=1 $where";
$courses = $DB->get_records_sql($sql);

相关问题