lundi 11 février 2019

how to display multiple rows from sql database into a checkbox based off same foreign key

I have a questionnaire that has a list of check-boxes for ages that save into database into each differnt rows but same foreign id (see image below the select statement as well as the results i am getting). Now I wanna take those 4 results and put them back into a checkbox list where the checkboxes if there is a value is checked, the way I got it to work in the actual questionnaire (if say the validation failed was through the querystring so the code looked like code slot 1 below)

but now I am using a CMS so it is taking the results straight from the database so im not so sure how to get the 4 values into an array i can use to check in_array for the code slot shown below.

enter image description here

PHP Code from the questionaire.

<?php
  if (isset($_GET['kage']) && $_GET['kage'] !== null) {
  $agegroups = explode("_", $_GET['kage']);

  $checked = in_array('kids', $agegroups) ? 'checked="checked"' : '';
     echo '<label for="kids" id="kid"><input type="checkbox" name="age[]" class="ages" value="kids" id="kids" ' . $checked . '>Ages 0 - 10</label>';
  $checked = in_array('teens', $agegroups) ? 'checked="checked"' : '';
     echo '<label for="teens" id="teen"><input type="checkbox" name="age[]" class="ages" id="teens" value="teens" ' . $checked . '> Ages 11-19 </label>';
  $checked = in_array('twenties', $agegroups) ? 'checked="checked"' : '';
     echo '<label for="twentys" id="twenty"><input type="checkbox" name="age[]" class="ages" id="twentys" value="twenties" ' . $checked . '> Ages 20-29</label>';
  $checked = in_array('thirties', $agegroups) ? 'checked="checked"' : '';
     echo '<label for="thirtys" id="thirty"><input type="checkbox" name="age[]" class="ages" id="thirtys" value="thirties" ' . $checked . '> Ages 30-39 </label>';
  $checked = in_array('forties', $agegroups) ? 'checked="checked"' : '';
     echo '<label for="fourtys" id="fourty"><input type="checkbox" name="age[]" class="ages" id="fourtys" value="forties" ' . $checked . '> Ages 40-49</label>';
  $checked = in_array('fifties', $agegroups) ? 'checked="checked"' : '';
     echo '<label for="fiftys" id="fifty"><input type="checkbox" name="age[]" class="ages" id="fiftys" value="fifties" ' . $checked . '> Ages 50-59 </label>';
  $checked = in_array('sixtyup', $agegroups) ? 'checked="checked"' : '';
     echo '<label for="sixtyplus" id="sixty"><input type="checkbox" name="age[]" class="ages" id="sixtyplus" value="sixtyup" ' . $checked . '> Ages 60+</label>';
  echo '<br>';
} else {
 echo '<label for="kids" id="kid"><input type="checkbox" name="age[]" class="ages" id="kids" value="kids">Ages 0 - 10</label>  
       <label for="teens" id="teen"><input type="checkbox" name="age[]" class="ages" id="teens" value="teens"> Ages 11-19 </label>
       <label for="twentys" id="twenty"><input type="checkbox" name="age[]" class="ages" id="twentys" value="twenties"> Ages 20-29</label>
       <label for="thirtys" id="thirty"><input type="checkbox" name="age[]" class="ages" id="thirtys" value="thirties"> Ages 30-39 </label>
       <label for="fourtys" id="fourty"><input type="checkbox" name="age[]" class="ages" id="fourtys" value="forties"> Ages 40-49</label>
       <label for="fiftys" id="fifty"><input type="checkbox" name="age[]" class="ages" id="fiftys" value="fifties"> Ages 50-59 </label>
       <label for="sixtyplus" id="sixty"><input type="checkbox" name="age[]" class="ages" id="sixtyplus" value="sixtyup"> Ages 60+</label>';
}
?>

When i try using

//where $vId = 2;
$sql_elite_ages = "select * from inf_ages_interacted where inf_id = $vId";
$rs_elite_ages = mysqli_query($vconncvnl, $sql_elite_ages);
$rs_elite_ages_all = mysqli_fetch_assoc($rs_elite_ages);
$rs_elite_ages_array = mysqli_fetch_array($rs_elite_ages); 

combined with

$agegroups = $rs_elite_ages_array;
$array= ' ';

foreach ($agegroups as $row){
echo $addary .= $row .' ';
}

my results are "2 2 2 2 2 2 2 2 2 2 2 2 2 2 teens 2 2 2 2 teens teens"




Aucun commentaire:

Enregistrer un commentaire