Skip to content

Instantly share code, notes, and snippets.

@ALTELMA
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ALTELMA/a387c0defccd81ee0b5e to your computer and use it in GitHub Desktop.
Save ALTELMA/a387c0defccd81ee0b5e to your computer and use it in GitHub Desktop.
PHP generate XML with large database.
class XMLFeed
{
private $xmlHeader = '<products>';
private $xmlFooter = '</products>';
private $xmlFeedFile = 'products.xml';
public function feed()
{
$offeset = 0;
$perLoop = 1000;
$xmlFile = fopen($this->xmlFeedFile, 'w');
fwrite($xmlFile, $this->xmlHeader);
for ($i = 0; $i < 20; $i++)
{
$offeset = $i * $perLoop;
$sql = "SELECT id, name, price, category FROM product "
. "LEFT JOIN category ON product.category_id = category.id "
. "LIMIT " . $offeset . "," . $perLoop;
$query = mysql_query($sql);
$products = mysql_fetch_assoc($query);
foreach ($products as $product)
{
$xml = '<product>';
$xml .= '<id>' . $product->id . '</id>';
$xml .= '<name>' . $product->name . '</name>';
$xml .= '<price>' . $product->price . '</price>';
$xml .= '<category>' . $product->category . '</category>';
$xml .= '</product>';
}
fwrite($xmlFile, $xml);
}
fwrite($xmlFile, $this->xmlFooter);
fclose($xmlFile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment