mardi 3 mai 2022

PHP - SQL query foreach choice checkbox

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['choice']):

array(2) {
  [0]=>
  string(4) "tank"
  [1]=>
  string(6) "weapon"
}



Aucun commentaire:

Enregistrer un commentaire