避免检索重复项(MYSQLi / PHP)

时间:2015-03-13 21:46:28

标签: javascript php html mysql mysqli

如下图所示,其中有一个子类别,其中包含重复项目,原因如下:

它查看课程标题并查看与其关联的子类别并填充该标题。问题在于,如果您有10个课程名称相似并且具有相同子类别的课程,则该子类别将多次显示。因此,我想做的是:

有一个if语句,规定子类别名称已经填充,而不需要再次填充它。以下是如何生成子类别:

          $search_query = $_GET['user_query'];

$get_cats = "select * from courses where (course_title like '%$search_query%' )  AND course_date1 >= CURRENT_DATE() LIMIT 5";

    $run_cats = mysqli_query($con, $get_cats);

    while ($row_cats=mysqli_fetch_array($run_cats)){
        $crs_sub1 = $row_cats['course_subc1'];
                $crs_sub2 = $row_cats['course_subc2'];
                        $crs_sub3 = $row_cats['course_subc3'];

        $crs_sub4 = $row_cats['course_subc4'];

        $crs_sub5 = $row_cats['course_subc5'];




        echo "<a href='searchPage.php?user_query=$crs_sub1' id='liSpacing'><label id='labelSearch'><input type='checkbox' name='checkbox' value='value'>&nbsp;$crs_sub1</label></a> <br />";



    }

提前致谢

enter image description here

更新

原件:

 $get_cats = "select * from courses where (course_title like '%$search_query%' )  AND course_date1 >= CURRENT_DATE() LIMIT 5";

更新:

  $get_cats = "select distinct course_title from courses where (course_title like '%$search_query%' )  AND course_date1 >= CURRENT_DATE() LIMIT 5";

更新后,我收到以下错误:

Notice: Undefined index: course_subc1 in 

Notice: Undefined index: course_subc2 in 

Notice: Undefined index: course_subc3 in 

Notice: Undefined index: course_subc4 in 

Notice: Undefined index: course_subc5 in 


Notice: Undefined index: course_subc1 in

Notice: Undefined index: course_subc2 in 

Notice: Undefined index: course_subc3 in 

Notice: Undefined index: course_subc4 in 
Notice: Undefined index: course_subc5 in 


Notice: Undefined index: course_subc1 in 

Notice: Undefined index: course_subc2 in 

Notice: Undefined index: course_subc3 in 

Notice: Undefined index: course_subc4 in 

Notice: Undefined index: course_subc5 in 

Notice: Undefined index: course_subc1 in 

Notice: Undefined index: course_subc2 in 
Notice: Undefined index: course_subc3 in 
Notice: Undefined index: course_subc4 in 
Notice: Undefined index: course_subc5 in 
Notice: Undefined index: course_subc1 in 
Notice: Undefined index: course_subc2 in 
Notice: Undefined index: course_subc3 in
Notice: Undefined index: course_subc4 in 
Notice: Undefined index: course_subc5 in 

以下是错误行:

 $run_cats = mysqli_query($con, $get_cats);

            while ($row_cats=mysqli_fetch_array($run_cats)){
                $crs_sub1 = $row_cats['course_subc1'];
                        $crs_sub2 = $row_cats['course_subc2'];
                                $crs_sub3 = $row_cats['course_subc3'];

                $crs_sub4 = $row_cats['course_subc4'];

                $crs_sub5 = $row_cats['course_subc5'];

3 个答案:

答案 0 :(得分:1)

我不确切知道您的桌面结构是什么,但我们会说该字段的名称是&#39; course_title&#39;

$sql = "select distinct course_title from courses where (course_title like '%$search_query%' )  AND course_date1 >= CURRENT_DATE() LIMIT 5";

答案 1 :(得分:1)

首先要做的事情。

要修复查询(并防止未定义的索引错误),您需要执行以下操作:

$get_cats = "select distinct course_title, course_subc1, course_subc2,
course_subc3, course_subc4, course_subc5 from courses where 
(course_title like '%$search_query%' )  AND course_date1 >= CURRENT_DATE()
LIMIT 5";

大但

您正在请求4个不使用的字段:course_subc2,course_subc3,course_subc4和course_subc5。此外,如果这些是类别(他们走路和说话类别)你有一个很大的规范化问题。你应该有一个类别表,并在课程和类别之间建立适当的关系。

答案 2 :(得分:0)

您需要SELECT DISTINCT来停止复制。无法编写修订后的查询,因为您没有提供有关表结构的任何信息。学习提供问题所需的数据,您将获得更好的答案。