Пример класса для работы с новостями:
PHP:
скопировать код в буфер обмена
скопировать код в буфер обмена
- <?PHP
- class News
- {
- private $sql;
- private $number;
- private $id;
- private $news_page;
- private $page_number;
- private $query;
- private $quantity_news;
- private $firs_news_page;
- private $quantity_page;
- public $data_news;
- function __construct()
- {
- $this->sql = new SafeMySQL();
- }
- public function News_All()
- {
- $this->data_news = $this->sql->getAll("SELECT * FROM news WHERE category=1 ORDER BY id DESC");
- return $this->data_news;
- }
- public function News_Index($number)
- {
- (int)$this->number = $number;
- $this->data_news = $this->sql->getAll("SELECT * FROM news WHERE category=1 ORDER BY id DESC LIMIT ?i",$this->number);
- return $this->data_news;
- }
- public function News_View($id)
- {
- (int)$this->id = $id;
- {
- Error::add('Ошибка в запросе!');
- return false;
- }
- else
- {
- $this->data_news = $this->sql->getRow("SELECT * FROM news WHERE category=1 AND id=?i LIMIT 1",$this->id);
- if($this->data_news == true)
- {
- return $this->data_news;
- }
- else
- {
- Error::add('Данная новость ещё не была создана!');
- return false;
- }
- }
- }
- public function News_Page($news_page, $page_number)
- {
- (int)$this->news_page = $news_page;
- (int)$this->page_number = $page_number;
- {
- Error::add('Ошибка в запросе!');
- return false;
- }
- $this->query = $this->sql->getRow("SELECT * FROM news WHERE category=1 LIMIT 1");
- if ($this->quantity_news < $this->news_page)
- {
- return false;
- }
- $this->quantity_page = (int)($this->quantity_news / $this->news_page);
- if (($this->quantity_news % $this->news_page) != 0)
- {
- $this->quantity_page++;
- }
- $this->firs_news_page = ($this->page_number -1)* $this->news_page;
- $this->data_news = $this->sql->getAll("SELECT * FROM news WHERE category=1 ORDER BY id DESC LIMIT ?i,?i",$this->firs_news_page, $this->news_page);
- if($this->data_news == true)
- {
- return $this->data_news;
- }
- else
- {
- Error::add('Данная страница не существует!');
- return false;
- }
- }
- }
- ?>