Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('max_execution_time', 300);
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "xml";
- $conn = new mysqli($servername, $username, $password, $dbname);
- $sql = "SELECT * FROM modifications LIMIT 10";
- $result = $conn->query($sql);
- $num = 0;
- //Parent row
- while($row = $result->fetch_assoc()){
- $prod[$num]['unique_id'] = $row['id'];
- $prod[$num]['parent_id'] = '';
- $prod[$num]['product_type'] = 'parent';
- $prod[$num]['title'] = $row['title'];
- $prod[$num]['description_en'] = '';
- $prod[$num]['price'] = 0;
- $prod[$num]['category'] = str_replace("|"," > ",$row['category']);
- $prod[$num]['image_1'] = '';
- $prod[$num]['ean'] = '';
- $prod[$num]['brand'] = $row['brand'];
- $prod[$num]['quantity'] = 0;
- $prod[$num]['size'] = '';
- $prod[$num]['colour'] = $row['colour'];
- $prod[$num]['gender'] = $row['gender'];
- $prod[$num]['product_variation'] = 'size';
- $prod[$num]['shipping_fee'] = '';
- $prod[$num]['material'] = '';
- //Define last parent row for future value update
- $lastParent = $num;
- $num++;
- $sql2 = "SELECT * FROM items WHERE modification_id = ".$row['id'];
- $result2 = $conn->query($sql2);
- //Child row
- while($row2 = $result2->fetch_assoc()){
- $prod[$num]['unique_id'] = $row['id'].'_'.$row2['item_id'];
- $prod[$num]['parent_id'] = $row['id'];
- $prod[$num]['product_type'] = 'child';
- $prod[$num]['title'] = $row2['item_title'];
- $prod[$num]['description_en'] = '';
- $prod[$num]['price'] = $row2['price'];
- $prod[$num]['category'] = str_replace("|"," > ",$row['category']);
- $prod[$num]['image_1'] = '';
- $prod[$num]['ean'] = $row2['ean'];
- $prod[$num]['brand'] = $row2['brand'];
- $prod[$num]['quantity'] = $row2['quantity'];
- $prod[$num]['size'] = $row2['size'];
- $prod[$num]['colour'] = $row2['colour'];
- $prod[$num]['gender'] = $row['gender'];
- $prod[$num]['product_variation'] = 'size';
- $prod[$num]['shipping_fee'] = 0;
- $prod[$num]['material'] = '';
- //Update parent price if current price is less
- $prod[$lastParent]['price'] = ($row2['price'] < $prod[$lastParent]['price'] || $prod[$lastParent]['price'] == 0 ? $row2['price'] : $prod[$lastParent]['price']);
- //Increase parent product quantity
- $prod[$lastParent]['quantity'] += $row2['quantity'];
- $num++;
- }
- }
- $output = fopen("php://output",'w') or die("Can't open php://output");
- header("Content-Type:application/csv");
- header("Content-Disposition:attachment;filename=feed.csv");
- 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'));
- foreach($prod as $product) {
- fputcsv($output, $product);
- }
- fclose($output) or die("Can't close php://output");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement