Jump to content
php.lv forumi

Problēma ar izvilkumiem no datubāzes


prieks

Recommended Posts

Radusies baigā problēma. Gribu no Prestashop izvilkt produktu datus xml failā, lai varētu parādīt salidzini.lv. Atradu kādu jau gatavu variantu, taču ar to man rodas kļūdas. Kods ir šāds:

 


$tags = array(
 'name' => 'name',
 'link' => 'link',
 'price_inc' => 'price',
 'imageUrl' => 'image',
 'ean' => 'ean',
 'categoryName' => 'category_full',
 'subCategoryName' => 'subCategory',
 'manufacturerName' => 'manufacturer',
);


function my_strip_tags($t)
{
 $t = preg_replace('/</',' <',$t);
 $t = preg_replace('/>/','> ',$t);
 $t = preg_replace('/[\n\r\t]/',' ',$t);
 $t = preg_replace('/  /',' ',$t); 
 return strip_tags($t);
}


function print_field($k, $v)
{
 global $tags;
 if (isset($tags[$k])) {
   $k = $tags[$k];
   $v = htmlspecialchars($v, ENT_NOQUOTES, "UTF-8");
   print "    <$k>$v</$k>\n";
 }
}


header("Content-Type:text/xml; charset=utf-8");
print '<?xml version="1.0" encoding="UTF-8"?>'."\n";
print "<root>\n";
$configuration = Configuration::getMultiple(array(
 'PS_LANG_DEFAULT', 
 'PS_SHIPPING_FREE_PRICE',
 'PS_SHIPPING_HANDLING',
 'PS_SHIPPING_METHOD', 
 'PS_SHIPPING_FREE_WEIGHT',
 'PS_CARRIER_DEFAULT'));
$id_lang = intval($configuration['PS_LANG_DEFAULT']);
$id_zone = intval($defaultCountry->id_zone);
$link = new Link();
$id_carrier = $configuration['PS_CARRIER_DEFAULT'];
$carrier = new Carrier(intval($id_carrier));
$carrierTax = 0;
if ($carrier->id_tax) {
 $tax = new Tax(intval($carrier->id_tax));
 if (Validate::isLoadedObject($tax) AND Tax::zoneHasTax(intval($tax->id), intval($id_zone)) AND !Tax::excludeTaxeOption())
   $carrierTax = $tax->rate;
}
$passwd = Tools::getValue('passwd');
$categories = Category::getCategories($id_lang, true, false);
$manufacturers = Manufacturer::getManufacturers(false, $id_lang);
if ($passwd == $confPasswd) {
 $products = Product::getProducts($id_lang, 0, 0, 'price', 'ASC', false, true);
}
else {
 $products = array();
}
$manMap =array();
foreach ($manufacturers as $manufacturer) {
 $manMap[$manufacturer['id_manufacturer']] = $manufacturer;
}
$catMap =array();
foreach ($categories as $category) {
 $catMap[$category['id_category']] = $category;

}
foreach ($products as $product) {
   $product['link'] = $link->getProductLink($product['id_product']);
$product['price_inc'] = $product['price'] * (1 + $product['tax_rate'] / 100);
$product['price_inc'] = round($product['price_inc'], 2);
 $cover = Product::getCover($product['id_product']);
 $product['imageUrl'] = _PS_BASE_URL_.$link->getImageLink($prodObj->link_rewrite, $product['id_product'].'-'.$cover['id_image'], 'large');

 $product['manufacturerName'] = $manMap[$product['id_manufacturer']]['name'];

 print "  <item>\n";
 foreach ($product as $k => $v) {
   print_field($k, $v);
 }
 $catList = Product::getIndexedCategories($product['id_product']);
 foreach ($catList as $cat) {
   $catNames = array();
   $catId = $cat['id_category'];
   while ($catId && $catId != 1) {
     array_unshift($catNames, trim($catMap[$catId]['name']));
     $catId = $catMap[$catId]['id_parent'];
   }
   print_field('categoryName', implode(' | ', $catNames));

//$result = mysql_query("SELECT * FROM s_category_product WHERE id_category='{$cat['id_category']}'");
//while($row = mysql_fetch_array($result)) {
	echo "<category_link>http://mansdomeins.lv/category.php?id_category={$cat['id_category']}</category_link>";
//}
 }
 print "  </item>\n";
}
print "</root>\n";

 

Taču izvadot rodas problēma. Ja konkrētajam produktam ir atzīmētas vairākas kategorijas, tad arī tiek piešķirtas izvadē vairākas kategorijas. Kā es varu likt izvadīt produktu tikai ar 1 kategoriju? (<category_full>)

 

Un otrs jautājums - man vajadzētu, lai šis xml fails ikdienas tiktu atjaunots. Kā es to varu izdarīt?

Link to comment
Share on other sites

Tevis paša kodā, tak ir, ka atgriež vairākas kategorijas un tad visām iet cauri ar ciklu.

Sliktākajā gadījumā aizkomentē foreach rindu un to noslēdzošo } un pēc foreach rindas ieliec $cat=$catList[0];

Labākā gadījumā, uztaisi/pārtaisi funkciju lai atgriež tikai vienu kategoriju un izņem ciklu un pārtaisi atbilstoši, pie datu izvadīšanas.

$catList = Product::getIndexedCategories($product['id_product']);
 foreach ($catList as $cat) {
   $catNames = array();
   $catId = $cat['id_category'];
   while ($catId && $catId != 1) {
     array_unshift($catNames, trim($catMap[$catId]['name']));
     $catId = $catMap[$catId]['id_parent'];
   }
   print_field('categoryName', implode(' | ', $catNames));

       //$result = mysql_query("SELECT * FROM s_category_product WHERE id_category='{$cat['id_category']}'");
       //while($row = mysql_fetch_array($result)) {
               echo "<category_link>http://mansdomeins.lv/category.php?id_category={$cat['id_category']}</category_link>";
       //}
 }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...