Advertisement
Guest User

feed

a guest
Feb 1st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. <?php
  2. ini_set('max_execution_time', 300);
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $dbname = "xml";
  7.  
  8. $conn = new mysqli($servername, $username, $password, $dbname);
  9.  
  10. $sql = "SELECT * FROM modifications LIMIT 10";
  11. $result = $conn->query($sql);
  12.  
  13. $num = 0;
  14. //Parent row
  15. while($row = $result->fetch_assoc()){
  16. $prod[$num]['unique_id'] = $row['id'];
  17. $prod[$num]['parent_id'] = '';
  18. $prod[$num]['product_type'] = 'parent';
  19. $prod[$num]['title'] = $row['title'];
  20. $prod[$num]['description_en'] = '';
  21. $prod[$num]['price'] = 0;
  22. $prod[$num]['category'] = str_replace("|"," > ",$row['category']);
  23. $prod[$num]['image_1'] = '';
  24. $prod[$num]['ean'] = '';
  25. $prod[$num]['brand'] = $row['brand'];
  26. $prod[$num]['quantity'] = 0;
  27. $prod[$num]['size'] = '';
  28. $prod[$num]['colour'] = $row['colour'];
  29. $prod[$num]['gender'] = $row['gender'];
  30. $prod[$num]['product_variation'] = 'size';
  31. $prod[$num]['shipping_fee'] = '';
  32. $prod[$num]['material'] = '';
  33.  
  34. //Define last parent row for future value update
  35. $lastParent = $num;
  36.  
  37. $num++;
  38.  
  39. $sql2 = "SELECT * FROM items WHERE modification_id = ".$row['id'];
  40. $result2 = $conn->query($sql2);
  41. //Child row
  42. while($row2 = $result2->fetch_assoc()){
  43. $prod[$num]['unique_id'] = $row['id'].'_'.$row2['item_id'];
  44. $prod[$num]['parent_id'] = $row['id'];
  45. $prod[$num]['product_type'] = 'child';
  46. $prod[$num]['title'] = $row2['item_title'];
  47. $prod[$num]['description_en'] = '';
  48. $prod[$num]['price'] = $row2['price'];
  49. $prod[$num]['category'] = str_replace("|"," > ",$row['category']);
  50. $prod[$num]['image_1'] = '';
  51. $prod[$num]['ean'] = $row2['ean'];
  52. $prod[$num]['brand'] = $row2['brand'];
  53. $prod[$num]['quantity'] = $row2['quantity'];
  54. $prod[$num]['size'] = $row2['size'];
  55. $prod[$num]['colour'] = $row2['colour'];
  56. $prod[$num]['gender'] = $row['gender'];
  57. $prod[$num]['product_variation'] = 'size';
  58. $prod[$num]['shipping_fee'] = 0;
  59. $prod[$num]['material'] = '';
  60.  
  61. //Update parent price if current price is less
  62. $prod[$lastParent]['price'] = ($row2['price'] < $prod[$lastParent]['price'] || $prod[$lastParent]['price'] == 0 ? $row2['price'] : $prod[$lastParent]['price']);
  63.  
  64. //Increase parent product quantity
  65. $prod[$lastParent]['quantity'] += $row2['quantity'];
  66.  
  67. $num++;
  68. }
  69. }
  70.  
  71. $output = fopen("php://output",'w') or die("Can't open php://output");
  72. header("Content-Type:application/csv");
  73. header("Content-Disposition:attachment;filename=feed.csv");
  74. fputcsv($output, array('unique_id','parent_id','product_type','title','description_en','price','category','image_1','ean','brand','quantity','size','colour', 'gender','product_variation','shipping_fee','material'));
  75. foreach($prod as $product) {
  76. fputcsv($output, $product);
  77. }
  78. fclose($output) or die("Can't close php://output");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement