function smd_query($atts, $thing) { extract(lAtts(array( 'column' => '', 'table' => '', 'where' => '', 'query' => '', 'form' => '', 'colsform' => '', 'break' => '', 'wraptag' => '', 'label' => '', 'labeltag' => '', 'class' => '', 'debug' => '0', ),$atts)); // Grab the form or embedded $thing $thing = (empty($form)) ? $thing : fetch_form($form); $colsform = (empty($colsform)) ? '' : fetch_form($colsform); $rs = array(); $out = array(); $colout = array(); // query overrides column/table/where, show overrides everything if ($query) { $query = smd_query_parse($query); $rs = getRows($query, $debug); } else { if ($column && $table) { $column = smd_query_parse($column); $table = smd_query_parse($table); $where = smd_query_parse($where); $rs = safe_rows($column, $table, $where, $debug); } else { trigger_error("You must specify at least 1 'column' and a 'table'."); } } if ($rs) { if ($debug > 1) { echo "++ QUERY RESULT SET ++"; dmp(count($rs). " ROWS"); dmp($rs); } $replacements = array(); $colreplacements = array(); $qry_rowcnt = 0; foreach ($rs as $row) { foreach ($row as $colid => $val) { if ($qry_rowcnt == 0 && $colsform) { $colreplacements['{'.$colid.'}'] = $colid; } // Construct the replacement array $replacements['{'.$colid.'}'] = $val; } if ($debug > 1) { echo "++ REPLACEMENTS ++"; dmp($replacements); } $out[] = parse(strtr($thing, $replacements)); $qry_rowcnt++; } } if ($out) { if ($colreplacements) { $colout[] = parse(strtr($colsform, $colreplacements)); } return doLabel($label, $labeltag).doWrap(array_merge($colout,$out), $wraptag, $break, $class); } } // Returns a string with any ? variables replaced with their globals from $pretext or $thisarticle function smd_query_parse($item) { global $pretext, $thisarticle; $modRE = '/(\?)([A-Za-z0-9_\- ]+)/'; $numMods = preg_match_all($modRE, $item, $mods); for ($modCtr = 0; $modCtr < $numMods; $modCtr++) { $modChar = $mods[1][$modCtr]; $modItem = trim($mods[2][$modCtr]); $lowitem = strtolower($modItem); if (array_key_exists($lowitem, $pretext)) { $item = str_replace($modChar.$modItem, $pretext[$lowitem], $item); } else if (isset($thisarticle[$lowitem])) { $item = str_replace($modChar.$modItem, $thisarticle[$lowitem], $item); } else { $item = str_replace($modChar.$modItem, $modItem, $item); } } return $item; }