Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php/mysql] kilka klas...
Forum PHP.pl > Forum > PHP
pionas
Witam,

dotychczas moja praca w php opierała się na funkcjach, chciałbym jednak poszerzyć trochę swoją wiedzę i umiejętności i mam kilka pytań.
Otóż ciekaw jestem jak działają klasy, tzn. interesuje mnie:
- klasa sesji
- klasa (auto)logowania z ustalaniem dostępu do poszczególnych podstron (jedno logowanie dla userów i admina)
- klasa newsów

Czyli klasy do prostego cms'a, jestem ciekaw jak to wszystko działa.

Macie może jakieś linki gdzie mógłbym znaleźć coś na ten temat?

Dziękuję i pozdrawiam
piotr94
klasa to pewien zbiór metod (czyli funkcji) oraz właściwości (czyli zmiennych)
klasę tworzysz tak:
  1. class HelloWord{
  2. public $imie; //zmienna publiczna
  3. public function SayHello(){//funkcja publiczna
  4. echo('Hello '.$this->imie);//tak pobieramy wartość zmiennej klasy
  5. }
  6. }
  7. $HelloObj=new HelloWord();//tworzymy obiekt
  8. $HelloObj->imie='Piotr94';//przypisujemy wartość zmiennej publicznej
  9. $HelloObj->SayHello();//wywołujemy funkcję publiczną

tak w wielkim skrócie o klasach
klasy pisze się samemu, w zależności od potrzeby
używa się ich zamiast funkcji, bo czytelniejsze jest zapisanie np.
  1. $news=new NewsClass('kategoria_1');
  2. $news->Show(10,'last');
  3. $hews->Comments();

niż za pomocom funkcji, czy php mieszanego z html'em
poza tym kod masz bardziej uporządkowany ;-)
zapraszam do manuala php: class
piotr94
1. session_start powinno być na samym początku
2. jeśli chcesz się bawić z klasami zacznij od czegoś prostego. Polecam książkę Zaawansowane Programowanie w PHP5 wyd. Helion
pionas
coś tam prostego to może i napiszę winksmiley.jpg interesuje mnie właśnie to co potrzebuje do napisania stronki winksmiley.jpg

w skrypcie chciałbym aby sesje opierały się na bazie danych winksmiley.jpg

[EDIT]
nawet jak dałem na początku session_start() to nie działa zapisywanie do bazy danych...
piotr94
a utworzyłeś bazę danych z odpowiednimi polami??

hmm, poczytaj coś o tej funkcji:
http://pl.php.net/session_set_save_handler
pionas
Tabela wygląda tak:
CREATE TABLE IF NOT EXISTS `sessions` (
`session_id` varchar(32) NOT NULL,
`session_start` int(11) unsigned NOT NULL,
`session_time` int(11) unsigned NOT NULL,
`session_value` text NOT NULL,
PRIMARY KEY (`session_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Wzięta z http://wortal.php.pl/phppl/wortal/artykuly...i/implementacje

Udało mi się to zrobić winksmiley.jpg

Zmieniłem klasę odpowiadającą za połączenie z bazą danych i sesji smile.gif

[EDIT]
chciałem wkleić tu kod ale był jakiś błąd, jakiś bo nie otrzymałem komunikatu co dokładnie jest nie tak winksmiley.jpg

Dzisiaj znalazłem trochę czasu na stworzenie własnej klasy artykułów i mam jedno pytanie, czy tak może być

Oto kod:
  1. class Articles {
  2.  
  3. function Display_Categories() {
  4. $query = mysql_query("SELECT id, title FROM art_categories ORDER BY id ASC");
  5. if(mysql_num_rows($query)>0) {
  6. while($row = mysql_fetch_array($query)) {
  7. $categories[] = $row;
  8. }
  9. return $categories;
  10. }
  11. }
  12.  
  13. function Display_Category($id) {
  14. $query = mysql_query("SELECT id, title, date, short_desc FROM articles WHERE cat=".intval($id)." ORDER BY id DESC");
  15. if(mysql_num_rows($query)>0) {
  16. while($row = mysql_fetch_array($query)) {
  17. $articles[] = $row;
  18. }
  19. return $articles;
  20. }
  21. }
  22.  
  23. function Display_Article($id) {
  24. $query = mysql_query("SELECT id, title, date, long_desc, author FROM articles WHERE id=".intval($id)."");
  25. if(mysql_num_rows($query)>0) {
  26. $row = mysql_fetch_array($query);
  27. $article[] = $row;
  28. return $article;
  29. }
  30. }
  31.  
  32. function Delete_Article($id) {
  33. // sprawdzanie czy uzytkownik ma uprawnienia - to dac tutaj w funkcji czy gdzies na zewnatrz?
  34. if (is_array($_SESSION['auth']) && in_array('articles', $_SESSION['auth'])) {
  35. $query = mysql_query("SELECT id FROM articles WHERE id=".intval($id)."");
  36. if(mysql_num_rows($query)>0) {
  37. mysql_query("DELETE FROM articles WHERE id=".intval($id)."")
  38. return true;
  39. } else {
  40. returne false;
  41. }
  42. } else {
  43. returne false;
  44. }
  45. }
  46.  
  47. function Add_Article($id) {
  48. // sprawdzanie czy uzytkownik ma uprawnienia - to dac tutaj w funkcji czy gdzies na zewnatrz?
  49. if (is_array($_SESSION['auth']) && in_array('articles', $_SESSION['auth'])) {
  50. $approved = 1;
  51. } else {
  52. $approved = 0;
  53. }
  54. $sql = "INSERT INTO articles (title, short_desc, long_desc, author, date, st) VALUES ('{$_POST['title']}', '{$_POST['short_desc']}', '{$_POST['long_desc']}', '{$_POST['author']}', '{$time}', '$approved')";
  55. $query = mysql_query($sql);
  56. return true;
  57. }
  58. }


Cześć,

mam kolejny problem z klasą.
Chodzi o upload obrazków.
Mam taką klasę:
  1. <?php
  2.  
  3. class Upload {
  4. public $dir;
  5. public $typ;
  6. public $file_width;
  7. public $file_height;
  8. public $file_maxwidth;
  9. public $file_maxheight;
  10. public $file_name;
  11. public $file_type;
  12. public $file_size;
  13. public $file_tempname;
  14. public $file_error;
  15. public $errors = array();
  16. public $file_ext;
  17. public $file_exts = array();
  18.  
  19.  
  20. public static function UpFile($file, $dir, $width=150, $height=150, $size=51200) {
  21. $this->dir = $dir;
  22.  
  23. $this->file_exts=array('gif', 'jpg', 'jpeg', 'png', 'bmp');
  24. $this->file_name = $_FILES[$file]['name'];
  25. $this->file_type = strtolower($_FILES[$file]['type']);
  26. $this->file_size = $_FILES[$file]['size'];
  27. $this->file_tempname = $_FILES[$file]['tmp_name'];
  28. $this->file_error = $_FILES[$file]['error'];
  29. $this->file_maxwidth = $width;
  30. $this->file_maxheight = $height;
  31.  
  32. $file_dimensions = @getimagesize($this->file_tempname);
  33. $this->file_width = $file_dimensions[0];
  34. $this->file_height = $file_dimensions[1];
  35.  
  36. if( !is_uploaded_file($this->file_tempname) )
  37. $this->errors[$file] = 'Nie udało się wgrać pliku na serwer';
  38. if( $this->file_size > $size )
  39. $this->errors[$file] = 'Za duży rozmiar pliku';
  40.  
  41. $this->file_ext = strtolower(str_replace(".", "", strrchr($this->file_name, ".")));
  42.  
  43. if( !in_array($this->file_ext, $this->file_exts) )
  44. $this->errors[$file] = 'Zły format pliku';
  45.  
  46. }
  47.  
  48. public static function upload_photo() {
  49. $photo_dest = $this->dir;
  50.  
  51. $width = $this->file_width ;
  52. $height = $this->file_height;
  53. if( $height > $this->file_maxheight )
  54. {
  55. $width = floor($width * $this->file_maxheight / $height);
  56. $height = $this->file_maxheight;
  57. }
  58. if( $width > $this->file_maxwidth )
  59. {
  60. $height = floor($height * $this->file_maxwidth / $width);
  61. $width = $this->file_maxwidth;
  62. }
  63.  
  64.  
  65. // RESIZE IMAGE AND PUT IN USER DIRECTORY
  66. switch($this->file_ext)
  67. {
  68. case "gif":
  69. $file = imagecreatetruecolor($width, $height);
  70. $new = imagecreatefromgif($this->file_tempname);
  71. $kek=imagecolorallocate($file, 255, 255, 255);
  72. imagefill($file,0,0,$kek);
  73. imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);
  74. imagejpeg($file, $photo_dest, 100);
  75. ImageDestroy($new);
  76. ImageDestroy($file);
  77. break;
  78.  
  79. case "bmp":
  80. $file = imagecreatetruecolor($width, $height);
  81. $new = $this->imagecreatefrombmp($this->file_tempname);
  82. for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }
  83. imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);
  84. imagejpeg($file, $photo_dest, 100);
  85. ImageDestroy($new);
  86. ImageDestroy($file);
  87. break;
  88.  
  89. case "jpeg":
  90. case "jpg":
  91. $file = imagecreatetruecolor($width, $height);
  92. $new = imagecreatefromjpeg($this->file_tempname);
  93. for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }
  94. imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);
  95. imagejpeg($file, $photo_dest, 100);
  96. ImageDestroy($new);
  97. ImageDestroy($file);
  98. break;
  99.  
  100. case "png":
  101. $file = imagecreatetruecolor($width, $height);
  102. $new = imagecreatefrompng($this->file_tempname);
  103. for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }
  104. imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);
  105. imagejpeg($file, $photo_dest, 100);
  106. ImageDestroy($new);
  107. ImageDestroy($file);
  108. break;
  109. }
  110.  
  111. chmod($photo_dest, 0777);
  112.  
  113. return true;
  114. }
  115. }
  116. ?>

Dostaję taki komunikat:
Fatal error: Using $this when not in object context in upload.class.php on line 21

Klasę wywołuję tak:
  1. $photo = new Upload();
  2. $file = '/Upload/avatar/'.time().'_'.$_FILES['avatar']['name'];
  3. $photo->UpFile('avatar', $file);
  4.  
  5. if (sizeof($photo->errors) == 0) {
  6. $photo->upload_photo();
  7. }


proszę o pomoc
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.