Example #1
0
 function __construct(&$db, $list_sql, $join_sql, $inserted = false)
 {
     $this->db =& $db;
     $this->inserted = $inserted;
     $list_sql = new SelectSQL($list_sql);
     $this->join_sql = new SelectSQL($join_sql);
     if (count($list_sql->SelectArray()) != 2) {
         throw new Exception("Запрос '" . $list_sql->SQL() . "' должен выбирать 2 столбца!");
     }
     if (count($this->join_sql->SelectArray()) != 1) {
         throw new Exception("Запрос '" . $this->join_sql->SQL() . "' должен выбирать 1 столбец!");
     }
     if (!$this->join_sql->FromOneTable()) {
         throw new Exception("Запрос '" . $this->join_sql->SQL() . "' должен быть из одной таблицы!");
     }
     $this->where_array = explode("=", $this->join_sql->WhereString());
     if (!$this->inserted) {
         if (count($this->where_array) != 2) {
             throw new Exception("Запрос '" . $this->join_sql->SQL() . "' должен быть по одному условию!");
         }
         if ($result = $this->db->Query_Fetch($this->join_sql->SQL())) {
             foreach ($result as $row) {
                 $this->join_array[] = $row[0];
             }
             $this->join_array = array_flip($this->join_array);
         }
     }
     foreach ($this->where_array as $k => $v) {
         $this->where_array[$k] = trim($v);
     }
     $table = Translit($this->join_sql->FromString());
     $result = $this->db->Query_Fetch($list_sql->SQL());
     foreach ($result as $row) {
         $name = $table . "_id_" . $row[0];
         $this->elements[$name] = new FCheckbox($name, $row[1], isset($this->join_array[$row[0]]) ? true : false);
     }
 }
	function PageSQL($sql, $rowsPerPage=10, $thisPage=1, $countsql=NULL){
		$this->rowsPerPage = $rowsPerPage;
		if ($countsql == NULL || $countsql === 0){
			$this->numRowsAll = $this->numRows;

	//	}else if ($countsql === (int) $countsql){
		}else if ( (int) $countsql ){
			$this->numRowsAll = $countsql;

		}else {
			$csql = new SelectSQL($countsql);
			$this->numRowsAll = $csql->fetchSingle();
			$csql = NULL;

		}

		$this->totalPage = $this->numRowsAll / $this->rowsPerPage;

		if((int)$this->totalPage < $this->totalPage) {
			$this->totalPage = (int)$this->totalPage + 1;
		}

		$this->thisPage = $thisPage >= 1 && $thisPage <= $this->totalPage ? $thisPage : 1;

		$this->prevPage = $this->thisPage > 0                ? $this->thisPage - 1 : 0;
		$this->nextPage = $this->thisPage < $this->totalPage ? $this->thisPage + 1 : 0;

		if ( ! is_array($sql) )
			$sql = array( $sql );

		if ( count($sql) ){
			for($i = 0; $i < count($sql); $i++){
				$singlesql = $sql[$i];

				$singlesql = sprintf($singlesql, ($this->thisPage - 1) * $this->rowsPerPage, $this->rowsPerPage);

				// dont work if there is % in the
				// sql statement; example: LIKE 'B%'
				// %% = %
				$singlesql = str_replace("(_)", "%", $singlesql);

				$sql[$i] = $singlesql;
			}
		}

		//print_r($sql);

		parent::SelectSQL($sql, true);
	}