0

我正在尝试创建一个页面,其中显示课程标题的项目符号列表并链接到用户是 Moodle 2.4 教师的任何课程。

我在这里找到了这段代码,它适用于显示 Moodle 课程类别,但不适用于用户的课程。我不是程序员,所以非常感谢任何帮助。

<?php
$con = mysql_connect("localhost","moodle","moodle");
if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("moodle", $con);
$result = mysql_query("SELECT name AS COURSE_NAME,parent FROM mdl_course_categories");

if (isset($result)!=1) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
}
echo "<p> The courses taught are: </p>";

///Display Course Categories
$query_catetories = mysql_query('SELECT cc.id, cc.parent, cc.name FROM   mdl_course_categories cc ');
$categories = mysql_fetch_all($query_catetories);

$tmp_categories = array();

foreach ($categories AS $row) {

$row['id'] = (int) $row['id'];
$row['parent'] = (int) $row['parent'];
if (!$tmp_categories[$row['parent']])
    $tmp_categories[$row['parent']] = array();
$tmp_categories[$row['parent']][] = $row;
}

$course_catetories = buildNode($tmp_categories);

echo '<ul>';
foreach ($course_catetories as $course_catetory) {
print_category_child($course_catetory);
}
echo '</ul>';

function print_category_child($category) {
echo '<li>' . $category['name'];
if (array_key_exists('children', $category)) {
    echo '<ul>';
    foreach ($category['children'] as $child) {
        print_category_child($child);
    }
    echo '</ul>';
}
echo '</li>';
}

function buildNode($inputArray, $parent = 0) {
$return = array();
foreach ($inputArray[$parent] AS $key => $row) {
    if (@$inputArray[$row['id']]) {
        $row['children'] = buildNode($inputArray, $row['id']);
    }
    $return[] = $row;
}
return $return;
}

function mysql_fetch_all($result) {
$all = array();
while ($all[] = mysql_fetch_assoc($result)) {

}
return array_filter($all);
}
///END Course Display 


?>'`
4

1 回答 1

0

看看你的 Mysql 查询:

$result = mysql_query("SELECT name AS COURSE_NAME,parent FROM mdl_course_categories");

您的结果是“mdl_course_categories”表中的类别名称列表。

如果要显示课程名称列表,请使用正确的表“mdl_course”。它可能是这样的:

mysql_select_db("moodle", $con);
$result = mysql_query("SELECT id, fullname FROM mdl_course WHERE category = "your_course_category_here");

while($row = mysql_fetch_array($result)){
    echo $row[0];
    echo "<br>";
    echo $row[1];
}
于 2013-03-06T10:08:47.880 回答