Pokażę proste klasy, które wykorzystuję na codzień:
<?php
class mysql
{
public $query;
public function __construct($config) {
global $config;
$this->connect($config);
$this->query('SET CHARACTER SET latin2');
$this->query('SET collation_connection = latin2_general_ci');
}
private function connect($config){
mysql_connect($config['hostname'], $config['username'], $config['password']);
mysql_select_db($config['database']);
}
public function query($query){
return mysql_query($query);
}
public function to_array($query) {
return mysql_fetch_array($query);
}
public function rows($query){
return mysql_numrows($query);
}
}
?>
jak widzisz -
teraz np. rozszerzenie klasy mysql:
<?php
class news extends mysql
{
public $id, $tytul, $autor, $time, $tresc, $cat;
public function czytaj($id){
global $config;
$this->id = $id;
$Fields = $this->to_array($this->query('SELECT * FROM `nius` WHERE `id` = '.$id.' LIMIT 1'));
$news = file_get_contents($config['template']."full_news.html");
$news = str_replace("{tytul}", $Fields['temat'], $news);
$news = str_replace("{news}", $Fields['tiny_tresc'].$Fields['tresc'], $news);
$news .= $this->komentarze();
return $news;
}
public function wyswietl_wszystkie(){
global $config;
$query = $this->query('SELECT * FROM `nius` ORDER BY `id` DESC');
while($Fields = $this->to_array($query))
{
$ile = $this->rows($this->query('SELECT `id` FROM `news_comments` WHERE `idnews` = '.$Fields['id'].''));
$news .= file_get_contents($config['template']."news.html");
$news = str_replace("{tytul}", ''.$Fields['temat'].'', $news);
$news = str_replace("{news}", $Fields['tiny_tresc'], $news);
$news = str_replace("{ile}", $ile, $news);
}
return $news;
}
private function komentarze(){
global $config;
$query = $this->query('SELECT * FROM `news_comments` WHERE `idnews` = '.$this->id.' ORDER BY `id` DESC');
while($Fields = $this->to_array($query))
{
$koment .= file_get_contents($config['template']."koment.html");
$koment = str_replace("{tresc}", $Fields['contents'], $koment);
}
return $koment;
}
}
?>[/code]
w funkcji czytaj przypisuję:
następnie wywołuję funkcję komentarze i tam odwołuję się do:
później to zczytuję w jednym pliku (scalam szablon), choć wcale tak nie trzeba robić :mrgreen:
Wiem, że są to prymitywne przykłady, ale chyba od takich najlepiej zaczynać