我有一个课程页面,此页面设置为显示详细信息(简单),然后是每个学期的第一,第二,第三和第四期教学。问题是,我的教师数据是在一个巨大的链条中处理的。
鲍比:1-1-1-1-1-1-1-1
蒂娜:20-20-20-10-1-1-1-1
Joey:20-1-1-1-49-432-10-19
我想要做的是找到一门课程:20,当显示所有信息时,将通过教师进行搜索,以找出谁在教授课程20以及什么时期?
实施例
Course id: 20
Period 1: Joey, Tina
Period 2: Tina
Period 3: Tina
我想让每位老师在一次搜索中教授该课程,但考虑到可能存在问题的capact数据的障碍。
老师表: id / name / link / course(1-1-1-1-1-1-1-1)
课程表: id / name / code / grade / level
教师课程包含课程表
中的ID答案 0 :(得分:1)
创建新表格
这样的事情:
CREATE TABLE `TeacherToPeriod` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`TeacherID` INT NOT NULL ,
`CourseID` INT NOT NULL ,
`Period` INT NOT NULL
) ENGINE = MYISAM ;
插入数据
这是一些PHP代码:
foreach($lines as $line){
$line_data = split($line, ': ');
$teacher = $line_data[0];
// SELECT your $teacher_id from the database
$courses = split($line_data[0], '-');
$i = 0;
foreach($courses as $course_id){
$i++;
$sql = "INSERT INTO `TeacherToPeriod` (`TeacherID` ,`CourseID` ,`Period`) ";
$sql.= "VALUES ($teacher_id, $course_id, $i);"
mysql_query($sql);
}
}
选择您想要的数据
SELECT * FROM `TeacherToPeriod` WHERE `CourseID` = 20 ORDER BY `Period` ASC;
答案 1 :(得分:1)
虽然重新构建你的数据库可能是最好的答案,但我想我会发布一个直接的php解决方案,它适用于你当前的结构,并推测你的其余代码。
//I set up variables to contain your expected search results so I could test
$search_course = '20';
$search_results = array(
'Bobby' => '1-1-1-1-1-1-1-1',
'Tina' => '20-20-20-10-1-1-1-1',
'Joey' => '20-1-1-1-49-432-10-19'
);
//explode the course strings into arrays and store with teacher names so
//you can loop through them later
foreach($search_results as $teacher=>$string_courses){
$array_courses = explode('-',$string_courses);
$search_results[$teacher] = $array_courses;
}
//Match course you are searching for to the elements in your array
//Create a result array with period and matching teachers
foreach($search_results as $teacher=>$courses){
foreach($courses as $period => $course){
if($course == $search_course){
$results[$period][] = $teacher;
}
}
}
//Loop through your result array and show the results
//I expect you'll have different html for this
foreach($results as $period => $teachers){
echo 'Period: ';
echo $period+1;
echo implode(',',$teachers);
echo '<br>';
}
打印结果与您在OP中所需的列表相匹配
答案 2 :(得分:0)
您应该更改数据库的结构,而不是存储所有句点的字符串,您应该有一个包含三列的附加表:teacher,course,period,并且在此表中为每个课程分别设置一个行老师在教学。然后确定谁在教什么课程只是通过课程id查询该表然后按期间排序的问题。 e.g:
SELECT teacher_id, course_id, period FROM course_info WHERE course_id = 20
ORDER BY period;