/** * Compile {db ...} tag * it htf write * * @param string $tag_args * @return string */ function _compile_db_start($tag_args) { $attrs = $this->_parse_attrs($tag_args); foreach ($attrs as &$attr) { $attr = str_replace("'", '', $attr); } if (empty($attrs['from'])) { $this->_syntax_error("missing db_select table name", E_USER_ERROR, __FILE__, __LINE__); } if (!array_key_exists('_name', $attrs)) { $attrs['_name'] = 'records'; } $output = "<?php \n"; if ($this->db == '') { global $php; $php->autoload('db'); $this->db = $php->db; $output .= "global \$php;\n"; } $select = new SelectDB($this->db); $select->call_by = 'smarty'; // if(!array_key_exists('limit',$attrs) and !array_key_exists('page',$attrs)) $attrs['limit']=10; $attrs['select'] = str_replace('"', '', $attrs['select']); $attrs['select'] = str_replace("'", '', $attrs['select']); $select->put($attrs); $output .= "\$sql=\"" . $select->getsql() . "\";\n"; $output .= "\$this->_tpl_vars['{$attrs['_name']}']=\$php->db->query(\$sql)->fetchall();\n"; $output .= '$' . $attrs['_name'] . '_count = count($this->_tpl_vars["' . $attrs['_name'] . '"]);' . "\n"; $output .= 'for($' . $attrs['_name'] . '_i=0;$' . $attrs['_name'] . '_i<$' . $attrs['_name'] . '_count;$' . $attrs['_name'] . '_i++):' . "\n"; $output .= '$this->_records["' . $attrs['_name'] . '"]["index"]=$' . $attrs['_name'] . '_i;' . "\n"; $output .= '$this->_records["' . $attrs['_name'] . '"]["first"]= $' . $attrs['_name'] . '_i==0;' . "\n"; $output .= '$this->_records["' . $attrs['_name'] . '"]["last"]= $' . $attrs['_name'] . '_i==($' . $attrs['_name'] . '_count-1);' . "\n"; $output .= '$this->_sections["' . $attrs['_name'] . '"]["index"]=$' . $attrs['_name'] . '_i;' . "\n"; $output .= "?>\n"; return $output; }
/** * 获取一个数据列表,功能类似于gets,此方法仅用于SiaoCMS,不作为同样类库的方法 * @param $params * @param $get * @return array */ function getList(&$params, $get = 'data') { $selectdb = new SelectDB($this->db); $selectdb->from($this->table); $selectdb->select($this->select); $selectdb->limit(isset($params['row']) ? $params['row'] : 10); unset($params['row']); $selectdb->order(isset($params['order']) ? $params['order'] : $this->primary . ' desc'); unset($params['order']); if (isset($params['typeid'])) { $selectdb->where($this->foreignkey . '=' . $params['typeid']); unset($params['typeid']); } $selectdb->put($params); if (array_key_exists('page', $params)) { $selectdb->paging(); global $php; $php->env['page'] = $params['page']; $php->env['start'] = 10 * intval($params['page'] / 10); if ($selectdb->pages > 10 and $params['page'] < $php->env['start']) { $php->env['more'] = 1; } $php->env['end'] = $selectdb->pages - $php->env['start']; $php->env['pages'] = $selectdb->pages; $php->env['pagesize'] = $selectdb->page_size; $php->env['num'] = $selectdb->num; } if ($get === 'data') { return $selectdb->getall(); } elseif ($get === 'sql') { return $selectdb->getsql(); } }
function getPlot(&$attrs) { $select = new SelectDB($this->db); $select->call_by = 'smarty'; $select->from(TABLE_PREFIX . '_plot'); unset($attrs['get'], $attrs['name'], $attrs['key'], $attrs['func']); $select->put($attrs); return $select->getsql(); }