I need to get all the values in one column in a table so I can create a whitelist for another query. Here's how I'm getting that:
$stmt = $dbh->prepare("select GROUP_CONCAT( cohort_id SEPARATOR ',') as cohort_id from ( select distinct cohort_id from table_cohorts) as m");
$stmt->execute();
$row = $stmt->fetch();
$avails = json_encode($row);
If I var_dump $avails, I get this:
string(125) "{"cohort_id":"national_percent,database_percent,cohort_1,cohort_2","0":"national_percent,database_percent,cohort_1,cohort_2"}"
The stuff with "cohort_id" is what I want. I need to form an array so I can drop it in something like this:
(in_array($sortvalue, $arrayofpossibleoptions)) //$sortvalue comes from AJAX
How can I get this in the right format? One further wrinkle, I need to add one additional value to the $arrayofpossibleoptions or something like this, but not sure what the proper syntax is:
(in_array($sortvalue, $arrayofpossibleoptions || 'another' ))
If I var_dump $row, I get this:
array(2) {
["cohort_id"]=>
string(51) "national_percent,database_percent,cohort_1,cohort_2"
[0]=>
string(51) "national_percent,database_percent,cohort_1,cohort_2"
}
group_concat()inside the query, since you're just going to be UNDOING that concatting anyways. kill the concat, do a normal query, fetch in a loop, and build your array in php.$stmt = $dbh->prepare("select cohort_id as cohort_id from ( select distinct cohort_id from table_cohorts) as m");$stmt->execute();$row = $stmt->fetch();$categories = explode(",",$row["cohort_id"]);while($row = ->fetch()) { $data[] = explode(...) }to build a new array of your exploded stuff.