Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> klasa i zmienne, pytanie
Memberek
post
Post #1





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 1.08.2011

Ostrzeżenie: (0%)
-----


Witam, ostatnio się uczyłem klas, w miarę ogarniam i teraz potrzebuje pomocy, ponieważ nie mogę dojść do tego, jak tworzyć zmienne automatycznie, abym mógł je pobrać.

mam np.
  1. $cos = new cos;
  2. $arr = array(
  3. "Kot" => "perski",
  4. "Pies"=> "bullterier",
  5. );
  6. $cos->tablica($arr);
  7. echo $cos->Pies; // zwraca bullterier
  8.  
  9. class cos {
  10.  
  11. public function tablica ($array) {
  12. foreach($array as $index => $wartosc) {
  13. // jak tutaj?
  14. }
  15.  
  16. }
  17.  
  18. }

mam nadzieje, że zrozumiecie o co chodzi i z góry dziękuję za pomoc, lub chociaż próbę pomocy!
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Memberek
post
Post #2





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 1.08.2011

Ostrzeżenie: (0%)
-----


  1. <?PHP
  2. class MySQL {
  3.  
  4. private $config,$connection,$lQuery;
  5.  
  6. public function __construct() {
  7. require_once("modules/Config.php");
  8. $this->config = new Config;
  9. $this->connection = MySQL_Connect($this->config->value("dbHost"), $this->config->value("dbUser"), $this->config->value("dbPass")) or die("Cannot connect to MySQL");
  10. MySQL_Select_DB($this->config->value("dbName"));
  11. }
  12.  
  13. public function __destruct(){
  14. if($this->connection) MySQL_Close($this->connection);
  15. }
  16.  
  17. public function query($query){
  18. return $this->lQuery = MySQL_Query($query);
  19. }
  20.  
  21. public function numRows($query = null) {
  22. if($query == null && !$this->lQuery) die ("No MySQL Query in MySQL::numRows()");
  23. return MySQL_Num_Rows(($query == null)?$this->lQuery:$query);
  24. }
  25.  
  26. public function fetch($query = null) {
  27. if($query == null && !$this->lQuery) die ("No MySQL Query in MySQL::fetch()");
  28. foreach(MySQL_Fetch_Array(($query == null)?$this->lQuery:$query) as $index => $value){
  29. $this->{$index} = $value;
  30. }
  31. }
  32.  
  33. public function getQuery(){
  34. return $this->lQuery;
  35. }
  36.  
  37. public function insert($table=null, $data=array()){
  38. if($table == null || gettype($data) != "array") die("Wrong param in MySQL::insert(table name, array data)");
  39. $keys = ""; $values = ""; $c=false;
  40. foreach($data as $key => $value){
  41. $keys.= (($c==true)?", ":"")."`$key`";
  42. $values.=(($c==true)?", ":"")."'$value'";
  43. if($c==false) $c=true;
  44. }
  45. return $this->query("INSERT INTO `$table` ($keys) VALUES ($values)");
  46. }
  47.  
  48. public function update($table=null, $data=array(), $param=null){
  49. if($table == null || gettype($data) != "array") die("Wrong param in MySQL::update(table name, array data)");
  50. $d = ""; $c=false;
  51. foreach($data as $key => $value){
  52. $d.= (($c==true)?", ":"")."`$key`='$value'";
  53. if($c==false) $c=true;
  54. }
  55. return $this->query("UPDATE `$table` SET $d $param");
  56. }
  57.  
  58. }
  59. ?>

tu jest moja klasa

Ten post edytował Memberek 1.08.2011, 19:04:13
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 30.12.2025 - 00:38