0

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 want to 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"

what i want to show is enter image description here

so the age group values match the relevant valued checkbox and if the value is present then the box is checked otherwise leave the box unchecked

so when i do this way @Lelio Faieta

$sql="SELECT `agegroup` FROM `inf_ages_interacted` WHERE inf_id = $vId";
$rs = mysqli_query($vconncvnl,$sql);

while($row = mysqli_fetch_assoc($rs)){

print_r($row); echo '<br>';

} the results i get is 

enter image description here

Meitie
  • 75
  • 1
  • 8
  • 1
    Please explain what output you want – MD. Jubair Mizan Feb 11 '19 at 08:22
  • Its important to know [how to prevent SQL injection in PHP](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). "wanna" isn't word. – danblack Feb 11 '19 at 08:22
  • @MD.JubairMizan the output I want is the items that are shown with the inf_id which are the same so for this case inf_id = 2 and the results in "agegroup" are 'kids,teens,twenties,thirties' so when I want them to show up as checked (see edited image above to show) but nothing is checked – Meitie Feb 11 '19 at 08:38
  • @danblack appologies that is slang ill change it now. "wanna means want to; or want a" in this case "i wanna take those" means "i want to take those" and yea i know about SQL injection i just taken the relevant snipits from the code – Meitie Feb 11 '19 at 08:39
  • @MD.JubairMizan i have added an extra image and explain if you need anything else please ask and thanks you in advance – Meitie Feb 11 '19 at 08:42
  • instead of doing select * select just the fields that you need. Your array will be a multidimensional one: first key for each row and then key for each column. you will loop (with foreach or while) through each row and you will get each column value by referencing it's name – Lelio Faieta Feb 11 '19 at 09:57
  • hey @LelioFaieta, im trying to do that at the moment `$sql="SELECT `agegroup` FROM `inf_ages_interacted` WHERE inf_id = $vId"; $rs = mysqli_query($vconncvnl,$sql); while($row = mysqli_fetch_assoc($rs)){ print_r($row); echo '
    '; echo $row;}` however the results i get is 4 arrays (1 array for each individual item ill add image to question so you can see
    – Meitie Feb 11 '19 at 10:26
  • @LelioFaieta i edited my question above to show you what happens – Meitie Feb 11 '19 at 10:30

1 Answers1

1

Instead of use foreach() use while()

This is the intended functionality of mysql_fetch_array(). If you want to not have the "duplicates" and just have the associative-array, use mysql_fetch_assoc() instead.

Example:

while ($row = mysql_fetch_assoc($data)) { ... }
MD. Jubair Mizan
  • 1,539
  • 1
  • 12
  • 20
  • hey so i tried that `while($row = mysqli_fetch_assoc($rs_elite_ages)){echo $row['agegroup'] .' '; $count++;` and that gives me the results ' kids teens twenties thirties' is there a way i can grab that `$row['agegroup']` outside of the loop? because i have the following `$checked = (strpos($row['agegroup'], 'kids') !== false) ? 'checked="checked"' : ''; echo '';` but if i put that in the while loop it will just make like 4 inputs one for each item @MD. Jubair – Meitie Feb 11 '19 at 10:11
  • If you need some data outside of loop you have to make another new array for that – MD. Jubair Mizan Feb 11 '19 at 10:29
  • i have added a bit extra (the part that goes "@Lelio Faieta" so you both can see the code and the results that is gotten once i use `print_r($row)` – Meitie Feb 11 '19 at 10:34
  • @Meitie So inside your while loop just create an array like `$data[]=$row['agegroup']` and check you get this array outside of loop then you can easily use `in_array()` or `array_search()` to find the which you want to Checked – MD. Jubair Mizan Feb 11 '19 at 10:34
  • 1
    IT WORKED :D i added the `$data[ ] =$row['agegroup']` and then used `$data` inside of my `$checked = in_array('kids', $data) ? 'checked="checked"' : '';` code which uses the in_array and it all works now :D – Meitie Feb 11 '19 at 10:40
  • @Meitie Cheers Dude . Happy Coding – MD. Jubair Mizan Feb 11 '19 at 10:42