0

我目前在 MySQL 数据库中有 5 个表。其中一些共享外键并且相互依赖。我根据他们的专业显示课程。每节课都在或fall期间教授。在我的数据库中,我有一个名为 term 的表,其中包含、和字段。特别是该字段是具有三个值的 a ,,。这表示,或。当我显示查询而不是显示查询时,或者我可以显示查询等 ?额外:如何在每个循环的末尾添加空间,使数据看起来不聚集?springall_yearidyearsemestersemestertinyint012fallspringall_year012fallspring

钥匙

0 Fall
1 Spring
2 All-year

PHP

<?


try {

    $pdo = new PDO ("mysql:host=$hostname;dbname=$dbname","$username","$pw");
    } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }
      $query = $pdo->prepare("SELECT course.name, course.code, course.description, course.hours, semester.semester, semester.year
                            FROM course
                            LEFT JOIN major_course_xref ON course.id = major_course_xref.course_id
                            LEFT JOIN major ON major.id = major_course_xref.major_id
                            LEFT JOIN course_semester_xref ON course.id = course_semester_xref.course_id
                            LEFT JOIN semester ON course_semester_xref.semester_id = semester.id");
      $query->execute();

     if ($query->execute()){

      while ($row = $query->fetch(PDO::FETCH_ASSOC)){       
        print $row['name'] . "<br>";
        print $row['code'] . "<br>";
        print $row['description'] . "<br>";
        print $row['hours'] . " hrs.<br>";
        print $row['semester'] . "<br>";
        print $row['year'] . "<br>";
    }
    }
else
    echo 'Could not fetch results.';

      unset($pdo); 
      unset($query);

?>

当前显示

Computer Programming I
CPSC1400
Introduction to disciplined, object-oriented program development.
4 hrs.
0
2013

欲望展示

Computer Programming I
CPSC1400
Introduction to disciplined, object-oriented program development.
4 hrs.
Fall
2013
4

3 回答 3

1

semester.semester在查询中替换为:

CASE semester.semester
  WHEN 0 THEN 'Spring'
  WHEN 1 THEN 'Fall'
  WHEN 2 THEN 'All-year'
END semester
于 2012-11-02T04:53:56.673 回答
1

再做一张桌子。说学期时间。

|  id  |  semester_time  |
|  0   |  Fall           |
|  1   |  Spring         |
|  2   |  All_year       |

然后只需通过semester_time 进行连接和检索。

基于上表的查询的可能模型:

SELECT course.name, course.code, course.description, course.hours, semester.semester, semester.year, semester_time 
                            FROM course
                            LEFT JOIN major_course_xref ON course.id = major_course_xref.course_id
                            LEFT JOIN major ON major.id = major_course_xref.major_id
                            LEFT JOIN course_semester_xref ON course.id = course_semester_xref.course_id
                            LEFT JOIN semester ON course_semester_xref.semester_id = semester.id
                            LEFT JOIN semester_time ON semester.semester = semester_time.id

在每个循环之后添加空间


 while ($row = $query->fetch(PDO::FETCH_ASSOC)){       
        print $row['name'] . "<br>";
        print $row['code'] . "<br>";
        print $row['description'] . "<br>";
        print $row['hours'] . " hrs.<br>";
        print $row['semester'] . "<br>";
        print $row['year'] . "<br>";
        echo "<div class='add-space'></div>";
    }

在CSS中

.add-space {
margin-bottom: 15px; 
}

只需更改值以满足您的需求。

于 2012-11-02T04:59:21.607 回答
0
while ($row = $query->fetch(PDO::FETCH_ASSOC)){       
        print $row['name'] . "<br>";
        print $row['code'] . "<br>";
        print $row['description'] . "<br>";
        print $row['hours'] . " hrs.<br>";
        print ($row['semester'] == 0) ? "FALL":( ($row['semester'] == 1) ? "Spring" : ( ($row['semester'] == 2) ? "All-year":"")) . "<br>";
        print $row['year'] . "<br>";
        print "<br>------------------------------<br />";
    }
于 2012-11-02T05:04:01.237 回答