Home > Net >  How to combine multiple php filters?
How to combine multiple php filters?

Time:12-02

I made a filter from one category with the names of the music genres. When clicked it shows the music sheets from that genre. But now I want to add the instruments. The problem is, I don't know how to combine those 2 filters like if for example I want to see the sheets from Christmas songs for Piano.

  <form method="post" id="filter">
                <input type="submit" name="button1" value="Alle"  id="click"/>
                <input type="submit" name="button2" value="Movies" />
                <input type="submit" name="button3" value="Christmas" />
                <input type="submit" name="button4" value="Folk" />
                <input type="submit" name="button5" value="Classic" />
                <input type="submit" name="button6" value="Funk" />
                <input type="submit" name="button7" value="Pop" />
                <input type="submit" name="button8" value="Romantic" />
                <input type="submit" name="button9" value="Baroque" />
            </form>
            <?php
            $query =
                'SELECT `sheets_title`, `sheets_composer`, `sheets_genre`, `sheets_instrument1`, `sheets_arrangement` FROM `imslp_sheets`,`imslp_genre` WHERE `sheets_genre_ID`=`genre_ID`';
            $result = $conn->query($query);

            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    $thisTitle = $row['sheets_title'];
                    $thisComposer = $row['sheets_composer'];
                    $thisGenre = $row['sheets_genre'];
                    $thisInstrument = $row['sheets_instrument1'];
                    $thisArrangement = $row['sheets_arrangement'];

                    if (strlen($thisComposer) > 23) {
                        $thisComposer = substr($thisComposer, 0, 23) . '...';
                    }

                    if (isset($_POST['button1'])) {
                        echo "
                                        <h2>$thisComposer</h2>
                                        <p>$thisTitle - $thisGenre - $thisInstrument</p>
                                    ";
                    }

                    if (isset($_POST['button2'])) {
                        if ($thisGenre === 'Movies') {
                            echo "
                                        <h2>$thisComposer</h2>
                                        <p>$thisTitle - $thisGenre - $thisInstrument</p>
                                    ";
                        }
                    }
                    if (isset($_POST['button3'])) {
                        if ($thisGenre === 'Christmas') {
                            echo "
                                        <h2>$thisComposer</h2>
                                        <p>$thisTitle - $thisGenre - $thisInstrument</p>
                                    ";
                        }
                    }
                    if (isset($_POST['button4'])) {
                        if ($thisGenre === 'Folk') {
                            echo "
                                        <h2>$thisComposer</h2>
                                        <p>$thisTitle - $thisGenre - $thisInstrument</p>
                                    ";
                        }
                    }
                    if (isset($_POST['button5'])) {
                        if ($thisGenre === 'Classic') {
                            echo "
                                        <h2>$thisComposer</h2>
                                        <p>$thisTitle - $thisGenre - $thisInstrument</p>
                                    ";
                        }
                    }
                    if (isset($_POST['button6'])) {
                        if ($thisGenre === 'Funk') {
                            echo "
                                        <h2>$thisComposer</h2>
                                        <p>$thisTitle - $thisGenre - $thisInstrument</p>
                                    ";
                        }
                    }
                    if (isset($_POST['button7'])) {
                        if ($thisGenre === 'Pop') {
                            echo "
                                        <h2>$thisComposer</h2>
                                        <p>$thisTitle - $thisGenre - $thisInstrument</p>
                                    ";
                        }
                    }
                    if (isset($_POST['button8'])) {
                        if ($thisGenre === 'Romantic') {
                            echo "
                                        <h2>$thisComposer</h2>
                                        <p>$thisTitle - $thisGenre - $thisInstrument</p>
                                    ";
                        }
                    }
                    if (isset($_POST['button9'])) {
                        if ($thisGenre === 'Baroque') {
                            echo "
                                        <h2>$thisComposer</h2>
                                        <p>$thisTitle - $thisGenre - $thisInstrument</p>
                                    ";
                        }
                    }
                }
            }
            ?>

CodePudding user response:

There are some ways to improve your code. If you wanna filter by genre, there's a better method to make the form (and it will let you add more filters too):

HTML

You don't need to create a button per element. Just use a list or radiobuttons to set one field with multiple values. The example includes both fields (genre and instrument).

If you are not very familiar with how to make forms, I recommend that you take a look at this: https://www.w3schools.com/html/html_forms.asp

<form method="post">
    <select name="genre">
        <option value="">-- All --</option>
        <option value="Alle">Alle</option>
        <option value="Movies">Movies</option>
        <option value="Christmas">Christmas</option>
        <option value="Folk">Folk</option>
        <option value="Classic">Classic</option>
        <option value="Funk">Funk</option>
        <option value="Pop">Pop</option>
        <option value="Romantic">Romantic</option>
        <option value="Baroque">Baroque</option>
    </select>
    <select name="instruments">
        <option value="">-- All --</option>
        <option value="Guitar">Guitar</option>
        <option value="Piano">Piano</option>
        <option value="Drums">Drums</option>
    </select>
    <button type="submit">Filter</button>
</form>

PHP SQL

Now you don't need a field per genre or instrument, you have to get the post value using filter_input(), which is safer than just getting $_POST['genre'].

Check if they're empty and, if not, add an AND clause in your SQL query. This will filter the results straight from the database (the best way), so you don't have to use if clauses to display the item or not, simplifying the way you display the content.

<?php

$query      = "SELECT `sheets_title`, `sheets_composer`, `sheets_genre`, `sheets_instrument1`, `sheets_arrangement` FROM `imslp_sheets`,`imslp_genre` WHERE `sheets_genre_ID`=`genre_ID`";
$genre      = filter_input(INPUT_POST, 'genre', FILTER_SANITIZE_SPECIAL_CHARS); // Get the genre POST value...
$instrument = filter_input(INPUT_POST, 'instrument', FILTER_SANITIZE_SPECIAL_CHARS); // Get the instrument POST value...

if ( ! empty($genre)) { // If $genry is not empty, add to the query...
    $query .= " AND `sheets_genre` = '$genre'";
}

if ( ! empty($instrument)) { // If $instrument is not empty, add to the query...
    $query .= " AND `sheets_instrument1` = '$instrument'";
}

$result = $conn->query($query);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $thisTitle       = $row['sheets_title'];
        $thisComposer    = $row['sheets_composer'];
        $thisGenre       = $row['sheets_genre'];
        $thisInstrument  = $row['sheets_instrument1'];
        $thisArrangement = $row['sheets_arrangement'];

        if (strlen($thisComposer) > 23) {
            $thisComposer = substr($thisComposer, 0, 23) . "...";
        }
        
        echo "<h2>$thisComposer</h2>";
        echo "<p>$thisTitle - $thisGenre - $thisInstrument</p>";
    }
}
  • Related