Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP] Zmienne w Smarty przy użyciu pętli while otrzymują złe wartości
Darekxp
post 31.05.2012, 23:29:22
Post #1





Grupa: Zarejestrowani
Postów: 117
Pomógł: 0
Dołączył: 13.05.2007

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


Witam! Tworzę wykres statystyk, przy użyciu Google Chart. Problem pojawił się, podczas pobierania rekordów z bazy z wykorzystaniem Smarty. Zmienne all i unique w szablonie otrzymują, jakby wartość z ostatniej pozycji pętli. Sprawdziłem podobny kod na samym php, bez Smarty i jest ok. Czy w Smarty potrzebuję np pętli w pętli, żeby zmienne all i unique miały prawidłowe wartości? Jak to rozwiązać? Za pomoc z góry dziękuję.

Działający kod php, bez Smarty:

  1. <?php
  2. $queryStats2 = mysql_query(' SELECT DISTINCT advertisingStatsDate FROM `advertising_stats` WHERE advertisingStatsAdvertisingId="2" ORDER by advertisingStatsDate ASC ');
  3. while($row2 = mysql_fetch_array($queryStats2))
  4. {
  5. $all = mysql_num_rows(mysql_query('SELECT DISTINCT advertisingStatsId FROM `advertising_stats` WHERE advertisingStatsDate="'.$row2['advertisingStatsDate'].'" '));
  6. $unique = mysql_num_rows(mysql_query('SELECT DISTINCT advertisingStatsIP FROM `advertising_stats` WHERE advertisingStatsDate="'.$row2['advertisingStatsDate'].'" '));
  7.  
  8. echo "['".$row2['advertisingStatsDate']."', $all, $unique], ";
  9. }
  10. ?>


Kod php z wykorzystaniem Smarty:

  1.  
  2. <?php
  3. $queryStats2 = mysql_query(' SELECT DISTINCT advertisingStatsDate FROM `advertising_stats` WHERE advertisingStatsAdvertisingId="'.$number.'" ORDER by advertisingStatsDate ASC ');
  4. while($row2 = mysql_fetch_array($queryStats2))
  5. {
  6. $stats2[] = $row2;
  7.  
  8. $all = mysql_num_rows(mysql_query('SELECT DISTINCT advertisingStatsId FROM `advertising_stats` WHERE advertisingStatsDate="'.$row2['advertisingStatsDate'].'" '));
  9. $unique = mysql_num_rows(mysql_query('SELECT DISTINCT advertisingStatsIP FROM `advertising_stats` WHERE advertisingStatsDate="'.$row2['advertisingStatsDate'].'" '));
  10.  
  11. $smarty -> assign('all', $all);
  12. $smarty -> assign('unique', $unique);
  13. }
  14.  
  15. $smarty -> assign('stats2', $stats2);
  16. ?>


Wycinek z szablonu:

  1. {section name="row2" loop="$stats2"}
  2. ['{$stats2[row2].advertisingStatsDate}', {$all}, {$unique}],
  3. {/section}
Go to the top of the page
+Quote Post
nospor
post 1.06.2012, 07:59:43
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




$smarty -> assign('all', $all);
$smarty -> assign('unique', $unique);
Przecież za każdym razem nadpisujesz zmienne to nic dziwnego ze otrzymujesz ostatni wynik. W czystym PHP ci to działało tylko dlatego, że od razu plułeś te dane na ekran.

Musisz to wszystko zapisać do tablicy, a dopiero tę tablicę przekazać do smarty i w smarty operować na tej tablicy


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

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
Darekxp
post 1.06.2012, 10:49:54
Post #3





Grupa: Zarejestrowani
Postów: 117
Pomógł: 0
Dołączył: 13.05.2007

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


Można prosić o jakiś przykład?
Go to the top of the page
+Quote Post
rocktech.pl
post 1.06.2012, 10:57:16
Post #4





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

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


Witam.

Możesz użyć metody append.

  1. <?php
  2. $smarty -> assign('all', array());
  3.  
  4. $smarty -> assign('unique', array());
  5. $queryStats2 = mysql_query(' SELECT DISTINCT advertisingStatsDate FROM `advertising_stats` WHERE advertisingStatsAdvertisingId="'.$number.'" ORDER by advertisingStatsDate ASC ');
  6.  
  7. while($row2 = mysql_fetch_array($queryStats2))
  8.  
  9. {
  10.  
  11. $stats2[] = $row2;
  12.  
  13.  
  14.  
  15. $all = mysql_num_rows(mysql_query('SELECT DISTINCT advertisingStatsId FROM `advertising_stats` WHERE advertisingStatsDate="'.$row2['advertisingStatsDate'].'" '));
  16.  
  17. $unique = mysql_num_rows(mysql_query('SELECT DISTINCT advertisingStatsIP FROM `advertising_stats` WHERE advertisingStatsDate="'.$row2['advertisingStatsDate'].'" '));
  18.  
  19.  
  20.  
  21. $smarty -> append('all', $all);
  22.  
  23. $smarty -> append('unique', $unique);
  24.  
  25. }
  26.  
  27.  
  28.  
  29. $smarty -> assign('stats2', $stats2);
  30.  
  31. ?>


A w szablonie daj sobie dodatkowo debug.

[SMARTY] pobierz, plaintext
  1. {section name="row2" loop="$stats2"}
  2.  
  3. ['{$stats2[row2].advertisingStatsDate}', {$all}, {$unique}],
  4.  
[SMARTY] pobierz, plaintext


--------------------
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
Darekxp
post 1.06.2012, 11:09:40
Post #5





Grupa: Zarejestrowani
Postów: 117
Pomógł: 0
Dołączył: 13.05.2007

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


Chwilkę, przed Twoim postem zrobiłem to w taki sposób, jak poniżej i jest ok. Pytanie, który sposób jest poprawniejszy/wydajniejszy ?

  1. {section name="row2" loop="$stats2"}
  2. ['{$stats2[row2].advertisingStatsDate}', {$all[$stats2[row2].advertisingStatsDate]}, {$unique[$stats2[row2].advertisingStatsDate]}],
  3. {/section}


  1. $queryStats2 = mysql_query(' SELECT DISTINCT advertisingStatsDate FROM `advertising_stats` WHERE advertisingStatsAdvertisingId="'.$number.'" ORDER by advertisingStatsDate ASC ');
  2. while($row2 = mysql_fetch_array($queryStats2))
  3. {
  4. $stats2[] = $row2;
  5.  
  6. $all[$row2['advertisingStatsDate']] = mysql_num_rows(mysql_query('SELECT DISTINCT advertisingStatsId FROM `advertising_stats` WHERE advertisingStatsDate="'.$row2['advertisingStatsDate'].'" '));
  7. $smarty -> assign('all', $all);
  8.  
  9. $unique[$row2['advertisingStatsDate']] = mysql_num_rows(mysql_query('SELECT DISTINCT advertisingStatsIP FROM `advertising_stats` WHERE advertisingStatsDate="'.$row2['advertisingStatsDate'].'" '));
  10. $smarty -> assign('unique', $unique);
  11. }
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: 19.07.2025 - 04:38