function create_model($arr, $table_name, $table_name_to_class) { if (!file_exists('data/application/models')) { mkdir('data/application/models', 0755, true); } $file = fopen("data/application/models/" . ucwords($table_name) . "_model.php", "w"); $file_data = get_model_data($arr, $table_name); // get_model_data function are in include page fwrite($file, $file_data); fclose($file); $file = fopen("data/application/models/Common_model.php", "w"); $file_data = get_common_model_data($arr, $table_name); // get_common_model_data function are in include page fwrite($file, $file_data); fclose($file); }
/** * 获取模型缓存(非会员模型) */ protected function get_model($name = 'content', $site = 0) { return get_model_data($name, $site); }
/** * 表单内容URL */ function form_show_url($modelid, $data) { if (empty($data) || empty($modelid)) { return null; } $form = get_model_data('form'); $config = $form[$modelid]['setting']['form']['url']; if (isset($config['show']) && $config['show']) { $data['y'] = date('Y', $data['inputtime']); $data['m'] = date('m', $data['inputtime']); $data['d'] = date('d', $data['inputtime']); $data['mid'] = $modelid; if (version_compare(PHP_VERSION, '5.5.0') >= 0) { $rep = new php5replace($data); $url = preg_replace_callback('#{([a-z_0-9]+)}#Ui', array($rep, 'php55_replace_data'), $config['show']); } else { $url = preg_replace('#{([a-z_0-9]+)}#Uei', "\$data[\\1]", $config['show']); } if ($config['tohtml'] && $config['htmldir']) { $url = $config['htmldir'] . '/' . $url; } if (version_compare(PHP_VERSION, '5.5.0') >= 0) { $rep = new php5replace($data); $url = preg_replace_callback('#{([a-z_0-9]+)\\((.*)\\)}#U', array($rep, 'php55_replace_function'), $url); } else { $url = preg_replace('#{([a-z_0-9]+)\\((.*)\\)}#Uie', "\\1(safe_replace('\\2'))", $url); } return SITE_PATH . $url; } else { return url('form/show', array('id' => $data['id'], 'modelid' => $modelid)); } }
/** * 内容页URL地址 */ function getUrl($data, $page = 0) { // 默认地址 $url = url('content/show', array('id' => $data['id'])); $cats = get_category_data(); $cat = $cats[$data['catid']]; $model = get_model_data(); $field = $model[$cat['modelid']]['fields']['data']; if ($field) { foreach ($field as $t) { if ($t['formtype'] == 'wurl') { if (isset($data[$t['field']])) { // 跳转外链 return $url; } } } } unset($cats); if (isset($cat['setting']['url']['use']) && $cat['setting']['url']['use'] == 1 && $cat['setting']['url']['show']) { $data['y'] = date('Y', $data['inputtime']); $data['m'] = date('m', $data['inputtime']); $data['d'] = date('d', $data['inputtime']); //$data['site'] = $cat['site']; $data['site'] = SITE_ID; $data['dir'] = $cat['catdir']; $data['pdir'] = getParentDir($data['catid'], $cat['setting']['url']['catjoin']); $data['page'] = $page; if (version_compare(PHP_VERSION, '5.5.0') >= 0) { $rep = new php5replace($data); $url = !is_numeric($page) || $page > 1 ? preg_replace_callback('#{([a-z_0-9]+)}#Ui', array($rep, 'php55_replace_data'), $cat['setting']['url']['show_page']) : preg_replace_callback('#{([a-z_0-9]+)}#Ui', array($rep, 'php55_replace_data'), $cat['setting']['url']['show']); } else { $url = !is_numeric($page) || $page > 1 ? preg_replace('#{([a-z_0-9]+)}#Uei', "\$data[\\1]", $cat['setting']['url']['show_page']) : preg_replace('#{([a-z_0-9]+)}#Uei', "\$data[\\1]", $cat['setting']['url']['show']); } if ($cat['setting']['url']['tohtml'] && $cat['setting']['url']['htmldir']) { $url = $cat['setting']['url']['htmldir'] . '/' . $url; } if (version_compare(PHP_VERSION, '5.5.0') >= 0) { $rep = new php5replace($data); $url = preg_replace_callback('#{([a-z_0-9]+)\\((.*)\\)}#U', array($rep, 'php55_replace_function'), $url); } else { $url = preg_replace('#{([a-z_0-9]+)\\((.*)\\)}#Uie', "\\1(safe_replace('\\2'))", $url); } return SITE_PATH . $url; } if ($page) { $url = url('content/show', array('id' => $data['id'], 'page' => $page)); } return $url; }
/** * 解析标签list */ protected function _listdata($param) { $_param = explode(' ', $param); $param = array(); foreach ($_param as $p) { $mark = strpos($p, '='); if ($p && $mark !== false) { $var = substr($p, 0, $mark); $val = substr($p, $mark + 1); if (isset($var) && $var) { $param[$var] = $val; } } } $system = $fields = $_fields = $not = $in = $or = $between = $like = array(); $dbcache = isset($param['cache']) ? (int) $param['cache'] : 0; unset($param['cache']); if (is_array($param)) { foreach ($param as $key => $val) { //参数归类 if (in_array($key, array('return', 'more', 'page', 'urlrule', 'num', 'join', 'on', 'order', 'table', 'pagesize', 'pagerule', 'action', 'tag', 'extend', 'site', 'form', 'fields'))) { $system[$key] = $val; } else { if (substr($key, 0, 3) == 'NOT') { $key = substr($key, 3); $not[] = $key; } elseif (substr($key, 0, 2) == 'OR') { $key = substr($key, 2); $or[] = $key; } elseif (substr($key, 0, 2) == 'IN') { $key = substr($key, 2); $in[] = $key; } elseif (substr($key, 0, 2) == 'BW') { $key = substr($key, 2); $between[] = $key; } elseif (substr($key, 0, 4) == 'LIKE') { $key = substr($key, 4); $like[] = $key; } $fields[$key] = $val; $_fields[] = $key; } } } $where = ''; //设置站点id $system['site'] = !isset($system['site']) || empty($system['site']) ? App::get_site_id() : $system['site']; //Action判断 if (isset($system['action']) && $system['action'] == 'position') { //推荐位 $data = position($system['site'], $fields['id'], isset($fields['catid']) ? $fields['catid'] : 0, isset($system['num']) ? $system['num'] : 0); if ($data) { $db = Controller::model('content'); foreach ($data as $i => $t) { if ($t['contentid']) { $row = $db->db->where('id', $t['contentid'])->get('content_' . $system['site'])->row_array(); $data[$i] = $t + $row; } } } if (isset($system['return']) && $system['return'] && $system['return'] != 't') { return array('return_' . $system['return'] => $data, 'total_' . $system['return'] => count($data)); } return array('return' => $data, 'total' => count($data)); } elseif (isset($system['action']) && $system['action'] == 'keywords') { //搜索关键字 $search = Controller::model('search'); $num = $system['num'] ? (int) $system['num'] : 5; if (isset($system['order']) && $system['order']) { $order = null; $orders = explode(',', $system['order']); foreach ($orders as $t) { list($_field, $_order) = explode('_', $t); if (in_array($_field, array('id', 'keywords', 'addtime', 'total'))) { $_orderby = isset($_order) && strtoupper($_order) == 'ASC' ? 'ASC' : 'DESC'; $order .= '`' . $_field . '` ' . $_orderby . ','; } } if (substr($order, -1) == ',') { $order = substr($order, 0, -1); } } else { $order = '`total` DESC'; } $data = $search->execute('select distinct keywords as title from ' . $search->prefix . 'search order by ' . $order . ' limit ' . $num, true, $dbcache); if (isset($system['return']) && $system['return'] && $system['return'] != 't') { return array('return_' . $system['return'] => $data, 'total_' . $system['return'] => count($data)); } return array('return' => $data, 'total' => count($data)); } elseif (isset($system['action']) && $system['action'] == 'sql') { // } elseif (isset($system['action']) && $system['action'] == 'relation') { if (isset($system['tag']) && $system['tag']) { //按关键字搜索 if (isset($fields['id']) && $fields['id']) { $where .= '`id`<>' . (int) $fields['id']; } $tags = @explode(',', $system['tag']); $kwhere = $k = NULL; foreach ($tags as $tag) { if ($tag) { if (empty($k)) { $kwhere .= '`title` like "%' . $tag . '%"'; } else { $kwhere .= ' OR `title` like "%' . $tag . '%"'; } $k = 1; } } if ($kwhere) { $where .= ' AND (' . $kwhere . ')'; } unset($k, $tags, $tag, $kwhere, $system['table'], $fields['id']); } else { //手动设置的相关文章 $data = $this->relation($fields['id'], $system['num']); if (isset($system['return']) && $system['return'] && $system['return'] != 't') { return array('return_' . $system['return'] => $data, 'total_' . $system['return'] => count($data)); } return array('return' => $data, 'total' => count($data)); } } elseif (isset($system['action']) && $system['action'] == 'field') { //字段信息 $mods = get_model_data(); $mod = $mods[$fields['modelid']]; $data = array(); if ($mod['fields']) { foreach ($mod['fields']['data'] as $t) { if ($fields['name'] == $t['field']) { //加载字段配置文件 App::auto_load('fields'); $data_fields = ''; $data_fields .= '<tr id="fine_' . $t['field'] . '">'; $data_fields .= '<th>' . (isset($t['not_null']) && $t['not_null'] ? '<font color="red">*</font> ' : '') . $t['name'] . ':</th>'; $data_fields .= '<td>'; $func = 'content_' . $t['formtype']; //防止出错,把字段内容转换成数组格式 $content = array($fields['value']); $content = var_export($content, true); $field_config = var_export($t, true); if (function_exists($func)) { eval("\$data_fields .= " . $func . "(" . $t['field'] . ", " . $content . ", " . $field_config . ");"); } $data_fields .= $t['tips'] ? '<div class="onShow">' . $t['tips'] . '</div>' : ''; $data_fields .= '<span id="ck_' . $t['field'] . '"></span>'; $data_fields .= '</td>'; $data_fields .= '</tr>'; $data[0]['form'] = $data_fields; if ($t['setting']) { $c = string2array($t['setting']); if ($c['content']) { $select = explode(PHP_EOL, $c['content']); $vdata = array(); foreach ($select as $i => $c) { list($n, $v) = explode('|', $c); $vdata[trim($n)] = $v === null ? trim($n) : trim($v); } $data[0]['data'] = $vdata; } } } } } if (isset($system['return']) && $system['return'] && $system['return'] != 't') { return array('return_' . $system['return'] => $data); } return array('return' => $data); } //主表判断 if (isset($system['table']) && $system['table']) { $table = $system['table']; } elseif (isset($system['form']) && $system['form']) { $table = 'form_' . $system['site'] . '_' . $system['form']; } else { $table = 'content'; } //加载Model实例 if (strpos($table, '.') !== false) { list($plugin, $table) = explode('.', $table); $db = App::plugin_model($plugin, $table); } else { $db = Controller::model('content'); if (strpos($table, 'content') === 0) { $table .= '_' . $system['site']; } } $table = $db->prefix . $table; $db->table_name = $table; $table_join = $table_data = $table_fields = $table_join_fields = $table_data_fields = $arrchilds = null; $_table_fields = $db->get_table_fields(); $table_fields = array_intersect($_fields, $_table_fields); //status判断 if (in_array('status', $_table_fields)) { $where .= ($where ? ' AND ' : ' ') . ' `' . $table . '`.`status`=1'; } if (isset($fields['catid']) && $fields['catid']) { //栏目信息 $cats = get_category_data($system['site']); $cat = $cats[$fields['catid']]; } if (isset($system['join']) && $system['join'] && $system['on']) { //JOIN联合查询 $table_join = $system['join']; //加载Model实例 if (strpos($table_join, '.') !== false) { list($plugin, $table_join) = explode('.', $table_join); $db_join = App::plugin_model($plugin, $table_join); } else { $db_join = Controller::model($table_join); } $_table_join_fields = $db_join->get_table_fields(); $table_join_fields = array_intersect($_fields, $_table_join_fields); foreach ($table_join_fields as $k => $c) { if (in_array($c, $table_fields)) { unset($table_join_fields[$k]); } } $table_join = $db->prefix . $table_join; } if (isset($system['more']) && $system['more']) { //附表 $model = null; if ($table == $db->prefix . 'content_' . $system['site']) { $models = get_model_data('content', $system['site']); if (isset($fields['catid']) && $fields['catid'] && isset($cat) && $cat) { $model = $models[$cat['modelid']]; } elseif (isset($fields['modelid']) && $fields['modelid']) { $model = $models[$fields['modelid']]; } } elseif ($table == $db->prefix . 'member' && isset($fields['modelid']) && $fields['modelid']) { $cache = new cache_file(); $models = $cache->get('model_member'); $model = $models[$fields['modelid']]; } if ($model) { $table_data = $model['tablename']; $db_data = Controller::model($table_data); $_table_data_fields = $db_data->get_table_fields(); $table_data_fields = array_intersect($_fields, $_table_data_fields); foreach ($table_data_fields as $k => $c) { if (in_array($c, $table_fields)) { unset($table_data_fields[$k]); } } $table_data = $db->prefix . $table_data; } } //WHERE整合 $fieldsAll = array($table => $table_fields, $table_join => $table_join_fields, $table_data => $table_data_fields); foreach ($fieldsAll as $_table => $t) { if (is_array($t)) { foreach ($t as $f) { if ($fields[$f] == '') { continue; } $and_or = in_array($f, $or) ? 'OR' : 'AND'; //栏目条件根据子栏目来做为条件 if ($f == 'catid' && isset($fields['catid']) && $fields['catid']) { if (isset($cat) && $cat && $cat['child']) { $arrchilds = $cat['arrchilds']; $not_in = in_array($f, $not) ? 'NOT IN' : 'IN'; $where .= ' ' . $and_or . ' `' . $_table . '`.`catid` ' . $not_in . ' (' . $arrchilds . ')'; } elseif (strpos($fields['catid'], ',') !== false) { $not_in = in_array($f, $not) ? 'NOT IN' : 'IN'; $where .= ' ' . $and_or . ' `' . $_table . '`.`catid` ' . $not_in . ' (' . $fields['catid'] . ')'; } else { $not_in = in_array($f, $not) ? '<>' : '='; $where .= ' ' . $and_or . ' `' . $_table . '`.`catid`' . $not_in . $fields['catid']; } } elseif ($f == 'thumb' && isset($fields['thumb']) && is_numeric($fields['thumb'])) { $where .= $fields['thumb'] ? ' ' . $and_or . ' `' . $_table . '`.`thumb`<>""' : ' ' . $and_or . ' `' . $_table . '`.`thumb`=""'; } else { $not_in = in_array($f, $in) ? 'IN' : ''; $not_in = in_array($f, $not) ? 'NOT IN' : $not_in; if (in_array($f, $between)) { if (strpos($fields[$f], '_') !== false) { list($v1, $v2) = explode('_', $fields[$f]); $v1 = is_numeric($v1) ? $v1 : '"' . addslashes($v1) . '"'; $v2 = is_numeric($v2) ? $v2 : '"' . addslashes($v2) . '"'; $where .= ' ' . $and_or . ' `' . $_table . '`.`' . $f . '` BETWEEN ' . $v1 . ' AND ' . $v2; } elseif (strpos($fields[$f], ',') !== false) { list($v1, $v2) = explode(',', $fields[$f]); $v1 = is_numeric($v1) ? $v1 : '"' . addslashes($v1) . '"'; $v2 = is_numeric($v2) ? $v2 : '"' . addslashes($v2) . '"'; $where .= ' ' . $and_or . ' `' . $_table . '`.`' . $f . '` BETWEEN ' . $v1 . ' AND ' . $v2; } else { continue; } } elseif ($not_in) { $where .= ' ' . $and_or . ' `' . $_table . '`.`' . $f . '` ' . $not_in . ' (' . $fields[$f] . ')'; } elseif (in_array($f, $like)) { $value = addslashes($fields[$f]); $where .= ' ' . $and_or . ' `' . $_table . '`.`' . $f . '` LIKE "' . $value . '"'; } else { $value = is_numeric($fields[$f]) ? $fields[$f] : '"' . addslashes($fields[$f]) . '"'; $where .= ' ' . $and_or . ' `' . $_table . '`.`' . $f . '`=' . $value . ''; } } } } } if ($where) { if (substr($where, 0, 4) == ' AND') { $where = ' WHERE' . substr($where, 4); } elseif (substr($where, 0, 3) == ' OR') { $where = ' WHERE' . substr($where, 3); } else { $where = ' WHERE' . $where; } //对WHERE延展 if (isset($system['extend']) && substr($system['extend'], 0, 6) == 'WHERE.') { $where .= ' ' . str_replace('-', ' ', substr($system['extend'], 6)); unset($system['extend']); } } //延展list $extend = ''; if (isset($system['extend']) && $system['extend']) { $extend = ' ' . str_replace('-', ' ', $system['extend']); } //FROM整合 $from = 'FROM ' . $table; if ($table_data) { $from .= ' LEFT JOIN ' . $table_data . ' ON `' . $table . '`.`' . $db->get_primary_key() . '`=`' . $table_data . '`.`' . $db_data->get_primary_key() . '`'; } if ($table_join && $system['on']) { $_join_name = null; if (in_array($system['on'], $_table_fields)) { $_join_name = $table; } elseif (isset($_table_data_fields) && in_array($system['on'], $_table_data_fields)) { $_join_name = $table_data; } if ($_join_name) { $from .= ' LEFT JOIN ' . $table_join . ' ON `' . $table_join . '`.`' . $db_join->get_primary_key() . '`=`' . $_join_name . '`.`' . $system['on'] . '`'; } } //ORDER排序 $order = ''; if (isset($system['order']) && $system['order']) { if (strtoupper($system['order']) == 'RAND()') { $order .= ' ORDER BY RAND()'; } else { $orders = explode(',', $system['order']); foreach ($orders as $t) { list($_field, $_order) = explode('_', $t); $_name = null; if (in_array($_field, $_table_fields)) { $_name = $table; } elseif (isset($_table_data_fields) && in_array($_field, $_table_data_fields)) { $_name = $table_data; } elseif (isset($_table_join_fields) && in_array($_field, $_table_join_fields)) { $_name = $table_join; } $_orderby = isset($_order) && strtoupper($_order) == 'ASC' ? 'ASC' : 'DESC'; if ($_name) { $order .= ' `' . $_name . '`.`' . $_field . '` ' . $_orderby . ','; } } if (substr($order, -1) == ',') { $order = ' ORDER BY' . substr($order, 0, -1); } } } //limit与分页 $limit = ''; if (isset($system['num']) && $system['num']) { $limit = ' LIMIT ' . $system['num']; } elseif (isset($system['page'])) { $pageurl = ''; $system['page'] = (int) $system['page'] ? (int) $system['page'] : 1; if ($system['urlrule']) { $pageurl = str_replace(array('_page_', '[page]'), '{page}', $system['urlrule']); $pagesize = $system['pagesize'] ? $system['pagesize'] : (isset($cat['pagesize']) ? $cat['pagesize'] : 10); } elseif ($cat) { $pageurl = getCaturl($cat, '{page}'); $pagesize = $system['pagesize'] ? $system['pagesize'] : $cat['pagesize']; } else { $pagesize = $system['pagesize'] ? $system['pagesize'] : 10; $pageurl = '{page}'; } $sql = 'SELECT count(*) AS total ' . $from . ' ' . $where; $count = $db->execute($sql, false, $dbcache); $total = $count['total']; $pagelist = Controller::instance('pagelist'); $pagelist->loadconfig($system['pagerule'] ? $system['pagerule'] : 'pagerule'); $start_id = $pagesize * ($system['page'] - 1); $limit = ' LIMIT ' . $start_id . ',' . $pagesize; $pagelist = $pagelist->total($total)->url($pageurl)->num($pagesize)->page($system['page'])->output(); } //查询字段筛选 if (isset($system['fields']) && $system['fields']) { $fields_array = explode(',', $system['fields']); //字段参数转为数组 $select_array = array(); foreach ($fields_array as $t) { //遍历字段参数数组,筛选无用字段、字段附加表前缀 if (in_array($t, $_table_fields)) { //主表字段 $select_array[] = '`' . $table . '`.`' . $t . '`'; } elseif (isset($_table_data_fields) && in_array($t, $_table_data_fields)) { //附表字段 $select_array[] = '`' . $table_data . '`.`' . $t . '`'; } elseif (isset($_table_join_fields) && in_array($t, $_table_join_fields)) { //联合表字段 $select_array[] = '`' . $table_join . '`.`' . $t . '`'; } } $select_field = empty($select_array) ? ' * ' : ' ' . implode(',', $select_array) . ' '; //组合成sql查询格式 unset($select_array, $fields_array); } else { $select_field = ' * '; } //查询结果 $sql = 'SELECT' . $select_field . $from . $where . $order . $limit . $extend; $data = $db->execute($sql, true, $dbcache); //释放变量 unset($_param, $param, $par, $p, $fields, $_fields, $not, $in, $or, $between, $dbcache, $like); unset($table, $db, $table_join, $table_data, $table_fields, $table_join_fields, $table_data_fields, $arrchilds, $_table_fields); unset($fieldsAll, $_table_data_fields, $cache, $db_join, $cats, $cat, $models, $model, $db_data, $where, $order, $from); if (isset($system['return']) && $system['return'] && $system['return'] != 't') { return array('sql_' . $system['return'] => $sql, 'total_' . $system['return'] => isset($total) ? $total : count($data), 'return_' . $system['return'] => $data, 'pagelist_' . $system['return'] => $pagelist); } return array('pagelist' => $pagelist, 'return' => $data, 'sql' => $sql, 'total' => isset($total) ? $total : count($data)); }
/** * 相关文章列表 */ protected function relation($id, $num = 10, $more = 0) { if (empty($id)) { return false; } $db = Controller::model('content'); $row = $db->from('content_' . App::get_site_id() . '_extend')->where('id=' . (int) $id)->select(false); if (empty($row) || empty($row['relation'])) { return null; } $ids = $row['relation']; $num = $num ? $num : 10; if ($more) { $cats = get_category_data(App::get_site_id()); $models = get_model_data('content', App::get_site_id()); $table = $models[$cats[$row['catid']]['modelid']]['tablename']; if ($table) { $sql = 'select * from `' . $this->ci->db->dbprefix('content_' . App::get_site_id()) . '` as a left join `' . $this->ci->db->dbprefix($table) . '` as b on a.id=b.id where a.id IN (' . $ids . ') order by a.listorder desc, a.updatetime desc limit ' . $num; $data = $this->ci->db->query($sql)->result_array(); return $data; } } $data = $db->from('content_' . App::get_site_id())->where('id in (' . $ids . ')')->order('listorder desc, updatetime desc')->limit($num)->select(); return $data; }
private function getSQL($param, $kw, $kw_fields, $kw_or) { $site_id = App::get_site_id(); $tablename = 'content_' . $site_id; $this->set_table_name($tablename); //���õ�ǰģ��Ϊ����ģ�� $_fields = $this->get_table_fields(); if (is_array($param) && $param) { $where_or = $param_fields = $_data_fields = $table_fields = $data_fields = array(); $category = get_category_data(); //��Ŀ��� foreach ($param as $key => $val) { //������� if (substr($key, 0, 2) == 'OR') { unset($param[$key]); $key = substr($key, 2); $where_or[] = $key; $param[$key] = $val; } } if (isset($param['modelid']) && $param['modelid']) { $cache = get_model_data(); //����ģ����� $table = $cache[$param['modelid']]['tablename']; if ($table) { $this->set_table_name($table); $_data_fields = $this->get_table_fields(true); } } elseif (isset($param['catid']) && $param['catid']) { $table = $category[$param['catid']]['tablename']; if ($table) { $this->set_table_name($table); $_data_fields = $this->get_table_fields(true); } } $more = isset($param['more']) && $param['more'] && $table && $_data_fields ? true : false; foreach ($param as $k => $v) { if (in_array($k, $_fields)) { $table_fields[] = $k; } elseif (isset($_data_fields) && in_array($k, $_data_fields)) { $more = true; $data_fields[] = $k; } } } else { if (empty($kw)) { return false; } } $where = '`' . $this->prefix . $tablename . '`.`status`=1'; if ($kw) { $kw = addslashes($kw); if ($kw_fields) { $kw_fields = explode(',', $kw_fields); $kw_where = ''; $kw_count = 0; foreach ($kw_fields as $f) { $andor = empty($kw_count) ? '' : ($kw_or ? ' OR' : ' AND'); if (in_array($f, $_fields)) { //���� $kw_where .= $andor . ' `' . $this->prefix . $tablename . '`.`' . $f . '` LIKE \'%' . $kw . '%\''; } elseif (isset($table) && isset($_data_fields) && in_array($f, $_data_fields)) { //���� $kw_where .= $andor . ' `' . $this->prefix . $table . '`.`' . $f . '` LIKE \'%' . $kw . '%\''; $more = true; } $kw_count = 1; } $where .= ' AND (' . $kw_where . ')'; } else { $where .= ' AND `' . $this->prefix . $tablename . '`.`title` LIKE \'%' . $kw . '%\''; } } if (isset($param['modelid']) && $param['modelid']) { $where .= ' AND `' . $this->prefix . $tablename . '`.`modelid`=' . (int) $param['modelid']; } if (isset($param['catid']) && $param['catid']) { $cat = $category[$param['catid']]; $where .= $cat['child'] ? ' AND `' . $this->prefix . $tablename . '`.`catid` IN (' . $cat['arrchilds'] . ')' : ' AND `' . $this->prefix . $tablename . '`.`catid`=' . (int) $param['catid']; } unset($param['catid'], $param['modelid']); if ($table_fields) { foreach ($table_fields as $field) { if (isset($param[$field]) && $param[$field]) { $value = $param[$field]; $andor = is_array($where_or) && in_array($field, $where_or) ? 'OR' : 'AND'; if (is_numeric($value)) { $where .= ' ' . $andor . ' `' . $this->prefix . $tablename . '`.`' . $field . '`=' . $value; } elseif (substr($value, 0, 1) == '%' && substr($value, -1, 1) == '%') { $where .= ' ' . $andor . ' `' . $this->prefix . $tablename . '`.`' . $field . '` LIKE \'' . $value . '\''; } else { $where .= ' ' . $andor . ' `' . $this->prefix . $tablename . '`.`' . $field . '`=\'' . $param[$field] . '\''; } } } } if ($data_fields && $table && $more) { foreach ($data_fields as $field) { if (isset($param[$field]) && $param[$field]) { $value = $param[$field]; $andor = is_array($where_or) && in_array($field, $where_or) ? 'OR' : 'AND'; if (is_numeric($value)) { $where .= ' ' . $andor . ' `' . $this->prefix . $table . '`.`' . $field . '`=' . $value; } elseif (substr($value, 0, 1) == '%' && substr($value, -1, 1) == '%') { $where .= ' ' . $andor . ' `' . $this->prefix . $table . '`.`' . $field . '` LIKE \'' . $value . '\''; } else { $where .= ' ' . $andor . ' `' . $this->prefix . $table . '`.`' . $field . '`=\'' . $param[$field] . '\''; } } } } $from = $more && $table ? $this->prefix . $tablename . ' LEFT JOIN ' . $this->prefix . $table . ' ON `' . $this->prefix . $tablename . '`.`id`=`' . $this->prefix . $table . '`.`id`' : $this->prefix . $tablename . ''; $sql = 'SELECT * FROM ' . $from . ' WHERE ' . $where . ' ORDER BY `' . $this->prefix . $tablename . '`.`updatetime` DESC LIMIT 500'; return array($sql, $from); }