Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Potrzebuje małej pomocy z klasami
Forum PHP.pl > Forum > PHP
Raven1122
Witam,

Mam takie klasy:

  1. <?php
  2. require_once("config.php");
  3. class mysql{
  4.  
  5. public function __construct(){
  6. $db = new PDO('mysql:host='.HOST.';dbname='.DB.'', USERNAME, PASSWORD);
  7. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  8. }
  9.  
  10. }
  11.  
  12. class sb extends mysql{
  13.  
  14. public function shout($author, $shout){
  15. $this->author = $author;
  16. $this->shout = $shout;
  17. $this->data = date("Y-m-d H:i:s");
  18. $this->olddata = date("Y-m-d H:i:s", time()-7*24*60);
  19.  
  20. //$stmt = $db->prepare("DELETE FROM isb_shouts WHERE sdate < ?");
  21. //$stmt->bindValue(1, $this->olddata);
  22. //$stmt->execute();
  23.  
  24. $stmt = $db->prepare("INSERT INTO isb_shouts(sid, author, sdate, shout) VALUES(?, ?, ?, ?, ?)");
  25. $stmt->bindValue(1, '');
  26. $stmt->bindValue(2, $this->author);
  27. $stmt->bindValue(3, $this->data);
  28. $stmt->bindValue(4, $this->author);
  29. $stmt->execute();
  30. }
  31.  
  32. public function fetchshouts(){
  33. $stmt = $db->prepare("SELECT * FROM isb_shouts");
  34. $stmt->execute();
  35. while($row = $stmt->fetch()){
  36. if(EMOTICONS == 1){
  37. $emot = $db->prepare("SELECT * FROM isb_emoticons");
  38. $emot->execute();
  39.  
  40. while($emots = $emot->fetch()){
  41. $emot_sign = $emots['emoticon_sign'];
  42. $emot_image = "<img src='".$emots['emoticon_image']."' />";
  43. str_replace($emot_sign, $emot_image, $row['shout']);
  44. }
  45. }
  46. if(SBCODE == 1){
  47. $sbcode = $db->prepare("SELECT * FROM isb_sbcode");
  48. $sbcode->execute();
  49.  
  50. while($code = $sbcode->fetch()){
  51. $code_sign = $sbcode['code'];
  52. $code_html = $sbcode['codehtml'];
  53. str_replace($code_sign, $code_html, $row['shout']);
  54. }
  55. }
  56. if(CENSURE == 1){
  57. $censure = $db->prepare("SELECT * FROM isb_censore");
  58. $censure->execute();
  59.  
  60. while($word = $censure->fetch()){
  61. $word_tocensore = $word['word'];
  62. $word_replecement = $word['replacement'];
  63. str_replace($word_tocensore, $word_replecement, $row['shout']);
  64. }
  65. }
  66. }
  67. return $row['author'];
  68. return $row['sdate'];
  69. return $row['shout'];
  70. }
  71.  
  72. }
  73.  
  74.  
  75. ?>


I teraz takie pytanie:
Jak wykorzystać $db w klasie dziedziczącej po mysql?? mam na myśli sb
sajegib
Skoro nie masz konstruktora w dziedziczącej klasie to zadziałał konstruktor z klasy nadrzędnej. Mogę się mylić, bo nie sprawdzę tego teraz, ale:

  1. $stmt = $db->prepare


zamień na

  1. $stmt = $this->db->prepare


I powinno grać
Raven1122
Nie działa ;(
CuteOne
  1. <?php
  2. require_once("config.php");
  3. class mysql{
  4.  
  5. protected $db;
  6.  
  7. public function __construct(){
  8. $this->db = new PDO('mysql:host='.HOST.';dbname='.DB.'', USERNAME, PASSWORD);
  9. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. }
  11.  
  12. }
  13.  
  14.  
  15. class sb extends mysql{
  16.  
  17. public function shout($author, $shout){
  18.  
  19. $this->db->funkcja();
  20. }
  21. }
Raven1122
Dalej niestety nie działa
sajegib
  1. $this->db = new PDO('mysql:host='.HOST.';dbname='.DB.'', USERNAME, PASSWORD);


A tutaj zmieniłeś wartości na właściwe?
Jakiś błąd wywala?
Raven1122
Rozwiązałem problem, ale pojawił się następny. Mianowicie ręcznie rekord mogę dodać, ale już przez AJAX się nie dodaje
CuteOne
czy my wyglądamy na wróżki?
1. poczytaj o debugowaniu zmiennych np. var_dump($zmienna)
2. jeżeli coś nie działa przez ajaxa to za pomocą dragonfly (opera) lub narzędzi programistycznych (chrome) zbadaj co zwraca
3. i najważniejsze.... podawaj kod
cudny
Cytat(CuteOne @ 29.01.2013, 21:06:46 ) *
  1. <?php
  2. require_once("config.php");
  3. class mysql{
  4.  
  5. protected $db;
  6.  
  7. public function __construct(){
  8. $this->db = new PDO('mysql:host='.HOST.';dbname='.DB.'', USERNAME, PASSWORD);
  9. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. }
  11.  
  12. }
  13.  
  14.  
  15. class sb extends mysql{
  16.  
  17. public function shout($author, $shout){
  18.  
  19. $this->db->funkcja();
  20. }
  21. }


Zabrakło wywołania konstruktora klasy nadrzędnej.
Poniższe powinno działać:

  1. <?php
  2. require_once("config.php");
  3. class mysql{
  4.  
  5. protected $db;
  6.  
  7. public function __construct(){
  8. $this->db = new PDO('mysql:host='.HOST.';dbname='.DB.'', USERNAME, PASSWORD);
  9. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. }
  11.  
  12. }
  13.  
  14.  
  15. class sb extends mysql{
  16. public function __construct(){
  17. parent::__construct();
  18. }
  19.  
  20. public function shout($author, $shout){
  21.  
  22. $this->db->funkcja();
  23. }
  24. }
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.