Ignore line breaks in fputcsv - php

I am currently trying to export a number of products from a custom CMS MYSQL database into a Magento compatible database.
The products include product descriptions which is stored as HTML that was serialised in a "ob_data" array with some other info when they were added through CMS system. Here is an example of one:
Array
(
[id] => 1085
[type] => 9
[url_key] => royal-worcester-mikado-milk-jug
[name] => Royal Worcester Mikado 1/2 Pint Milk Jug
[pattern] => Mikado
[manufacturer] => Royal Worcester
[stock] => 0
[content] => <p>Bone china<br />Holds approx1/2 pint.<br />Excellent condition.</p>
[weight] => 0.75
[price] => 20.00
[image] => /media/dContent/eCommerce/old/165.jpg
)
Using unserialze I can extract this and some other info into an array which can be written to the CSV. However when I write this information to a CSV file with fputcsv I get the following:
1085,9,royal-worcester-mikado-milk-jug,"Royal Worcester Mikado 1/2 Pint Milk Jug",Mikado,"Royal Worcester",0,"<p>Bone china<br />
Holds approx 1/2 pint.<br />
Excellent condition.</p>",0.75,20.00,/media/dContent/eCommerce/old/165.jpg
Essentially the <br/> tags that are in the description are also adding new lines in my CSV breaking its format and making it impossible to import.
Is there anyway that I can stop this happening and format the content so that it can comfortably written into the CSV?

Related

php showing error but all things are good

please help me.
this is the php code:
<?php
$path_to_file = 'https://kamboz.000webhostapp.com/OurGrocery/OurItemsGet/all_items.php';
$file_contents = file_get_contents($path_to_file);
$json = json_decode($file_contents);
foreach ($json->data as $item) {
echo $item->id;
}
?>
when i run this it shows this error:
Notice: Trying to get property 'data' of non-object in /storage/ssd4/665/18079665/public_html/OurGrocery/TESTING_FOLDER/testing.php on line 28
Warning: Invalid argument supplied for foreach() in /storage/ssd4/665/18079665/public_html/OurGrocery/TESTING_FOLDER/testing.php on line 28
this is the json link: https://kamboz.000webhostapp.com/OurGrocery/OurItemsGet/all_items.php
all things are good. checked it manually and use many websites for chcek json is valid or not. it is valid. but i think it is 'not a object' error. please anyone help me.
The JSON generated on this page is, as per the previous comment, not valid. Using json_decode on the seemingly good string will yield NULL because of various faults in the encoding. There are various faults with the data which should have been fixed using json_encode though perhaps the newlines would require further tweaks beforehand.
Using a regex to replace certain patterns on the above (sortof) json string allows the data to be parsed correctly
// fetch the "broken" JSON string
$url='https://kamboz.000webhostapp.com/OurGrocery/OurItemsGet/all_items.php';
$content=file_get_contents( $url );
// patterns of "broken" json to look for
$pttns=array(
'#\r\n\r\n"#m',
'#\s?\r\n\s?(\w)#m',
'#\r\n\s?"#m'
);
// replace matches with these
$replacements=array(
'"',
'$1',
'"'
);
// do the string fudging
$content=preg_replace( $pttns, $replacements, $content );
// now decoding json **should** be ok (?)
$json=json_decode( $content );
if( is_null( $json ) )echo 'Bad JSON';
else{
$data=$json->data;
// process the json data however you need.
printf('<h1>It works now... <span style="color:red">%s</span></h1>',$data[0]->name);
foreach( $data as $i => $obj ){
printf('<h2>Record: #%d</h2><pre>%s</pre>',$i,print_r($obj,true));
}
}
This yields:
It works now... Colgate Active Salt Tooth Paste 100g TUBE
Record: #0
stdClass Object
(
[id] => 1
[img_url] => https://kamboz.000webhostapp.com/OurGrocery/img_product/1.png
[name] => Colgate Active Salt Tooth Paste 100g TUBE
[category] => Colgate Active Salt Tooth Paste 100g TUBE
[description] => good quality.
[description_full] => this is Colgate Active Salt Tooth Paste 100g TUBE. you can use it in your daily life.
[rating] => 5.0
[discount] => 7
[price] => 64.32
)
Record: #1
stdClass Object
(
[id] => 2
[img_url] => https://kamboz.000webhostapp.com/OurGrocery/img_product/2.png
[name] => Colgate Active Salt 200gm TUBE
[category] => Colgate Active Salt 200gm TUBE
[description] => good quality.
[description_full] => this is Colgate Active Salt 200gm TUBE. you can use it in your daily life.
[rating] => 5.0
[discount] => 4
[price] => 111.82
)
Record: #2
stdClass Object
(
[id] => 3
[img_url] => https://kamboz.000webhostapp.com/OurGrocery/img_product/3.png
[name] => Patanjali DISH WASH BAR
[category] => Patanjali DISH WASH BAR shabun sabun sabn
[description] => good quality.
[description_full] => this is Patanjali DISH WASH BAR. you can use it in your daily life.
[rating] => 5.0
[discount] => 5
[price] => 9.99
)
None of that string fudging would be necessary if the JSON were properly generated initially and this does not constitute a robust solution - focus on creating valid json initally and the rest is simple.

How can I get the cod costs in WooCommerce?

I've a problem. I'm currently writing a WordPress plugin for WooCommerce which reads out an order for a booking system.
The booking system has an own field for cod costs (costs on delivery / Nachnahmegbühr (deutsch)). The problem is that I can't find a method to read this cost from an order.
If I read out the fees from the order via $order->get_fees(), I can see the cod costs but this is not safe enough cause the fee name is always in the shop language which makes it nearly impossible to read out the cod costs this way (in an international plugin):
[data:protected] => Array
(
[order_id] => 24
[name] => Zahlungsgebühr
[tax_class] => 0
[tax_status] => taxable
[amount] => 2.16
[total] => 2.16
[total_tax] => 0.34
[taxes] => Array
(
[total] => Array
(
[6] => 0.344828
)
)
)
So is there any known way to get this kind of costs?

php sort multidimensional array on date column

I have an array $dataStoreForTableImport set up in the following way.
$dataStoreForTableImport['title']
$dataStoreForTableImport['content']
$dataStoreForTableImport['date']
$dataStoreForTableImport['link']
$dataStoreForTableImport['username']
$dataStoreForTableImport['website']
It contains data as below
Array
(
[0] => Array
(
[title] => Quote from Tony Blair
[content] => ... from beating it I'm afraid." (Tony Blair, Sky News) He had every opportunity to put religion in its ...
[articledate] => 28/09/2013
[link] => http://boards.fool.co.uk/message.asp?source=isesitlnk0000001&mid=12890951
[Username] => Michael Dray
[website] => The Motley Fool
)
[1] => Array
(
[title] => Re: The Tony Blair Show
[content] => ... I am irritated that he got such an easy ride; Why? Because he is not to your political liking? He was a witness; he was not on trial and he spoke under oath. What did you expect Jay to ask him? I had dealings with a QC a few years ago. He was as ...
[articledate] => 28/05/2012
[link] => http://boards.fool.co.uk/message.asp?source=isesitlnk0000001&mid=12564154
[Username] => Michael Dray
[website] => The Motley Fool
)
[2] => Array
(
[title] => Re: The Tony Blair Show
[content] => ... If your doubts about Jay's competence/bias were shared I'm sure it would have been debated ad nauseam on Radio 4. Eh - are you serious? I'm a Radio 4 fan - but thats despite its hatred of all things right of centre, not becuase of. ...
[articledate] => 28/05/2012
[link] => http://boards.fool.co.uk/message.asp?source=isesitlnk0000001&mid=12564167
[Username] => Michael Dray
[website] => The Motley Fool
)
[3] => Array
(
[title] => Re: The Tony Blair Show
[content] => ... Maybe Tony should have brought Cherie with him - remember Rupert Murdoch and the reaction of Wendi Deng to the custard pie incident. IMHO Cherie is every bit as intimidating:-) Wendi Deng did not eject the pie flinger, she intervened when he acted. Use of an angled bat to deflect criticism from ...
[articledate] => 30/05/2012
[link] => http://boards.fool.co.uk/message.asp?source=isesitlnk0000001&mid=12565346
[Username] => Michael Dray
[website] => The Motley Fool
)
[4] => Array
(
[title] => Re: The Tony Blair Show
[content] => ... What did surprise me was the fact that he had his own personal bodyguards in the hearing with him. Although, given the level of security that allowed that protester to break into the hearing, maybe he had a point! Eh? He is clearly at risk of terror ...
[articledate] => 28/05/2012
[link] => http://boards.fool.co.uk/message.asp?source=isesitlnk0000001&mid=12564500
[Username] => Michael Dray
[website] => The Motley Fool
)
I want to be able to remove rows from this array that if the articleDate is before a given date.
I have tried everything but it does not seem to work. I am not even able to get it to sort correctly by date?
The date comes in the format - February 10, 2007
I have used
$sortDate = date('d/m/Y', strtotime($sortDate));
to format it to the format shown in the array above.
Can anyone please help?
Thanks
Mike
Do the filtering and sorting of rows in the database backend. This will improve the performance of you app. Use a WHERE clause for filtering by date and an ORDER BY clause for ordering by date in you SQL query.

PHP Regex, Only getting back partial Results

I have the following PHP regex:
#<tr[\s\S]*?<a class="b1"[\s\S]*?<em[^>]*>([^<]*)[\s\S]*?stars_small_([0-9].[0-9])#
Which I am using on this site:
Gamespy
I get back this data:
[1] => Array
(
[0] => AC/DC Live: Rock Band Track Pack
[1] => Ace Combat 6: Fires of Liberation
[2] => All-Pro Football 2K8
[3] => Alone in the Dark
[4] => Armored Core 4
[5] => Army of Two
[6] => Army of Two: The 40th Day
)
[2] => Array
(
[0] => 3.5
[1] => 2.5
[2] => 3.5
[3] => 3.5
[4] => 2.5
[5] => 3.5
[6] => 3.5
)
This is what I am looking for, however I don't seem to be getting back all of the data. I should get the following Titles with scores. But for some reason I am only getting some of them.
AC/DC Live: Rock Band Track Pack
Ace Combat 6: Fires of Liberation
Afro Samurai
Alan Wake
Aliens vs. Predator
All-Pro Football 2K8
Alone in the Dark
Amped 3
Armored Core 4
Army of Two
Army of Two: The 40th Day
Assassin's Creed
Assassin's Creed II
Assassin's Creed: Brotherhood
Avatar: The Game
I have tested my regex here:
http://www.solmetra.com/scripts/regex/index.php
Using this HTML:
http://justpaste.it/20u5
Any help explaining why I am only getting back some of the results would be greatly appreciated. Thanks
Change the sub-pattern stars_small_([0-9].[0-9]) to stars_small_([0-9](?:\.[0-9])?) as some of the urls only have one digit in the SRC attribute of the IMG tag.

How to retrieve custom attributes in Opencart?

I have defined some custom attributes and assigned them to products via the Opencart admin panel. I'm trying to retrieve these attributes upon checkout to adjust some shipping costs.
I'm currently working in the catalog/model/shipping/ups.php file in the getQuote function. I'm getting close with this:
print_r($this->cart->getProducts());
Which gives me the following product data:
Array
(
[59] => Array
(
[key] => 59
[product_id] => 59
[name] => My Product Name
[model] => ABC
[shipping] => 1
[image] => data/myproduct.jpg
[option] => Array
(
)
[download] => Array
(
)
[quantity] => 1
[minimum] => 1
[subtract] => 1
[stock] => 1
[price] => 29.99
[total] => 29.99
[reward] => 0
[points] => 0
[tax_class_id] => 0
[weight] => 3.75
[weight_class_id] => 5
[length] => 0.00
[width] => 0.00
[height] => 0.00
[length_class_id] => 3
)
)
However, no custom attributes are returned. I'm sure there is a nice way to pass the product ID to an attribute-getting function, but I can't find it. The Opencart docs are a little lacking for the development side and no luck on Google.
Usually I'd grep the hell out of the whole directory structure to find what I'm looking for, but shell access is disabled on this particular web host :(.
EDIT
I believe Attributes are a newer feature that is built into Opencart. Its like a custom field.
Anyway, on the admin side I went to Catalog > Attributes and created a custom attribute called "Shipping Box Type". Then, under a specific product I can set the attribute. See screenshot. My goal is to retrieve the value of "Shipping Box Type". Does that answer your question #Cleverbot? I'm sure I can do a custom db query to grab it, but theres got to be a built-in function to grab attributes?
Retrieve attributes for a given product_id
$this->load->model('catalog/product');
$attributes = $this->model_catalog_product->getProductAttributes(59);
print_r($attributes);
Output
Array
(
[0] => Array
(
[attribute_group_id] => 9
[name] => Shipping Fields
[attribute] => Array
(
[0] => Array
(
[attribute_id] => 16
[name] => Shipping Box Type
[text] => SM2
)
)
)
)
Taking it further, here is an example of looping through the products currently in the cart to see what attributes they have:
$this->load->model('catalog/product');
$cart_products = $this->cart->getProducts();
// get attributes for each product in the cart
foreach($cart_products as $product) {
$attributes = $this->model_catalog_product->getProductAttributes($product['product_id']);
print_r($attributes);
}
This is not the most intuitive feature in Opencart. First you have to go catalog>attributes>attribute groups and you have to create a group name that will be a title for your attribute. For this it might be "Shipping Specifications".
Then you need to go to catalog>attributes>attributes> and create a new attribute under "Shipping Specifications" named "Shipping Box Type".
Now you are ready to go to catalog>products and add the attribute to a product.
The catch is that it won't be displayed how you were hoping I think. It will show up under the specifications tab next to description on your products page. You have the easy option of changing "Specifications" to the Heading of your choice in catalog/view/*your_theme*/products/products.tpl or you can edit the same tpl file to change the container/format that the data output goes.

Categories