Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [ZF] Sprawdzenie czy użytkownik głosował
razielnr1
post
Post #1





Grupa: Zarejestrowani
Postów: 10
Pomógł: 0
Dołączył: 9.04.2013

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


Sprawdzam czy użytkownik już głosował, jeśli nie - to może zagłosować ; jeśli tak - nie może oddać głosu i pojawia się komunikat, że już oddał głos.
Co prawda nie może oddać już głosu jeśli głosował, lecz nie pojawia się komunikat i nie wiem co jest źle w warunku.

Akcja:


  1. public function voteAction()
  2. {
  3.  
  4. $auth = Zend_Auth::getInstance();
  5. $identity = $auth->getIdentity();
  6.  
  7.  
  8. if ($identity) {
  9.  
  10. $votes = new Application_Model_DbTable_Votes();
  11.  
  12. $answer_id = $this->getRequest()->getParam('answer');
  13. $identity->users_id;
  14.  
  15.  
  16. $vote = $votes->fetchRow($votes->select()
  17. ->where('answer = ?', $answer_id));
  18.  
  19.  
  20. $answers = new Application_Model_DbTable_Answers();
  21.  
  22. $mark = $this->getRequest()->getParam('mark');
  23.  
  24.  
  25. if (!$vote) {
  26.  
  27. $answer = $answers->find($answer_id);
  28.  
  29. if ($answer->count()) {
  30.  
  31. $answer = $answer->current();
  32.  
  33. if ($mark == 'plus') {
  34. ++$answer->mark;
  35. $votes->createRow(array(
  36. 'answer' => $answer_id,
  37. 'author' => $identity->users_id
  38. ))->save();
  39. }
  40. else {
  41. --$answer->mark;
  42. $votes->createRow(array(
  43. 'answer' => $answer_id,
  44. 'author' => $identity->users_id
  45. ))->save();
  46. }
  47. $answer->save();
  48. }
  49. }
  50.  
  51. $this->view->vote = $vote;
  52.  
  53. }
  54.  
  55. $session = new Zend_Session_Namespace;
  56.  
  57. $this->_helper->redirector->gotoUrl($session->redirect);
  58.  
  59. }



Widok:

  1. <?php if ($this->identity): ?>
  2. <?php if ($this->identity->users_id != $answer['author']): ?>
  3. <?php if ($this->identity->users_id != $this->vote['author'] && $this->vote['answer'] == NULL): ?>
  4. <a href="<?php echo $this->url(array('action' => 'vote', 'controller' => 'index', 'answer' => $answer['answers_id'], 'mark' => 'plus')); ?>">
  5. <i style="margin-right: 5px" class="icon-thumbs-up"></i>
  6. <span style="color: green;">Dobra odpowiedź</span>
  7. </a>
  8. &nbsp&nbsp&nbsp&nbsp
  9. <a href="<?php echo $this->url(array('action' => 'vote', 'controller' => 'index', 'answer' => $answer['answers_id'], 'mark' => 'minus')); ?>">
  10. <i style="margin-right: 5px" class="icon-thumbs-down"></i>
  11. <span style="color: red;">Zła odpowiedź</span>
  12. </a>
  13. <?php elseif ($this->identity->users_id == $this->vote['author'] && $this->vote['answer'] != NULL): ?>
  14. Oddałeś
  15. <?php endif; ?>
  16. <?php endif; ?>
  17. &nbsp&nbsp&nbsp&nbsp
  18. <span style="font-weight: bold;">
  19. Średnia ocen <?php echo '(' . $answer['mark'] . ')' . '&nbsp&nbsp&nbsp&nbsp'; ?>
  20. </span>
  21. <?php endif; ?>
  22.  
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
kamilus
post
Post #2





Grupa: Zarejestrowani
Postów: 17
Pomógł: 1
Dołączył: 18.09.2004
Skąd: Tczew

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


To nie ma prawa zadziałać.
  1. <?php if ($this->identity->users_id != $this->vote['author'] && $this->vote['answer'] == NULL): ?>

Jeżeli dopiero głosowałeś to obiekt $this->vote w widoku jest pusty.

fetchRow zwraca obiekt, a nie tablicę.
Ogólnie nie jestem pewny linii 2 w widoku. Brakuje tutaj trochę logicznego myślenia.
Sprawdź czy został już oddany głos:

  1. $vote = $votes->fetchRow($votes->select()
  2. ->where('answer = ?', $answer_id));

zamień na:
  1. $vote = $votes->fetchRow($votes->select()
  2. ->where('answer = ?', $answer_id)->where('author=?',$identity->users_id);


Wówczas pierwszym warunkiem w widoku powinno być sprawdzenie, czy $this->vote jest obiektem (jeżeli tak to oddano głos).

dalej:
  1. $votes->createRow(array( ...

powinno być
  1. $vote = $votes->createRow(array( ...


  1. $this->view->vote = $vote;


Przenieś do warunku dla !$vote (wewnątrz ifa - przed zamykający } ).

I teraz w widoku możesz dać:

  1. <? if($this->vote) {
  2. // .. odano głos
  3. } else if ($this->vote->answer == NULL) {
  4. // ... nie oddałeś i tutaj spis opcji
  5. }

Go to the top of the page
+Quote Post
razielnr1
post
Post #3





Grupa: Zarejestrowani
Postów: 10
Pomógł: 0
Dołączył: 9.04.2013

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


Super fachowa pomoc. Dziękuję!
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: 19.08.2025 - 10:56