Beispiel #1
0
 public function view()
 {
     $questionDB = new QuestionModelDB();
     //普通条件,检查GET参数
     $fieldArr = $questionDB->getFields();
     $where = $whereArr = array();
     foreach ($fieldArr as $v) {
         if (array_key_exists($v, $_GET) && $_GET[$v] !== null) {
             $where[] = "`{$v}` = ?";
             $whereArr[] = $_GET[$v];
         }
     }
     $whereStr = count($where) > 0 ? implode(" AND ", $where) : '1=1';
     if (array_key_exists('_search_field', $_GET) && $_GET['_search_field'] != '' && in_array($_GET['_search_field'], $fieldArr, true) && array_key_exists('_search_keyword', $_GET) && $_GET['_search_keyword'] != '') {
         $whereStr = $whereStr == '1=1' ? $_GET['_search_field'] . " LIKE ?" : $whereStr . " AND " . $_GET['_search_field'] . " LIKE ?";
         $whereArr[] = "%" . $_GET['_search_keyword'] . "%";
     }
     //排序条件
     $orderStr = '`id` DESC';
     if (!empty($_POST['order'])) {
         foreach ($_POST['order'] as $k => $v) {
             $orderArr[] = "`{$k}` {$v}";
         }
         $orderStr = implode(',', $orderArr);
     }
     //查询操作
     $sql = "SELECT * FROM `" . $questionDB->getTableName() . "` WHERE {$whereStr} ORDER BY {$orderStr}";
     $data = $questionDB->getData($sql, $whereArr, 20);
     $pageStr = $questionDB->getPageStr();
     //读取外键数据
     $this->setView('vote_id', $_GET['vote_id']);
     //模版显示
     $this->setView('pageStr', $pageStr);
     $this->setView('data', $data);
     $this->display('Question.html');
 }