Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP] obliczenie sumy
egdstudio
post 20.04.2011, 07:27:33
Post #1





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 18.01.2009

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


witam

mam maly problem z uzyskaniem sumy w ponizszej funkcji. w obrebie funkcji nie moge otrzymac wyniku z funkcji suma czy moze jest inny sposob na otrzymanie tego wyniku?

  1. function tep_draw_radio_menu($name, $values, $default = '', $parameters = '', $required = false) {
  2. $field ='<table border="0" cellspacing="0" cellpadding="0"><tr><td class="main">';
  3. if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);
  4.  
  5. for ($i=0, $n=sizeof($values); $i<$n; $i++) {
  6. $value = tep_output_string($values[$i]['id']);
  7. $field .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
  8. if ($i == 0) $field .= ' checked';
  9.  
  10. $field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;', '\'' => ''', '<' => '&lt;', '>' => '&gt;')) . ' razem: ';
  11. $subject= $name;
  12. $patern='#[0-9]+#i';
  13. preg_match($patern, $subject, $matches);
  14. $wartosc=tep_db_query("select * from products_attributes where products_id='".$matches[0]."' and options_values_id='".$value."'");
  15. while($wartosc_atr=tep_db_fetch_array($wartosc)){
  16. $field.=''.number_format($wartosc_atr['options_values_price'],2).''.$wartosc_atr['price_prefix'].'';
  17. }
  18. $product_info_query1 = tep_db_query("select p.products_id, pd.products_name, pd.products_description, p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price, p.products_tax_class_id, p.products_date_added, p.products_date_available, p.manufacturers_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_id = '" . $matches[0] . "' and pd.products_id = p.products_id and pd.language_id = '4'");
  19.  
  20. while($product_info1 = tep_db_fetch_array($product_info_query1)){
  21. $field.=''.number_format($product_info1['products_price'],2).'';
  22. }
  23.  
  24.  
  25. $field.= ''. suma($wartosc_atr['options_values_price'],$product_info1['products_price']).'';
  26. }
  27. $field .= '</td></tr></table>';
  28.  
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
ToAr
post 20.04.2011, 07:57:30
Post #2





Grupa: Zarejestrowani
Postów: 49
Pomógł: 18
Dołączył: 7.04.2011
Skąd: Toruń

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


a jak wygląda funkcja suma()?

jesli zwraca zmienną, to spróbuj do funkcji

  1. function tep_draw_radio_menu($name, $values, $default = '', $parameters = '', $required = false) {


dodać ją jako zmienną globalną

  1. function tep_draw_radio_menu($name, $values, $default = '', $parameters = '', $required = false) {
  2. global $zmienna;
Go to the top of the page
+Quote Post
egdstudio
post 20.04.2011, 08:31:04
Post #3





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 18.01.2009

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


zwykla suma
  1. function suma($zmienna1, $zmienna2){
  2. return $zmienna1+$zmienna2;
  3. }
Go to the top of the page
+Quote Post
ToAr
post 20.04.2011, 11:04:43
Post #4





Grupa: Zarejestrowani
Postów: 49
Pomógł: 18
Dołączył: 7.04.2011
Skąd: Toruń

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


a działa, gdy

  1. $field.= ''. suma($wartosc_atr['options_values_price'],$product_info1['products_price']).'';


zamienisz na

  1. $field.= '$wartosc_atr['options_values_price'] + $product_info1['products_price'];
Go to the top of the page
+Quote Post
egdstudio
post 21.04.2011, 08:37:39
Post #5





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 18.01.2009

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


nie, wyswietla tylko dzialanie a nie wynik
Go to the top of the page
+Quote Post
Ghost_78
post 21.04.2011, 08:44:13
Post #6





Grupa: Zarejestrowani
Postów: 222
Pomógł: 34
Dołączył: 3.11.2010

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


a co Ci się wyświetla jak dasz w tamtym miejscu:
  1. echo 'SUMA: '.($wartosc_atr['options_values_price'] + $product_info1['products_price']);


--------------------
Always look on the bright side of life ;-)
Go to the top of the page
+Quote Post
egdstudio
post 21.04.2011, 09:10:09
Post #7





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 18.01.2009

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


  1. suma: 0


Go to the top of the page
+Quote Post
Ghost_78
post 21.04.2011, 09:20:12
Post #8





Grupa: Zarejestrowani
Postów: 222
Pomógł: 34
Dołączył: 3.11.2010

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


Jeżeli dobrze zrozumiałem Twoje zamiary to chcesz wszystko podsumować.
Jak dla mnie to musisz troche przerobic:

  1. while($wartosc_atr=tep_db_fetch_array($wartosc)){
  2. $field.=''.number_format($wartosc_atr['options_values_price'],2).''.$wartosc_atr['price_prefix'].'';
  3. $sumaPrice+=$wartosc_atr['options_values_price'];
  4. }


  1. while($product_info1 = tep_db_fetch_array($product_info_query1)){
  2. $field.=''.number_format($product_info1['products_price'],2).'';
  3. $sumaProdPrice+=$product_info1['products_price'];
  4. }


i dopiero na koncu:
  1. $field.= suma($sumaPrice,$sumaProdPrice);


Jak dla mnie to suma w tym co napisałeś działa dobrze. Problem w tym, że parametry, które przekazujesz zawierają 0.
Jeżeli to co napisałem nie działa tak jak powinno to napisz co dokładnie chcesz zsumować.


--------------------
Always look on the bright side of life ;-)
Go to the top of the page
+Quote Post
egdstudio
post 21.04.2011, 09:50:53
Post #9





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 18.01.2009

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


przed pętlami dołożyłem

  1. $sumaPrice=0; $sumaProdprice=0;


i zaczyna liczyć prawidłowo, bez tego podwaja wynik, ale jest już ok.

Temat do zamknięcia

Dziekuję za pomoc
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: 25.07.2025 - 09:58