Should the php snippet at the end of this old thread work with the latest version of PT?
http://www.pricetapestry.com/node/506
I tried, but the export file is empty.
I need to export the data imported into PT into a csv or xml file.
Thanks!
Hello David,
How can i select a merchant to export data for?
For example export data for merchant1,merchant4 etc.
Thanks!
Hi,
To hard code the required merchant, you can add a WHERE clause to the SELECT SQL, so where you have the following code at line 8 of the above script:
$sql = "SELECT * FROM `".$config_databaseTablePrefix."products`";
...REPLACE with:
$sql = "SELECT * FROM `".$config_databaseTablePrefix."products` WHERE merchant='Merchant Name'";
If you wanted to pass the merchant name as a parameter in the URL, use:
$sql = "SELECT * FROM `".$config_databaseTablePrefix."products` WHERE merchant='".database_safe($_GET["merchant"])."'";
...and then in the request URL, add:
?merchant=Merchant+Name
("+" is the URL encoding for SPACE)
Hope this helps!
Cheers,
David.
--
PriceTapestry.com
Hi.
Can you update the code for PHP 7? This functions are deprecated.
Also, how can I modify the script to save the CSV on the server rather than download?
Thanks!
Hi,
Here's the above updated for mysqli_* functions;
<?php
set_time_limit(0);
require("includes/common.php");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=products.csv");
$link = mysqli_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword,$config_databaseName);
$sql = "SELECT * FROM `".$config_databaseTablePrefix."products`";
mysqli_real_query($link,$sql);
$result = mysqli_use_result($link);
print "Merchant,Name,Description,ImageURL,BuyURL,Price,Category,Brand\n";
$search = array("\n","\r",",");
while($row = mysqli_fetch_assoc($result))
{
foreach($row as $k => $v)
{
$row[$k] = str_replace($search," ",$v);
}
print $row["merchant"].",";
print $row["name"].",";
print $row["description"].",";
print $row["image_url"].",";
print $row["buy_url"].",";
print $row["price"].",";
print $row["category"].",";
print $row["brand"]."\n";
}
?>
Without modification you could use redirection to save to the server e.g. as a cron job or from the command line using;
cd /path/to/scripts/;php export.php > ../feeds/export.csv
So a change of directory to the scripts folder; followed by the command using ">" to redirect stdout to the file ../feeds/export.csv. If you would prefer the script to write to a file directly just let me know...
Cheers,
David.
--
PriceTapestry.com
Hi dflsports,
Have a go with the following which uses an unbuffered query (so no memory constraints) instead of database_querySelect(). For a large database, there may have been insufficient memory to save the result set using the basic version from node 506...
<?php
set_time_limit(0);
require("includes/common.php");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=products.csv");
$link = @mysql_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword);
@mysql_select_db($config_databaseName,$link);
$sql = "SELECT * FROM `".$config_databaseTablePrefix."products`";
$result = mysql_unbuffered_query($sql,$link);
print "Merchant,Name,Description,ImageURL,BuyURL,Price,Category,Brand\n";
$search = array("\n","\r",",");
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
foreach($row as $k => $v)
{
$row[$k] = str_replace($search," ",$v);
}
print $row["merchant"].",";
print $row["name"].",";
print $row["description"].",";
print $row["image_url"].",";
print $row["buy_url"].",";
print $row["price"].",";
print $row["category"].",";
print $row["brand"]."\n";
}
?>
Cheers,
David.
--
PriceTapestry.com