Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP][MySQL] Przekazanie danych z metody do formularza, [OOP]
jg44
post
Post #1





Grupa: Zarejestrowani
Postów: 122
Pomógł: 14
Dołączył: 14.07.2011
Skąd: Gliwice / Wrocław

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


Witam,
mam taką klasę:
  1. class Comment
  2. {
  3. public $name;
  4. public $message;
  5.  
  6. // (...)
  7.  
  8. public function setName($name)
  9. {
  10. $this->name = $name;
  11. }
  12.  
  13. public function setMessage($message)
  14. {
  15. $this->message = $message;
  16. }
  17.  
  18. public function printComments()
  19. {
  20. $r = mysql_query("SELECT * FROM ksiega WHERE ID='$this->id'")or die(mysql_error());
  21. while($l = mysql_fetch_array($r))
  22. {
  23. $name = $l['nazwa'];
  24. $message = $l['tresc'];
  25. }
  26. }
  27.  
  28. // (...)
  29.  
  30. }

Formularz:
  1. <form action="index.php" method="post">
  2. <input type="text" value="<?php ?>" name="name" /><br />
  3. <textarea cols="40" rows="6" name="message"><?php ?></textarea><br />
  4. <input type="submit" name="wyslany" value="Dodaj" />
  5. </form>

W jaki sposób przekazać zmienne $name i $message z metody printComments() do formularza?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
rocktech.pl
post
Post #2





Grupa: Zarejestrowani
Postów: 587
Pomógł: 131
Dołączył: 8.02.2010

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


Witam.

Edycja , źle zrozumiałem pytanie.

Metoda printComments nic nie zwraca. a powinna zwrócić np. tablicę która potem sobie przelecisz.

  1. $comment = new Comment();
  2. foreach($comment->printComments() as $val) {
  3. echo '<form action="index.php" method="post">
  4. <input type="text" value="'.$val['nazwa'].'" name="name" /><br />
  5. <textarea cols="40" rows="6" name="message">'.$val['tresc'].'</textarea><br />
  6. <input type="submit" name="wyslany" value="Dodaj" />
  7. </form>';
  8. }


Ten post edytował rocktech.pl 7.01.2012, 11:32:50


--------------------
Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo. --Brian Moore

I never go looking for a sucker. I look for a Champion and make a sucker of of him. --Amarillo Slim


Home-made : js-gui-classes | Accordion | Tabs | Carousel / php-sms-classes | Obsługa bramki SMS MultiInfo | Obsługa bramki SMS Mobiltek
Go to the top of the page
+Quote Post
jg44
post
Post #3





Grupa: Zarejestrowani
Postów: 122
Pomógł: 14
Dołączył: 14.07.2011
Skąd: Gliwice / Wrocław

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


Cytat(rocktech.pl @ 7.01.2012, 11:18:53 ) *
Metoda printComments nic nie zwraca. a powinna zwrócić np. tablicę która potem sobie przelecisz.

Też racja. smile.gif
EDIT: zrobiłem teraz coś takiego:
  1. public function printComments()
  2. {
  3. $tablica = array();
  4. $r = mysql_query("SELECT nazwa,tresc FROM ksiega WHERE ID=3")or die(mysql_error());
  5. while($l = mysql_fetch_array($r))
  6. {
  7. $tablica[] = $l['nazwa'];
  8. $tablica[] = $l['tresc'];
  9. }
  10. //print_r($tablica);
  11. return $tablica;
  12. }

Formularz:
  1. foreach($entry->printComments() as $indeks)
  2. {
  3. echo '<form action="index.php" method="post">
  4. <input type="text" value="'.$indeks['nazwa'].'" name="name" /><br />
  5. <textarea cols="40" rows="6" name="message">'.$indeks['tresc'].'</textarea><br />
  6. <input type="submit" name="wyslany" value="Dodaj" />
  7. </form>';
  8. }

To nie działa - drukowane są dwa formularze - pierwszy z wartościami z tabeli o ID=1, drugi - o ID=2, dla zapytań z ID>3 zwraca to samo, co dla ID=2. Co namieszałem?

EDIT: już sobie poradziłem, tak to należało zrobić:
  1. public function printComments()
  2. {
  3. $tablica = array();
  4. $r = mysql_query("SELECT nazwa,tresc FROM ksiega WHERE ID=7")or die(mysql_error());
  5. while($l = mysql_fetch_array($r))
  6. {
  7. $tablica[] = $l['nazwa'];
  8. $tablica[] = $l['tresc'];
  9. }
  10. return $tablica;
  11. }

Formularz:
  1. <?
  2. include_once("model/Model.php");
  3. $entry = new Model();
  4. $tab = $entry->printComments();
  5. ?>
  6. <form action="index.php" method="post">
  7. <input type="text" value="<? echo $tab[0]; ?>" name="name" /><br />
  8. <textarea cols="40" rows="6" name="message"><? echo $tab[1]; ?></textarea><br />
  9. <input type="submit" name="wyslany" value="Dodaj" />
  10. </form>


Ten post edytował jg44 7.01.2012, 16:34:53
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 20.08.2025 - 05:06