Php MySQL查询搜索(高级)
我有一个课程页面,该页面设置为显示详细信息(简单),然后是谁在每个学期的第一、第二、第三和第四期教授。问题是,我的教师数据是在一个巨大的链中处理的 鲍比:1-1-1-1-1-1-1-1-1-1-1-1 蒂娜:20-20-20-10-1-1-1-1 乔伊:20-1-1-1-49-432-10-19 我想做的是找到一门课程:20,当所有的信息都显示出来时,会通过教师进行搜索,以找出谁在教授课程20,以及什么时期 前 我想让每一位教授这门课程的老师在一次搜索中找到答案,但考虑到capact数据的障碍,这可能是一个问题 教师表: id/姓名/链接/课程(1-1-1-1-1-1-1-1) 课程表: id/姓名/代码/等级/级别Php MySQL查询搜索(高级),php,mysql,Php,Mysql,我有一个课程页面,该页面设置为显示详细信息(简单),然后是谁在每个学期的第一、第二、第三和第四期教授。问题是,我的教师数据是在一个巨大的链中处理的 鲍比:1-1-1-1-1-1-1-1-1-1-1-1 蒂娜:20-20-20-10-1-1-1-1 乔伊:20-1-1-1-49-432-10-19 我想做的是找到一门课程:20,当所有的信息都显示出来时,会通过教师进行搜索,以找出谁在教授课程20,以及什么时期 前 我想让每一位教授这门课程的老师在一次搜索中找到答案,但考虑到capact数据的障碍,
教师课程包含课程表中的id您应该更改数据库的结构,而不是存储所有句点的字符串,您应该有一个包含三列的附加表:教师、课程、句点,并在该表中为教师正在讲授的每门课程设置一行。然后,确定谁在教授什么课程只需按课程id查询该表,然后按时段排序即可。e、 g:
SELECT teacher_id, course_id, period FROM course_info WHERE course_id = 20
ORDER BY period;
创建新表 大概是这样的:
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;
虽然重新构造数据库可能是最好的答案,但我想我应该发布一个直接的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>';
}
//我设置了变量来包含您期望的搜索结果,以便测试
$search_course='20';
$search\u results=数组(
“博比”=>“1-1-1-1-1-1-1-1”,
“蒂娜”=>“20-20-20-10-1-1-1-1”,
“乔伊”=>“20-1-1-1-49-432-10-19”
);
//将课程字符串分解为数组,并使用教师名称存储,以便
//您可以稍后循环查看它们
foreach($teacher=>$string\u课程搜索结果){
$array_courses=分解('-',$string_courses);
$search\u results[$teacher]=$array\u课程;
}
//将要搜索的路线与数组中的元素匹配
//创建带有句点和匹配教师的结果数组
foreach($teacher=>$courses作为搜索结果){
foreach($period=>$course的课程){
如果($course==$search\u course){
$results[$period][]=$teacher;
}
}
}
//在结果数组中循环并显示结果
//我想你会有不同的html
foreach($period=>$teachers的结果){
回声“周期:”;
回声$period+1;
回声内爆(“,”,$teachers);
回声“
”;
}
打印的结果与OP中所需的列表匹配您的数据库是什么样子的?(dbms、结构、版本)到底是谁这样存储的?每个单元格中存储的数据项不得超过一个。如果你这样做了,结果是这样的。我这样做了,这是一个好主意,只是像这样反转并不多,但我太远了,无法回头。无法搜索示例x-x-20-x-x-x-x-x-x,其中x被所有表接受?我最喜欢这种方法,基准测试非常棒,因为我必须检索所有字段,然后进行检查。不过这没关系,因为永远不会有超过100个字段。谢谢!:)
//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>';
}