Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] Wyswietlanie zwroconego przez funkcje wiersza
Forum PHP.pl > Forum > Przedszkole
Wolfie
Witam,

Napisalem sobie taka klase :

  1. <?php
  2.  
  3. class Table {
  4. public $tableName;
  5. public $db;
  6.  
  7. public function __construct($host, $user, $pass, $dbname) {
  8. $this->connection = mysql_connect($host, $user, $pass);
  9. mysql_select_db($dbname, $this->connection);
  10. }
  11.  
  12.  
  13. function find($id) {
  14. if(!$row = mysql_query("SELECT * FROM $this->tableName WHERE id='$id'")){
  15. throw new Exception('Error finding row');
  16. }
  17. return $row;
  18. }
  19.  
  20. function setTableName($tableName) {
  21. return $this->tableName = $tableName;
  22. }
  23. }
  24.  
  25.  
  26. class Row {
  27. function insert() {
  28. }
  29. function update() {
  30. }
  31. function delete() {
  32. }
  33. }
  34.  
  35. try {
  36. $tabelka = new Table('localhost', 'root', 'wmateusz', 'spam');
  37. $tabelka->setTableName('tokens');
  38. $tabelka->find(2);
  39. }
  40.  
  41. catch(Exception $e){
  42. echo $e->getMessage();
  43. exit();
  44. }


No ale nie wiem jak wyswietlic teraz wiersz ktory zwraca funkcja find().......chodzi mi o jakies sensowne obiektowe rozwiazanie.....

----------------------------------------------------------------------------

no tak, brakuje wrzucenia wynikow zapytania do tabelki np mysql_fetch_assoc.....
motylo
  1. $temp = $tabelka->find(2);
  2. print_r($temp);

Hint: na końcu metody jest return winksmiley.jpg

----

Nie doczytałem ostatnich linijek, przepraszam.

Można dodać własność klasy np:
  1. private $_currentRow;

I na końcu metody find zamiast zwracać, zrobić przypisanie przez
  1. $this->_currentRow = $row;

I dodać metodę
  1. public function row($id=NULL){
  2. if(!is_null($id)){
  3. $this->find($id);
  4. }
  5. return $this->_currentRow;
  6. }
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.