-1

It all works perfectly fine, the problem is that i'm currently at 6000 code lines, is there a more efficient way for replacing all of those switch? Here's my form with all the possibilities:

<form action="" method="POST">

                <select name="ordine">
                    <option value="0" selected hidden>ordina per...</option>
                    <option value="1">A-Z</option>
                    <option value="2">Z-A</option>
                </select>
                <select name="epoca">
                    <option value="0" selected hidden>Secoli di scrittura</option>
                    <option value="1">1300</option>
                    <option value="2">1800</option>
                    <option value="3"></option>
                    <option value="4"></option>
                    <option value="5"></option>
                    <option value="6"></option>
                    <option value="7"></option>
                    <option value="8"></option>
                </select>
                <select name="movimento">
                    <option value="0" selected hidden>Movimento letterario</option>
                    <option value="1">Dolce stil novo</option>
                    <option value="2">VEDI FOTO DI WIKIPEDIA</option>
                    <option value="3"></option>
                    <option value="4"></option>
                    <option value="5"></option>
                    <option value="6"></option>
                    <option value="7"></option>
                    <option value="8"></option>
                </select>
                <label class="checkbox-inline">
                    <input type='hidden' value='0' name='letteratura_ita'>
                    <input type="checkbox" name="letteratura_ita" value="1">Letteratura italiana
                </label>
                <label class="checkbox-inline">
                    <input type='hidden' value='0' name='letteratura_estero'>
                    <input type="checkbox" name="letteratura_estero" value="1">Letteratura straniera
                </label>

                <button type="submit" name='ricerca'>Cerca</button>
            </form>

Sorry for the words in italian... well, since I've got all of those possibilities, I started doing a switch(true) for it. Here's the sample code of the first one:

case ($opzione1 == 1 && $opzione2 == 0 && $opzione3 == 0 && $opzione4 == 0 && $opzione5 == 0):
                        $comando = "select * from autori order by cognome";
                        $risultato = mysqli_query($conn, $comando);
                        while ($autore = mysqli_fetch_assoc($risultato)) {
                            echo "<table border='1' class='autori'>\n";
                            $idautore = $autore["idautore"];
                            echo '<td><img src="data:image/jpeg;base64,' . base64_encode($autore['img']) . '" width="200" height="200"/> </td>';
                            echo "<tr>\n";
                            echo "<td> {$autore['nome']} {$autore['cognome']} </td>";
                            echo "</tr>\n";
                            echo "<tr>\n";
                            echo "<td> {$autore['descrizione_breve']} </td>";
                            echo "</tr>\n";
                            echo "</table>\n";
                        }
                        mysqli_close($conn);
                        break;
Giujitsu
  • 3
  • 2
  • 2
    Working solutions that need refactoring are a better fit for the [Code Review](https://codereview.stackexchange.com) Stack Exchange site. – El_Vanja May 22 '21 at 21:32
  • If you have `switch(true)` in your code, then you are using an "antipattern". There is no functional benefit in using it and it usually signals that it is time to refactor your code. It is typically a good idea to separate "processing code" from "displaying code". Also, you don't need to `fetch()` https://stackoverflow.com/a/66775416/2943403 – mickmackusa May 22 '21 at 21:40
  • We don't know where these `$opzione` variables are coming from. We don't know your exact desired result. Your question is **Unclear** by Stack Overflow standards. – mickmackusa May 22 '21 at 21:46

1 Answers1

0

You can get the post value for each select and then create a single mysqli query with all the option for searching in one string. To avoid SQL injection risk i suggest to use array with the values to use in mysqli query and use post value only like index for the array. Example:

$epoca = array("%","1300","1800",...);
$movimento = array("%","Dolce stil novo","VEDI FOTO DI WIKIPEDIA",...;
$order = array("ASC","ASC","DESC");
...
$epoca_index = $_POST['epoca'];
$movimento_index = $_POST['movimento'];
$ordine_index = $_POST['ordine'];
...

$comando = "select * from autori WHERE epoca LIKE '$epoca[$epoca_index]' && movimento LIKE '$movimento[$movimento_index]' ... order by cognome '$ordine' ";
Stefino76
  • 369
  • 4
  • 10