Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Klasa: koszyk, prosba o spojrzenie doswiadczonym okiem
phpion
post
Post #1





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Chcialbym dac wam pod ostrzal moja druga klasunie. Tym razem ma to byc koszyk zakupow.
Dane koszyka trzymane sa w tablicy z $_SESSION['koszyk'][ID_ELEMENTU] a wartosc poszczegolnej komorki okresla ZAMOWIONA_ILOSC. Np.
  1. <?php
  2. $_SESSION['koszyk'][1] = 20;
  3. $_SESSION['koszyk'][2] = 10;
  4. ?>

Nie wiedzialem za bardzo jak rozwiazac kwestie pobierania danych o towarach bedacych w koszyku wiec getData() i polaczenie ze SQL w niej jest tymczasowe tongue.gif.
Oto moja klasa:
  1. <?php
  2. class Basket
  3. {
  4. public $basket;
  5. public $sum;
  6. public $countItems;
  7. public $basketHandler;
  8.  
  9. public function Basket()
  10. {
  11. $this->__construct();
  12. }
  13.  
  14. public function __construct()
  15. {
  16. $this->basket = Array();
  17. $this->basketHandler = &$_SESSION['koszyk'];
  18. }
  19.  
  20. public function addItem($itemID, $itemAmount)
  21. {
  22. if (isset($this->basketHandler[$itemID]))
  23. $this->basketHandler[$itemID] += $itemAmount;
  24. else
  25. $this->basketHandler[$itemID] = $itemAmount;
  26. }
  27.  
  28. public function getData()
  29. {
  30. $arrayTemp = Array();
  31. $this->sum = 0;
  32.  
  33. if (count($this->basketHandler) > 0)
  34. foreach ($this->basketHandler as $index => $value)
  35. $arrayTemp[] = $index;
  36.  
  37. if (count($arrayTemp) > 0)
  38. {
  39. $sql = mysql_connect("localhost", "root", "");
  40. mysql_select_db("sklep", $sql);
  41.  
  42. $query = "SELECT id, nazwa, cena, vat FROM produkty WHERE id IN (".implode(", ", $arrayTemp).")";
  43. $query = mysql_query($query);
  44.  
  45. if (mysql_num_rows($query) > 0)
  46. {
  47. $i = 0;
  48.  
  49. while ($record = mysql_fetch_row($query))
  50. {
  51. $this->basket[$i]['id'] = $record[0];
  52. $this->basket[$i]['name'] = $record[1];
  53. $this->basket[$i]['amount'] = $this->basketHandler[$record[0]];
  54. $this->basket[$i]['vat'] = $record[3];
  55. $this->basket[$i]['netPrice'] = number_format($record[2], 2);
  56.  
  57. $this->basket[$i]['grossPrice'] = number_format(($this->basket[$i]['netPrice'] + $this->basket[$i]['netPrice'] * $this->basket[$i]['vat'] / 100), 2);
  58.  
  59. $this->basket[$i]['netValue'] = number_format(($this->basket[$i]['netPrice'] * $this->basket[$i]['amount']), 2);
  60.  
  61. $this->basket[$i]['grossValue'] = number_format(($this->basket[$i]['grossPrice'] * $this->basket[$i]['amount']), 2);
  62.  
  63. $this->sum += $this->basket[$i]['grossValue'];
  64.  
  65. $i++;
  66. }
  67.  
  68. $this->countItems = $i;
  69. }
  70. }
  71. }
  72.  
  73. public function showBasket()
  74. {
  75. if ($this->countItems() > 0)
  76. {
  77. $return = "<table border="1">n";
  78. $return .= "<tr><td>Nazwa:</td><td>Cena netto:</td><td>VAT:</td><td>Cena brutto:</td><td>Ilosc:</td><td>Wartosc netto:</td><td>Wartosc brutto:</td></tr>n";
  79.  
  80. foreach ($this->basket as $index => $value)
  81. $return .= "<tr><td>".$this->basket[$index]['name']."</td><td>".$this->basket[$index]['netPrice']."</td><td>".$this->basket[$index]['vat']."</td><td>".$this->basket[$index]['grossPrice']."</td><td>".$this->basket[$index]['amount']."</td><td>".$this->basket[$index]['netValue']."</td><td>".$this->basket[$index]['grossValue']."</td></tr>n";
  82.  
  83. $return .= "<tr><td colspan="6" style="text-align: right;">Do zaplaty:</td><td>".$this->sum."</td></tr>n";
  84. $return .= "</table>";
  85.  
  86. return $return;
  87. }
  88. }
  89.  
  90. public function countItems()
  91. {
  92. $return = (isset($this->countItems)) ? $this->countItems : count($this->basketHandler);
  93.  
  94. return $return;
  95. }
  96.  
  97. public function getSum()
  98. {
  99. if (!isset($this->sum))
  100. $this->getData();
  101.  
  102. return $this->sum;
  103. }
  104.  
  105. public function emptyBasket()
  106. {
  107. foreach ($this->basketHandler as $index => $value)
  108. unset($this->basketHandler[$index]);
  109. }
  110. }
  111. ?>

Prosze o porady i sugestie co w niej zmienic, co dopisac itp. Z gory serdeczne dzieki!
Go to the top of the page
+Quote Post

Posty w temacie


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: 21.08.2025 - 05:52