doushao5047 2019-03-03 00:02
浏览 23
已采纳

根据所选复选框进行过滤

I have some checkboxes for each movie genre. Each checkbox (movie genre) comes from the db, so there is one checkbox for each movie genre.

 <input type='checkbox' name='movieGenre[]' value='Comedy'>Comedy/>
 <input type='checkbox' name='movieGenre[]' value='Action'>Action/>
 ...

The user can select 1 or more checkboxes and then click in a button "Show" and it should be displayed a table with movies associated with the movie genres checked.

However, to achieve this is necessary to verify which checkboxes are checked and do a query to show only the movies that are associated with the selected checkboxes (movie genres). Do you know what is necessary for this?

  • 写回答

1条回答 默认 最新

  • dsl36367 2019-03-03 00:47
    关注

    To get you started, here is a quick example

    //...
    
    if ( ! empty ( $_POST['movieGenre'] ) && is_array ( $_POST['movieGenre'] ) )
    {
        // bind param container
    
        $query = [];
    
        // bind param types, (s)
    
        $binds = '';
    
        // trim and remove possible empty values
    
        $selected = array_filter ( array_map ( 'trim', $_POST['movieGenre'] ) );
    
        // go through the $_POST data
    
        foreach ( $selected AS $genre )
        {
            // valid data should only contain [a-zA-Z]
    
            if ( ctype_alpha ( $genre ) )
            {
                // valid data, build up the (OR) clause
    
                $query[] = $genre;
    
                $binds .= 's';
            }
        }
    
        if ( ! empty ( $query ) )
        {
            // db connect
    
            // db statement
    
            // db prepare
    
            // db bind
    
            // db execute
    
            // db result
    
            // while ( db fetch )
    
            // db free result
    
            // db close
        }
        else
        {
            // error, all $_POST data was invalid
        }
    }
    else
    {
        // error, nothing selected
    }
    
    //...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计