Last active
August 29, 2015 14:25
-
-
Save ALTELMA/a387c0defccd81ee0b5e to your computer and use it in GitHub Desktop.
PHP generate XML with large database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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