-3

im trying made filter for multiple checkboxes values, but i have problem to get only specific data. I would like to tight results in a loop if I mark choice[] and choice2[] I tried to add an additional value for each foreach, but I got an error foreach () argument must be of type array | object, string given

DATABASE

id ... forWho runeType
1 ... tank weapon
2 ... mage weapon
3 ... archer weapon
4 ... tank armor
5 ... mage armor
6 ... archer armor

HTML

<div class="runesFilter-view">
    <div class="runesCheckBox">
        <input class="form-check-input" name="choice[]" id="check-tank" type="checkbox" value="tank" />
        <label class="form-check-label" for="check-tank">dla Tanka</label>
        <input class="form-check-input" name="choice1[]" id="" type="hidden" value="tank" />
    </div>
    <div class="runesCheckBox">
        <input class="form-check-input" name="choice[]" id="check-mage" type="checkbox" value="mage" />
        <label class="form-check-label" for="check-mage">dla Maga</label>
        <input class="form-check-input" name="choice1[]" id="" type="hidden" value="mage" />
    </div>
    <div class="runesCheckBox">
        <input class="form-check-input" name="choice[]" id="check-archer" type="checkbox" value="archer" />
        <label class="form-check-label" for="check-archer">dla Łucznika</label>
        <input class="form-check-input" name="choice1[]" id="" type="hidden" value="archer" />
    </div>
</div>
<div class="runesFilter-view">
    <div class="runesCheckBox">
        <input class="form-check-input" name="choice[]" id="check-weapon" type="checkbox" value="weapon" />
        <label class="form-check-label" for="check-weapon">na Broń</label>
        <input class="form-check-input" name="choice2[]" id="" type="hidden" value="weapon" />
    </div>
    <div class="runesCheckBox">
        <input class="form-check-input" name="choice[]" id="check-armor" type="checkbox" value="armor" />
        <label class="form-check-label" for="check-armor">na Zbroję</label>
        <input class="form-check-input" name="choice2[]" id="" type="hidden" value="armor" />
    </div>
    <div class="runesCheckBox">
        <input class="form-check-input" name="choice[]" id="check-ring" type="checkbox" value="ring" />
        <label class="form-check-label" for="check-ring">na Pierścień</label>
        <input class="form-check-input" name="choice2[]" id="" type="hidden" value="ring" />
    </div>
    <div class="runesCheckBox">
        <input class="form-check-input" name="choice[]" id="check-amulet" type="checkbox" value="amulet" />
        <label class="form-check-label" for="check-amulet">na Amulet</label>
        <input class="form-check-input" name="choice2[]" id="" type="hidden" value="amulet" />
    </div>
    <div class="runesCheckBox">
        <input class="form-check-input" name="choice[]" id="check-artefakt" type="checkbox" value="artefakt" />
        <label class="form-check-label" for="check-artefakt">na Artefakt</label>
        <input class="form-check-input" name="choice2[]" id="" type="hidden" value="artefakt" />
    </div>
</div>

and PHP

if (isset($_GET['btSubmit']))
{
  if (!isset($_GET['choice']))
  {
    echo "<p class='align-center'><span class='badge bg-danger'>Wybierz najpierw typ run których szukasz</span></p>";
  }
  else 
  {
    $checked_array = $_GET['choice'];
    foreach ($_GET['choice1'] as $key => $value)
    {
      if(in_array($_GET['choice1'][$key], $checked_array))
      {
        $forWho = $_GET['choice1'][$key];

        $wybierz_ile = "SELECT * FROM runes WHERE forWho = ('$forWho')";
        $wez_ile = mysqli_query($polaczenie, $wybierz_ile);
        $pokaz_ile = mysqli_num_rows($wez_ile);

        if(empty($pokaz_ile))
        {
          echo "<p class='align-center'><span class='badge bg-danger'>Nie ma w bazie run <strong>$forWho</strong></span></p>";
        }
        else
        {
          echo "<p class='align-center'><span class='badge bg-success'>Znaleziono $pokaz_ile pasujących run dla klasy <strong>$forWho</strong></span></p>";
          while ($row_num = mysqli_fetch_assoc($wez_ile))
          {
              $string = str_replace(' ', '-', $row_num['runeName']);
              echo "<div class='runeWrapper'><div class='runeBoxImg $row_num[forWho]'><div class='runeImg'><img src='img/runes/rune-$string.png' alt='' /></div></div>",
              "<div class='runeBox'><p>Description 1</p></div>",
              "<div class='runeBox'><p>Description 2</p></div>",
              "<div class='runeBox'><p>Description 3</p></div></div>";
          }
        }
      }
    }
  }
}

After click submit (before while) var_dump($_GET['choice1']):

array(9) {
  [0]=>
  string(4) "tank"
  [1]=>
  string(4) "mage"
  [2]=>
  string(6) "archer"
  [3]=>
  string(3) "all"
  [4]=>
  string(6) "weapon"
  [5]=>
  string(5) "armor"
  [6]=>
  string(4) "ring"
  [7]=>
  string(6) "amulet"
  [8]=>
  string(8) "artefakt"
}
Tomasz Kużaj
  • 27
  • 1
  • 8
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman May 03 '22 at 14:31
  • Please, always show us ALL the error message not a summary. It is also useful to indicatae in the code which line is causing the error (as from the Line Number) in the error message as people often dont show us all the code – RiggsFolly May 03 '22 at 14:42
  • thx, im very begginer level in programming and any warning is very helpfull for me. – Tomasz Kużaj May 03 '22 at 14:55

1 Answers1

0

First, before the loop add the following code and tell what the output was:

echo '<pre>';
var_dump($_GET['choice']);
echo '</pre>';
exit;

I don't know why your HTML is structured this way but I think you could do the following:

<div class="runesCheckBox">
        <input class="form-check-input" name="choice1[]" id="check-tank" type="checkbox" value="tank" />
        <label class="form-check-label" for="check-tank">dla Tanka</label>
    </div>
if (isset($_GET['btSubmit']))
{
  if (!isset($_GET['choice1']))
  {
    echo "<p class='align-center'><span class='badge bg-danger'>Wybierz najpierw typ run których szukasz</span></p>";
  }
  else 
  {
    foreach ($_GET['choice1'] as $forWho)
    {
        $wybierz_ile = "SELECT * FROM runes WHERE forWho = ('$forWho')";
        $wez_ile = mysqli_query($polaczenie, $wybierz_ile);
        $pokaz_ile = mysqli_num_rows($wez_ile);

        if(empty($pokaz_ile))
        {
          echo "<p class='align-center'><span class='badge bg-danger'>Nie ma w bazie run <strong>$forWho</strong></span></p>";
        }
        else
        {
          echo "<p class='align-center'><span class='badge bg-success'>Znaleziono $pokaz_ile pasujących run dla klasy <strong>$forWho</strong></span></p>";
          while ($row_num = mysqli_fetch_assoc($wez_ile))
          {
              $string = str_replace(' ', '-', $row_num['runeName']);
              echo "<div class='runeWrapper'><div class='runeBoxImg $row_num[forWho]'><div class='runeImg'><img src='img/runes/rune-$string.png' alt='' /></div></div>",
              "<div class='runeBox'><p>Description 1</p></div>",
              "<div class='runeBox'><p>Description 2</p></div>",
              "<div class='runeBox'><p>Description 3</p></div></div>";
          }
        }
      }
    }
}
Lucas
  • 394
  • 2
  • 13