Guest User

Untitled

a guest
Mar 1st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
  3.  
  4.  
  5. $servername = 'localhost';
  6. $username = 'names';
  7. $password = 'password';
  8. $dbname = 'base';
  9. $array_attrib = array();
  10. //название группы атрибутов которые будем проставлять
  11. if(isset($_POST['name']) && !empty($_POST['name'])) {
  12. $group_atrib = htmlspecialchars($_POST['name']);
  13. }
  14. else{
  15. $group_atrib = 'не указана';
  16. }
  17. if(isset($_POST['id']) && !empty($_POST['id'])) {
  18. //id-категории товаров в которой нужно проставить атрибуты из группы атрибутов $group_atrib
  19. $cat_id = htmlspecialchars($_POST['id']);
  20. //таблица связки продукт-категория
  21. }
  22. else{
  23. $cat_id = 0;
  24. }
  25. $tbl_prodcat = 'oc_product_to_category';
  26. //таблица с именами категорий
  27. $tbl_catdescr = 'oc_category_description';
  28.  
  29.  
  30. $conn = new mysqli($servername, $username, $password, $dbname);
  31. // Check connection
  32. if ($conn->connect_error) {
  33. die("Connection failed: " . $conn->connect_error);
  34. }
  35.  
  36. // Строим запрос
  37. $sql = "SELECT $tbl_prodcat.product_id, $tbl_catdescr.name FROM $tbl_prodcat, $tbl_catdescr WHERE $tbl_prodcat.category_id = '$cat_id' AND $tbl_prodcat.category_id = $tbl_catdescr.category_id";
  38.  
  39. $sql2 = "SELECT oc_attribute_description.name as descr, oc_attribute_group_description.name as names FROM `oc_attribute_description`, `oc_attribute_group_description`, `oc_attribute` WHERE oc_attribute.attribute_id = oc_attribute_description.attribute_id AND oc_attribute.attribute_group_id = oc_attribute_group_description.attribute_group_id ORDER BY oc_attribute_group_description.name";
  40.  
  41. $sql3 = "SELECT oc_attribute.attribute_id as namesid FROM `oc_attribute_description`, `oc_attribute_group_description`, `oc_attribute` WHERE oc_attribute.attribute_id = oc_attribute_description.attribute_id AND oc_attribute.attribute_group_id = oc_attribute_group_description.attribute_group_id AND oc_attribute_group_description.name = '$group_atrib'";
  42.  
  43. //получаем массив id атрибутов по категории
  44. $result3 = $conn->query($sql3);
  45.  
  46.  
  47. if ($result3->num_rows > 0) {
  48. while($row = $result3->fetch_assoc()) {
  49.  
  50.  
  51. //echo "//" .$row["namesid"]."<br>";
  52.  
  53. $array_attrib[] = $row["namesid"];
  54.  
  55. }
  56. }
  57.  
  58. // Получаем результаты по aтрибутам
  59. $result = $conn->query($sql2);
  60. if ($result->num_rows > 0) {
  61.  
  62. // output data of each row
  63.  
  64. echo "<!DOCTYPE html>
  65. <html>
  66. <head>
  67. <style>
  68. table {
  69. font-family: arial, sans-serif;
  70. border-collapse: collapse;
  71. width: 100%;
  72. }
  73.  
  74. td, th {
  75. border: 1px solid #dddddd;
  76. text-align: left;
  77. padding: 8px;
  78. }
  79.  
  80. tr:nth-child(even) {
  81. background-color: #dddddd;
  82. }
  83. </style>
  84. </head>
  85. <body>
  86. <table>
  87. <tr>
  88. <th>Группа атрибутов</th>
  89. <th>Название атрибута</th>
  90. </tr>";
  91.  
  92. while($row = $result->fetch_assoc()) {
  93. echo "<tr><td>" . $row["names"]. "</td><td>".$row["descr"]."</td></tr>";
  94. }
  95.  
  96. } else {
  97. echo "0 results";
  98. }
  99. echo "
  100. </table><hr>
  101. <form action=\"\" method=\"post\">
  102. <p><b>ID категории</b>
  103. <input type=\"text\" size=\"40\" name=\"id\">
  104. &nbsp;<b>Название группы атрибутов</b>
  105. <input type=\"text\" size=\"40\" name=\"name\">
  106. <input type=\"submit\">
  107. </form>
  108.  
  109. ";
  110.  
  111.  
  112. // Получаем результаты
  113. echo "<textarea id=\"bar\" rows=\"100\" cols=\"185\">";
  114. $result = $conn->query($sql);
  115. if ($result->num_rows > 0) {
  116. while($row = $result->fetch_assoc()) {
  117. foreach ($array_attrib as &$value) {
  118.  
  119. $product_id = $row["product_id"];
  120. $language_id = '1';
  121. $text = 'заполнить';
  122.  
  123. //echo "Товар id: " . $row["product_id"]. " / категория: " . $row["name"]." / номер атрибута: ".$value."<br>";
  124.  
  125. $sql4 = "INSERT INTO `oc_product_attribute` (product_id, attribute_id, language_id, text) VALUES ('$product_id', '$value', '$language_id', '$text') ON DUPLICATE KEY UPDATE product_id='$product_id', attribute_id='$value'";
  126. //$sql4 = "UPDATE `oc_product_attribute` SET attribute_id = '$value' WHERE product_id = '$product_id' AND attribute_id = '$value'";
  127. //$sql4 = "DELETE FROM `oc_product_attribute` WHERE product_id = '$product_id'";
  128. echo $sql4.";\n";
  129.  
  130. }
  131. }
  132. } else {
  133. echo "0 results";
  134. }
  135. $conn->close();
  136.  
  137. echo "</textarea></body></html>";
Add Comment
Please, Sign In to add comment