我有一个按课程分类的大数据库,并且我有一个网站可以在其中查找这些课程。有没有一种简单的方法可以为每个课程创建单独的页面,这是SQL结果? 大约有1000个条目。 基本上,我希望它的行为就像您单击一门课程时一样,它将带您进入域中包含该课程信息的单独页面。 每门课程分别制作1000个不同的PHP文件将花费太长时间
答案 0 :(得分:1)
您应该制作一个动态页面,该页面根据提供的数据而变化。有很多方法可以做到这一点。
我将为您提供一个示例,说明如何使用GET请求进行操作。因此,您可以通过更改url中的查询字符串来查看课程信息。
例如
www.example.com/courses/?id=123
将显示ID为123的课程
我为您提供的示例代码不是完整的代码,您将需要设置数据库连接并自行查询,但我认为这将使您对要做的事情有个清晰的认识。
<?php
// url: www.example.com/courses/?id=123
//check to see if id is passed in url
if(isset($_GET['id'])){
$course_id = $_GET['id'];
//TODO: connect to db
//TODO: query DB course table to find record with $course_id (123)
//This is an example record from the DB. you would get this by doing a query as mentioned above
$result = array(
"id"=>"123",
"name"=>"My Course",
"description"=> "This is a great course!"
);
$title = $result['name'];
$heading = $result['name']." - ".$result['name'];
$description =$result['description'];
}
// Deal with if no course is found, ie. no id or id not found
if(!isset($_GET['id']) || !$result || count($result)<1)){
$title = "Not Found";
$heading = "Not Found";
$description = "Course not found.";
}
?>
<html>
<head
<?php // Dynamic title using the data from the DB ?>
<title>Course - <?php echo $title; ?></title>
</head>
<body>
<!-- Dynamic heading using the data from the DB -->
<h1><?php echo $heading; ?></h1>
<p>
<!-- Dynamic description using the data from the DB -->
<?php echo $description; ?>
</p>
</body>
</html>
您应该仔细研究如何使用php创建动态网页,我敢肯定您会找到很多有用的资源和视频。
答案 1 :(得分:-1)
简短回答-是!
您需要研究使用PHP创建与数据库的连接,运行SQL命令以提取所需的数据,然后使用PHP在页面上操作/显示数据。
如果您对使用PHP感到满意,可以找到许多指南来帮助您解决这类问题。例如:https://www.w3schools.com/php/php_mysql_connect.asp