I have a table I created, how do I make a form for a user to filter the data?

I have created a table, I now need to filter the data from that table when someone selects options from a form.

Whats a good way to do that?

Related posts

Leave a Reply

2 comments

  1. Here is the html:

    <form name="filter" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post">
        <input type="hidden" name="filter_submit" value="Y">
            <select name="your_filter" class="input">
                <option value="none">nothing selected</option>
                <option value="b">b</option>
                <option value="c">c</option>                                                    
            </select>
        <input type="submit" name="submit" class="input" value="Filter" />
        </form>
    

    Here is the php to filter data:

    function filter() {
    global $wpdb;
    $table_name = $wpdb->prefix . "your_table_listings";
    $sql = $wpdb->get_results("SELECT * FROM $table_name");
    
    if ($_POST['filter_submit'] == 'Y'){
            $your_filter = $_POST['your_filter'];
    
            if ($your_filter !== 'none'){
                        $add_sql = ' yourcolumn = '' . $your_filter . ''';  
                        $sql = $wpdb->get_results("SELECT * FROM $table_name WHERE $add_sql");
            }
    }
    }
    

    Here are some links that might help:

    wpdb