Skip to content

Instantly share code, notes, and snippets.

@uded
Last active December 22, 2015 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uded/6447380 to your computer and use it in GitHub Desktop.
Save uded/6447380 to your computer and use it in GitHub Desktop.
The last iteration, `while ($row = $result->FetchRow()) {` does include an option to use associative arrays to manage a multi select list with both display and value or a non-associative array for just values which will be used to as text display in the select widget.
<?
public static function getVariableOptions($params, &$report) {
$report->conn->SetFetchMode(ADODB_FETCH_NUM);
$query = 'SELECT DISTINCT '.$params['column'].' FROM '.$params['table'];
if(isset($params['where'])) {
$query .= ' WHERE '.$params['where'];
}
$macros = $report->macros;
foreach($macros as $key=>$value) {
if(is_array($value)) {
foreach($value as $key2=>$value2) {
$value[$key2] = mysql_real_escape_string(trim($value2));
}
$macros[$key] = $value;
}
else {
$macros[$key] = mysql_real_escape_string($value);
}
if($value === 'ALL') $macros[$key.'_all'] = true;
}
//add the config and environment settings as macros
$macros['config'] = PhpReports::$config;
$macros['environment'] = PhpReports::$config['environments'][$report->options['Environment']];
$result = $report->conn->Execute(PhpReports::renderString($query, $macros));
if (!$result) {
throw new Exception("Unable to get variable options: ".$report->conn->ErrorMsg());
}
$options = array();
if(isset($params['all']) && $params['all']) {
$options[] = 'ALL';
}
while ($row = $result->FetchRow()) {
if ($result->FieldCount() > 1) {
$options[] = array('display'=>$row[0], 'value'=>$row[1]);
} else {
$options[] = $row[0];
}
}
return $options;
}
<?
while ($row = $result->FetchRow()) {
if ($result->FieldCount() > 1) {
$options[] = array('display'=>$row[0], 'value'=>$row[1]);
} else {
$options[] = $row[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment