Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP]Lista wielokrotnego wyboru wraz z podaniem ilości sztuk
sauber94
post 7.02.2020, 20:51:23
Post #1





Grupa: Zarejestrowani
Postów: 55
Pomógł: 0
Dołączył: 22.05.2015

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


Cześć.

Posiadam formularz w PHP, w którym chciałbym dodać możliwość wyboru kilku przedmiotów pobieranych z tabeli bazy danych wraz z przypisaniem ilości każdego z nich.
Ma ktoś pomysł jak to zrobić, ewentualnie jakiś przykład, którym mógłbym się oprzeć?
Go to the top of the page
+Quote Post
Neutral
post 8.02.2020, 17:15:21
Post #2





Grupa: Zarejestrowani
Postów: 286
Pomógł: 46
Dołączył: 10.01.2016

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


  1. <form method="get" action="">
  2. <select multiple="multiple" name="nuts[]">
  3. <option value="a" name="nuts[0]">hazel A</option>
  4. <option value="b" name="nuts[1]">hazel B</option>
  5. <option value="c" name="nuts[2]">hazel C</option>
  6. </select>
  7. <select multiple="multiple" name="costs[]">
  8. <option value="a" name="cost_vals[0]">A - 4$</option>
  9. <option value="b" name="cost_vals[1]">B - 2.5$</option>
  10. <option value="c" name="cost_vals[2]">C - 16$</option>
  11. </select>
  12. <input type="hidden" value="4" name="a"/>
  13. <input type="hidden" value="2.5" name="b"/>
  14. <input type="hidden" value="16" name="c"/>
  15. <input type="submit" />
  16. </form>


  1. <?php
  2. // handle data from form
  3.  
  4. $nuts = $_GET['nuts'] ?? [];
  5. $costs = $_GET['costs'] ?? [];
  6. $cost_vals = $_GET['cost_vals'] ?? [];
  7.  
  8. print_r($nuts);
  9. print_r($costs);
  10.  
  11. // if $nuts == $costs ...
  12.  
  13. for($i=0;count($nuts) == count($costs) && $i<count($nuts);$i++) {
  14. if($nuts[$i] == $costs[$i]) {
  15. echo $_GET[$nuts[$i]] . "<br />";
  16. }
  17. }
  18.  
  19. ?>


Cytat
Cześć.

Posiadasz może jakiś inny przykład co do listy wielokrotnego wyboru wraz z podaniem ilości sztuk?

Przykładowo obecnie w formularzu mam wybór tylko jednego materialu, wycinek mojego formularza z tym związany to:
  1. <div class="form-group">
  2. <label class="control-label col-sm-2" for="id_pracownika">Pracownik:</label>
  3. <select name="id_materialu" id="id_materialu" class="form-control">
  4. <?php
  5. foreach($materials as $material){ ?>
  6. <option value="<?=$material['id_materialu']?>"><?=$material['imie']?></option>
  7. <?php } ?>
  8. </select>
  9. </div>

Przydałoby mi się coś w stylu, że jest jedna lista wyboru, wybieram z niej dany materiał i obok wpisuję ilość. Po tym automatycznie pokazuje się niżej puste pole wyboru listy materiału tak, że znowu mogę wybrać kolejny materiał i obok wpisać ilość.

Pozdrawiam.


Proszę odpisywać w wątku, nie we wiadomościach prywatnych.

Kod
const btn = document.querySelector('button');

const vals = [[1,2,45,23],['sdf', 'bc', 'd', 'a']];

function create_el (n_el, attr, what, where) {
  let arr = [];
  for(let i=0;i<n_el;i++) {
    arr.push(document.createElement(what));
    if(attr.length == n_el) {
      arr[i].setAttribute(attr[0], attr[1]+(i+1)+'[]')
    }
    where.appendChild(arr[i]);
  }
  return arr;
}

btn.onclick = function() {
  let arr_n1 = create_el(2, ['name', 'slct'], 'select', document.querySelector('form'));

  let opt, opt2;
  for(let j=0;j<arr_n1.length;j++) {
    for(let i=0;i<vals[0].length;i++) {
      opt1 = document.createElement('option');
      opt1.value = vals[j][i];
      opt1.textContent = vals[j][i];
      arr_n1[j].appendChild(opt1);
    }
  }
};


To jest tylko wzór, nie kod, który byłby napisany starannie. Jeśli zechciałbyś skorzystać z tego kodu, najlepiej abyś go przepisał lub poprawił.

Ten post edytował Neutral 8.02.2020, 17:15:58
Go to the top of the page
+Quote Post
sauber94
post 17.02.2020, 14:23:19
Post #3





Grupa: Zarejestrowani
Postów: 55
Pomógł: 0
Dołączył: 22.05.2015

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


Witam po przerwie smile.gif

Wykombinowałem możliwość dodania materiałów pod określone zlecenie troszkę inaczej. Mianowicie przy każdym stworzonym zleceniu mam możliwość kliknięcia w nie w celu dodania materiałów (nazwałem to sobie materiały przydzielone).

Napotkał mnie jak zwykle problem. Wszystko pięknie działa prócz dodania do bazy pod określone zlecenie materiału. Gdy dodam w bazie (phpMyAdmin) pod określone zlecenie materiały to na stronie www na liście ładnie i się wyświetla. Problem mam więc po stronie kodu, który udostępniam poniżej. Może ktoś podpowie gdzie leży błąd.

  1. <?php
  2.  
  3. class Assigned_Material extends Controller
  4. {
  5. public static function index() {
  6. $resources = new Resources();
  7. Form::post(['id_materialu', 'ilosc'], function () {
  8. $id_materialu = $_POST['id_materialu'];
  9. $ilosc = $_POST['ilosc'];
  10. $id_zlecenia = $_GET['id'];
  11. $r = new Resources();
  12. $r->createAssigned_Materials($id_zlecenia, $id_materialu, $ilosc);
  13. header('Location: /assigned_material?order='.$id.'');
  14. });
  15.  
  16. return [
  17. 'assigned_materials' => $resources->getAssigned_Materials(),
  18. 'materials' => $resources->getMaterials(),
  19. ];
  20. }
  21. }



  1. public function getAssigned_Materials()
  2. {
  3. $response = false;
  4. try {
  5. $pdo = new ConnectionDatabase();
  6. $db = $pdo->connect();
  7. $query = $db->prepare("SELECT * FROM materialy_przydzielone");
  8. $query->execute();
  9. $response = $query->fetchAll(PDO::FETCH_ASSOC);
  10.  
  11. } catch (PDOException $e) {
  12. $response = $e;
  13. }
  14.  
  15. return $response;
  16. }
  17.  
  18.  
  19. public function getAssigned_Material($id)
  20. {
  21. $response = false;
  22. try {
  23. $pdo = new ConnectionDatabase();
  24. $db = $pdo->connect();
  25. $query = $db->prepare("SELECT * FROM materialy_przydzielone WHERE id_materialu = :id");
  26. $query->bindParam(':id', $id);
  27. $query->execute();
  28. $response = $query->fetchAll(PDO::FETCH_ASSOC);
  29.  
  30. } catch (PDOException $e) {
  31. $response = $e;
  32. }
  33. return $response[0];
  34. }
  35.  
  36.  
  37. public function createAssigned_Materials($id_zlecenia, $id_materialu, $ilosc)
  38. {
  39. $response = false;
  40. try {
  41. $pdo = new ConnectionDatabase();
  42. $db = $pdo->connect();
  43. $query = $db->prepare("INSERT INTO materialy_przydzielone (id_zlecenia, id_materialu, ilosc)
  44. VALUES (:id_zlecenia, :id_materialu, :ilosc)");
  45. $query->bindParam(':id_zlecenia', $id_zlecenia);
  46. $query->bindParam(':id_materialu', $id_materialu);
  47. $query->bindParam(':ilosc', $ilosc);
  48. $query->execute();
  49. $response = true;
  50. } catch (PDOException $e) {
  51. $response = $e;
  52. }
  53. return $response;
  54. }
Go to the top of the page
+Quote Post
Lion_87
post 17.02.2020, 21:03:59
Post #4





Grupa: Zarejestrowani
Postów: 850
Pomógł: 120
Dołączył: 15.02.2007
Skąd: Łódź

Ostrzeżenie: (10%)
X----


A może "$_GET['order']" zamiast "$_GET['id']" ?
Go to the top of the page
+Quote Post
sauber94
post 18.02.2020, 13:48:26
Post #5





Grupa: Zarejestrowani
Postów: 55
Pomógł: 0
Dołączył: 22.05.2015

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


Niestety nie pomogła ta zmiana :/
Go to the top of the page
+Quote Post
dublinka
post 18.02.2020, 15:08:50
Post #6





Grupa: Zarejestrowani
Postów: 594
Pomógł: 66
Dołączył: 22.02.2008
Skąd: Dublin

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


Niebardzo rozumiem.
1.Masz dodany rekord i chcesz go aktualizowac czy dodac calkiem nowy.
2.Skad Ci sie tam bierze $_GET['id']
3.Formularz pokaz.
4.Nie prosciej dla Ciebie zamiast klas zrobic w prostszy sposob ?
5. Jaka ma budowe tabela.
6. header('Location: /assigned_material?order='.$id.''); // a gdzie rozszerzenie?

Ten post edytował dublinka 18.02.2020, 15:36:11


--------------------
Go to the top of the page
+Quote Post
sauber94
post 18.02.2020, 21:00:57
Post #7





Grupa: Zarejestrowani
Postów: 55
Pomógł: 0
Dołączył: 22.05.2015

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


Lion_87, raczej masz rację, powinno być $id_zlecenia = $_GET['order'];

dublinka, chcę dodać nowy. Dodaję materiał do utworzonego wcześniej zlecenia.

Wygląd tabeli materialy_przydzielone:
id_zlecenia
id_materialu
ilosc

Kod formularza przydzielenia materiału do zlecenia wygląda tak:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Formularz</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="./libs/style.css">
  8. <script src="https://kit.fontawesome.com/a076d05399.js"></script>
  9. </head>
  10. <body>
  11. <div id="forum">
  12. <div id="navigation"><i class="fa fa-home"></i> Strona główna <i class="fas fa-chevron-left"></i> Przypisz materiał</div>
  13. <table cellpadding="10" class="table table-bordered">
  14. <thead>
  15. <tr>
  16. <th>Materiał</th>
  17. <th>Ilość</th>
  18. <th>Usuń</th>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. <?php
  23. foreach($assigned_materials as $assigned_material){ ?>
  24. <tr>
  25. <td><?=(isset($info['nazwa'])) ? $info['nazwa'] : ""?><?=$assigned_material['id_materialu']?></td>
  26. <td><?=$assigned_material['ilosc']?></td>
  27. <td>
  28. <form method="post" action='./assigned_materialdelete'>
  29. <input type="hidden" name="id" value="<?=$info['id_materialu']?>">
  30. <input value="Usuń" type="button" onClick="confSubmit(this.form);" class="btn btn-danger"/>
  31. </form>
  32. </td>
  33. </td>
  34. </tr>
  35. <?php } ?>
  36. </tbody>
  37. </table>
  38. <br /><h2>Nowe przypisanie materiału</h2><br />
  39. <form action="" method="post">
  40.  
  41. <div class="form-group">
  42. <label class="control-label col-sm-2" for="id_materialu">Materiał:</label>
  43. <select name="id_materialu" id="id_materialu" class="form-control">
  44. <?php
  45. foreach($materials as $material){ ?>
  46. <option value="1" value="<?=$material['id_materialu']?>"><?=$material['nazwa']?></option>
  47. <?php } ?>
  48. </select>
  49. </div>
  50. <div class="form-group">
  51. <label class="control-label col-sm-2" for="ilosc">Ilość:</label>
  52. <div>
  53. <input required type="text" class="form-control" id="ilosc" name="ilosc">
  54. </div>
  55. </div>
  56. <div class="form-group">
  57. <div class="col-sm-offset-2">
  58. <button onClick="submit(this.form, <?$_SESSION['id']?>)" class="btn btn-danger">Przypisz materiał</button> <button type="reset" value="Anuluj" class="btn btn-danger">Wyczyść</button><br />
  59. </div>
  60. </div>
  61. </form>
  62. </div>
  63. </body>
  64. <script type="text/javascript">
  65. function confSubmit(form) {
  66. if (confirm("Jestes pewny?")) {
  67. form.submit();
  68. }
  69. }
  70. </script>
  71. </html>


Ten post edytował sauber94 18.02.2020, 21:14:58
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: 28.03.2024 - 15:47