Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][Zend] Wyswietlanie danych z mysql w Zend_Form
aggreSSive115
post 11.09.2011, 20:01:31
Post #1





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 11.09.2011

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


Witam,

utworzyłem sobie prosty formularz w Zend Frameworku, lecz mam problem z wyświetlaniem w nim (w jego textarea'ch) danych pobieranych z MySQL'a.

Mój formularz wygląda następująco:
Kod
<?php

class Application_Form_EditForm extends Zend_Form
{  

    public function init() {
        
        $this->autor = new Zend_Form_Element_Text('autor');
        $this->autor->setLabel('Autor: ')
              ->setRequired(true);
        
        $this->tytul = new Zend_Form_Element_Text('tytul');
        $this->tytul->setLabel('Tytul: ')
              ->setRequired(true);
        
        $this->tresc = new Zend_Form_Element_Textarea('tresc');
        $this->tresc->setLabel('Tresc: ')
              ->setRequired(true)
              ->setAttrib('cols', '40')
              ->setAttrib('rows', '15');
        
        $this->wyslij = new Zend_Form_Element_Submit('wyslij');
        $this->wyslij->setLabel('Dodaj newsa');
        
        $this->addElements(array($this->autor, $this->tytul, $this->tresc, $this->wyslij));
        $this->setMethod('post');
        $this->setAction('../index/news-edit');
    }
}


Czyli prosty zwyczajny formularz - proszę o pomoc jak mogę pobrane dane z MySQL ustawić jako wyświetlanie w tym formularzu?

Pozdrawiam!
Go to the top of the page
+Quote Post
CuteOne
post 11.09.2011, 20:32:06
Post #2





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


Powiem szczerze, że nigdy nie wstawiałem treści do textarea... może standardowa metoda zadziała
  1. $form = new Application_Form_EditForm();
  2. $form->getElement('name')->setValue($row['name']);
Go to the top of the page
+Quote Post
aggreSSive115
post 12.09.2011, 07:55:32
Post #3





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 11.09.2011

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


Hmmm wykombinowalem cos takiego:

Index Controller:
Kod
    // edycja newsa
    public function newseditAction()
    {        
        // pobieranie newsa o danym ID
        $oNewsEdit = new Application_Model_News();
        $aNewsEdit = $oNewsEdit->getNews($_GET['id']);
        $this->view->aNewsEdit = $aNewsEdit;
        
        // wyswietlanie formularza
        $oNewsForm = new Application_Form_News();        
        $this->view->oNewsForm = $oNewsForm;
        
        // pobranie newsa z mysql i wpisanie do formularza
        $oNewsForm->getElement('autor')->setValue($_POST['autor']);
        $oNewsForm->getElement('tytul')->setValue(2);
        $oNewsForm->getElement('tresc')->setValue(3);        
    }


Model:
Kod
    // pobieranie newsa
    public function getNews($iId) {
        $this->wynik = mysql_query("SELECT * FROM news WHERE id='".mysql_escape_string($iId)."'");

            $aGet = array();

            while($this->row=mysql_fetch_assoc($this->wynik))
                    {
                        $aGet[]=$this->row;
                    }

                    return $aGet;
        }


Lecz ciągle nic nie wyświetla w polach, probowałem wyświetlić poza nimi i wszystko bylo cacy. Problem jest z przekazaniem tego wyniku z modelu do controllera. Mam takie notatki:

Kod
Notice: Undefined property: IndexController::$wiersz in D:\serv\www\systemnewsow-zf\application\controllers\IndexController.php on line 48


Pozdrawiam!

Ten post edytował aggreSSive115 12.09.2011, 11:10:06
Go to the top of the page
+Quote Post
zend
post 12.09.2011, 16:44:22
Post #4





Grupa: Zarejestrowani
Postów: 580
Pomógł: 85
Dołączył: 25.03.2010
Skąd: Skrzyszów :)

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


  1. $form = new Form();
  2. $form -> populate( array( 'nazwa pola' => 'wartość' ) );
  3.  
  4. if($this -> getRequest() -> isPost() && $form -> isValid( $this -> _getAllParams() ))
  5. {
  6. $form -> getValues();
  7. //zapis
  8. }
  9. $this -> view -> form = $form;
  10.  


Najwygodniej jest nazywać kolumny w tabeli tak samo jak pola w formularzu, skorzystaj też z zend_db_table zamiast korzystać z natywnych rozszerzeń z php, znacznie przyspiesza to debugowanie i pisanie sql'i

Ten post edytował zend 12.09.2011, 16:44:52
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 Wersja Lo-Fi Aktualny czas: 18.07.2025 - 02:23