Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Zmiana liczby produktow w koszyku
dolar
post
Post #1





Grupa: Zarejestrowani
Postów: 92
Pomógł: 0
Dołączył: 19.10.2016

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


Niestety napotkalam problem. Podczas zmiany ilosci produktu w koszyku nie potrafie przyporzadkowac zmiany do danego produktu i zmieniaja mi sie wszystkie produkty na raz. Oto moj kod:

  1. <form method="post">
  2. <table id="produkty">
  3. <tr>
  4. <th>Id</th>
  5. <th>Kategoria</th>
  6. <th>Nazwa</th>
  7. <th>Rozmiar</th>
  8. <th>Cena</th>
  9. <th>Ilosc</th>
  10. <th>Usuń</th>
  11. <th>Suma</th>
  12. </tr>

  1.  
  2. if (array_key_exists('ilosc', $_POST)){
  3. $numer = (int)$_POST['ilosc'];
  4. } else {
  5. $numer=10;
  6. }
  7.  
  8. if (array_key_exists('minus', $_POST)){
  9. $numer--;
  10. }
  11. if (array_key_exists('plus', $_POST)){
  12. $numer++;
  13. }
  14.  
  15.  
  16.  
  17. foreach ($_SESSION['koszyk'] as $key => $produkt) {
  18. $key=$key+1;
  19. echo "<tr>";
  20. echo "<td>" . $key. "</td>";
  21. echo "<td>" . $produkt['kategoria'] . "</td>";
  22. echo "<td>" . $produkt['nazwa'] . "</td>";
  23. echo "<td>" . $produkt['rozmiar'] . "</td>";
  24. echo "<td>" . $produkt['cena'] . "</td>";
  25. echo '<td>';
  26. echo '<button type="submit" name="minus">-</button>';
  27. echo '<input type="text" name="ilosc" id="ilosc" value='.$numer.'>';
  28. echo '<button type="submit" name="plus">+</button>';
  29. echo "</td>";
  30. echo "<td><button type='submit' name='usun' value='usun'>Usun</button></td>";
  31. echo "</tr>";
  32. }
  33. }
  34.  
  35.  
  36.  


Ktos cos? Bo serio od wczoraj utknelam (IMG:style_emoticons/default/sad.gif)

Ten post edytował dolar 7.12.2016, 16:30:09
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dolar
post
Post #2





Grupa: Zarejestrowani
Postów: 92
Pomógł: 0
Dołączył: 19.10.2016

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


Produkt:

  1.  
  2. <?php
  3.  
  4. $produkty = array(
  5. 'kategoria' => 'bluzki',
  6. 'nazwa' => 'Lewis',
  7. 'rozmiar' => 'xl',
  8. 'cena' => 20.50,
  9. 'ilosc' => ''
  10. ),
  11. 'kategoria' => 'spodnie',
  12. 'nazwa' => 'Lee',
  13. 'rozmiar' => 31,
  14. 'cena' => 99.50
  15. ),
  16. 'kategoria' => 'bluzki',
  17. 'nazwa' => 'Modoo',
  18. 'rozmiar' => 'l',
  19. 'cena' => 29.90
  20. ),
  21. 'kategoria' => 'spodnie',
  22. 'nazwa' => 'Wrangler',
  23. 'rozmiar' => 28,
  24. 'cena' => 100.50
  25. ),
  26. 'kategoria' => 'bluzki',
  27. 'nazwa' => 'Cropp',
  28. 'rozmiar' => 'l/xl',
  29. 'cena' => 29.50
  30. ),
  31. 'kategoria' => 'spodnie',
  32. 'nazwa' => 'BigStar',
  33. 'rozmiar' => 30,
  34. 'cena' => 89.50
  35. )
  36. );
  37.  
  38. if (array_key_exists('wybrany_produkt', $_POST)){
  39. $idProduktu=$_POST['wybrany_produkt'];
  40. $_SESSION['koszyk'][] = $produkty[$idProduktu];
  41. echo '<div id="komunikat">';
  42. echo "Produkt dodano do koszyka!";
  43. echo '</div>';
  44.  
  45. }
  46.  
  47.  
  48.  
  49. ?>
  50.  
  51. <html>
  52. <head>
  53. <meta charset="UTF-8">
  54.  
  55. <style>
  56. #komunikat {
  57. position: absolute;
  58. width: 100%;
  59. text-align: center;
  60. top: 50px;
  61. font-weight: bolder;
  62. color: #8b0000;
  63. }
  64.  
  65. #produkty {
  66. width: 100%;
  67. text-align: center;
  68.  
  69. }
  70.  
  71. #produkty th {
  72. padding: 4px;
  73. background-color: #eee;
  74. }
  75.  
  76. #produkty td {
  77. padding: 4px;
  78. }
  79.  
  80. h1 {
  81. margin-top: 100px;
  82. text-align: center;
  83. }
  84.  
  85. #obrazek {
  86. width: 150px;
  87. position: absolute;
  88. top:0px;
  89. right:0px;
  90.  
  91. }
  92.  
  93. #koszyk {
  94. width: 150px;
  95. height: 80px;
  96. }
  97.  
  98. #liczbaProduktow {
  99. width: 20px;
  100. height: 20px;
  101. text-align: center;
  102. position: absolute;
  103. left:0px;
  104. top:-15px;
  105. font-weight: bolder;
  106. }
  107.  
  108.  
  109.  
  110. </style>
  111. </head>
  112. <body>
  113.  
  114. <div id="obrazek">
  115. <a href=""><img src="""/></a>
  116. <p id="liczbaProduktow">
  117. <?php
  118. if (array_key_exists('koszyk', $_SESSION)){
  119. $i=count($_SESSION['koszyk']);
  120. echo $i;
  121. }
  122.  
  123. ?>
  124.  
  125. </p>
  126. </div>
  127.  
  128. <h1>Produkty</h1>
  129. <form method="post">
  130. <table id="produkty">
  131. <tr>
  132. <th>Kategoria</th>
  133. <th>Nazwa</th>
  134. <th>Rozmiar</th>
  135. <th>Cena</th>
  136. <th>Opcje</th>
  137. </tr>
  138. <?php
  139. foreach ($produkty as $key => $produkt) {
  140. echo "<tr>";
  141. echo "<td>" . $produkt['kategoria'] . "</td>";
  142. echo "<td>" . $produkt['nazwa'] . "</td>";
  143. echo "<td>" . $produkt['rozmiar'] . "</td>";
  144. echo "<td>" . $produkt['cena'] . "</td>";
  145. echo '<td><button type="submit" name="wybrany_produkt" value="'.$key.'">Kup</button></td>';
  146. echo "</tr>";
  147. }
  148. ?>
  149. </table>
  150. </form>
  151.  
  152.  
  153. </body>
  154.  
  155. <script>
  156. setTimeout(function(){
  157. document.getElementById('komunikat').style.display = 'none';
  158. }, 2000);
  159. </script>
  160. </html>
  161.  
  162.  



Koszyk

  1.  
  2. <?php
  3.  
  4.  
  5. if (!array_key_exists('koszyk', $_SESSION)) {
  6. $_SESSION['koszyk'] = array();
  7. }
  8.  
  9.  
  10. ?>
  11.  
  12. <html>
  13. <head>
  14. <meta charset="UTF-8">
  15. <style>
  16.  
  17. #produkty {
  18. width: 100%;
  19. text-align: center;
  20.  
  21. }
  22.  
  23. #produkty th {
  24. padding: 4px;
  25. background-color: #eee;
  26. }
  27.  
  28. #produkty td {
  29. padding: 4px;
  30. }
  31.  
  32.  
  33. h1 {
  34. margin-top: 50px;
  35. text-align: center;
  36. }
  37.  
  38. #napisPusty {
  39. width:150px;
  40. margin: auto;
  41. }
  42.  
  43. #ilosc {
  44. width:25px;
  45. text-align: center;
  46. }
  47.  
  48. </style>
  49. </head>
  50. <body>
  51. <h1>Koszyk</h1>
  52.  
  53. <?php
  54.  
  55.  
  56. if (empty($_SESSION['koszyk'])){
  57. echo "<div id='napisPusty'>Twoj koszyk jest pusty</div>";
  58. } else {
  59. ?>
  60. <form method="post">
  61. <table id="produkty">
  62. <tr>
  63. <th>Id</th>
  64. <th>Kategoria</th>
  65. <th>Nazwa</th>
  66. <th>Rozmiar</th>
  67. <th>Cena</th>
  68. <th>Ilosc</th>
  69. <th>Usuń</th>
  70. <th>Suma</th>
  71. </tr>
  72. <?php
  73.  
  74.  
  75. if (array_key_exists('ilosc', $_POST)){
  76. $numer = (int)$_POST['ilosc'];
  77. } else {
  78. $numer=1;
  79. }
  80.  
  81. if (array_key_exists('minus', $_POST) && $numer>0){
  82. $numer--;}
  83. }
  84. if (array_key_exists('plus', $_POST)){
  85. $numer++;
  86. }
  87.  
  88. foreach ($_SESSION['koszyk'] as $key => $produkt) {
  89.  
  90. echo "<tr>";
  91. echo "<td>" . $key. "</td>";
  92. echo "<td>" . $produkt['kategoria'] . "</td>";
  93. echo "<td>" . $produkt['nazwa'] . "</td>";
  94. echo "<td>" . $produkt['rozmiar'] . "</td>";
  95. echo "<td>" . $produkt['cena'] . "</td>";
  96. echo '<td>';
  97. echo '<button type="submit" name="minus" value="'.$key.'">-</button>';
  98. echo '<input type="text" name="ilosc" id="ilosc" value='.$numer.'>';
  99. echo '<button type="submit" name="plus" value="'.$key.'">+</button>';
  100. echo "</td>";
  101. echo "<td><button type='submit' name='usun' value='".$key."'>Usun</button></td>";
  102. echo "</tr>";
  103.  
  104. }
  105.  
  106. if (array_key_exists('usun', $_POST)){
  107. unset($_SESSION['koszyk'][$key]);
  108. }
  109.  
  110.  
  111. ?>
  112.  
  113. </table>
  114.  
  115. </body>
  116. </html>


Z unset ten sam problem (IMG:style_emoticons/default/sad.gif)
Zaczelam kombinowac cos zeby dodac jeszcze do tablicy $produkty jeszcze jedna kategorie 'ilosc' => 0, ale tez nie wiem jak to ugryzc
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 27.12.2025 - 08:24