Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
    
eek, whatever you do, don't put POST data directly in a query like that! use prepared statements. –  Milo Jun 9 '12 at 14:27

If you use $_POST and know the options you should check to make sure no additional data is coming in. Only the options.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.