Skip to content

Instantly share code, notes, and snippets.

@fuzzysteve
Created December 11, 2013 02:51
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 fuzzysteve/7904434 to your computer and use it in GitHub Desktop.
Save fuzzysteve/7904434 to your computer and use it in GitHub Desktop.
A little something to load assets into a DB
<?php
require_once('db.inc.php');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "$url");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($curl);
$xml = new simpleXMLElement ($content);
$containersql="insert ignore into containers (id,typeid,location) values (?,?,?)";
$itemvsql="insert into assetsv (version,container,typeid,quantity,flag) values (?,?,?,?,?)";
$itemsql="insert into assets (container,typeid,quantity,flag) values (?,?,?,?)";
$versionchecksql="select count(*) cached from assetVersions where versionkey=?";
$versionupdatesql="insert into assetVersions (downloaded,versionkey) values (now(),?)";
$clearsql="truncate table assets";
$versionkey=md5($xml->cachedUntil);
$stmt = $dbh->prepare($versionchecksql);
$stmt->execute(array($versionkey));
$row = $stmt->fetchObject();
if ($row->cached)
{
exit;
}
$trunstmt=$dbh->prepare($clearsql);
$trunstmt->execute();
$stmt = $dbh->prepare($versionupdatesql);
$stmt->execute(array($versionkey));
$versionid=$dbh->lastInsertId();
$containerstmt=$dbh->prepare($containersql);
$itemvstmt=$dbh->prepare($itemvsql);
$itemstmt=$dbh->prepare($itemsql);
#echo $content;
foreach ($xml->result->rowset->row as $container)
{
$containerstmt->execute(array($container['itemID'],$container['typeID'],$container['locationID']));
$condense=array();
if (!empty($container->rowset))
{
foreach ($container->rowset->row as $item)
{
if (array_key_exists($item['typeID'].":".$item['flag'],$condense))
{
$condense[$item['typeID'].":".$item['flag']]+=$item['quantity'];
}
else
{
$condense[$item['typeID'].":".$item['flag']]=$item['quantity'];
}
}
}
foreach ($condense as $citem => $quantity)
{
list ($typeid,$flag)=explode(':',$citem);
$itemvstmt->execute(array($versionid,$container['itemID'],$typeid,$quantity,$flag));
$itemstmt->execute(array($container['itemID'],$typeid,$quantity,$flag));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment