Page is a not externally linkable
ZakAltF4 - 10:20 pm on Feb 16, 2012 (gmt 0)
I have written a script to dynamically write our Meta descriptions and keywords .. My issue that I am having is that I am receiving a PHP memory error. Granted this is when trying to run the script from the browser. I don't have control over the PHP memory settings, however according to our Dedicated Host it is set to 1024MB. Will running this cron off the server still generate this error? If so, is there a way that any of you can see that I can reduce the amount of memory used for the following script?
Some background info: There are some 15,000 products that reside in over 700 categories ...
<?php
//This script takes individual product/category info and generates more narrowed meta data ...
$mageFilename = $_SERVER['DOCUMENT_ROOT'] . '/app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app('-- snip --_english'); // change to admin possibly
Mage::setIsDeveloperMode(true);
ini_set('display_errors',1);
echo "####################################### Start ###################################<br>";
?>
<?php
$collection = Mage::getModel('catalog/category')->getCollection();
$collection->addAttributeToSelect('*');
$counter = 0;
foreach ($collection as $element) {
$category_name = $element->getName();
$category_id = $element->getId();
if ($counter == 99999) { break; } // Set to 3 for testing
if ($counter !== 0 && $counter !== 1)
{
echo $category_name . " " . $category_id . "<br>"; // Do stuff here
$mArray = array();
$category = new Mage_Catalog_Model_Category();
//$category->getResource()->getAttribute('description');
$loaded = $category->load($category_id);
$collection1 = $loaded->getProductCollection();
$pc = 0;
foreach ($collection1 as $product) {
// var_dump($product) . "<br><br>";
//if ($pc < 4){
$pid = $product->getId();
$model = Mage::getModel('catalog/product'); //getting product model
$_product = $model->load($pid);
//##################################
$result = $_product->getName();
$result = preg_replace('/\ - .*/', '', $result);
array_push($mArray, $result);
$acv=array_count_values($mArray); // 1=>2, 2=>3,3=>1
arsort($acv); //save keys, 2=>3, 1=>2, 3=>1
$result=array_keys($acv);
//###################################
$pc++;
// }
}
$kw = $category_name; //get name
$ds = "";
if (isset($result[0])){
$kw = $kw . ", " . $result[0];
$ds = $ds . ", " . $result[0];
}
if (isset($result[1])){
$kw = $kw . ", " . $result[1];
$ds = $ds . ", " . $result[1];
}
if (isset($result[2])){
$kw = $kw . ", " . $result[2];
$ds = $ds . ", " . $result[2];
}
if (isset($result[3])){
$kw = $kw . ", " . $result[3];
$ds = $ds . ", " . $result[3];
}
if (isset($result[4])){
$kw = $kw . ", " . $result[4];
$ds = $ds . ", " . $result[4];
}
$kw = $kw . ", Natural, Organic, Healthy, Green";
$kw = preg_replace('/[^a-z0-9]/i', ' ', $kw);
try
{
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$table = "catalog_category_flat_store_1";
$query = "UPDATE {$table} SET meta_keywords = '{$kw}' WHERE entity_id = $category_id" ;
$writeConnection->query($query);
}
catch(Exception $e)
{
echo $e;
}
$ds = "In " . $category_name . " , enjoy brands like " . $ds . "! Live eco-friendly with -- Snip -- !" ;
$ds = preg_replace('/[^a-z0-9]/i', ' ', $ds);
try
{
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$table = "catalog_category_flat_store_1";
$query = "UPDATE {$table} SET meta_description = '{$ds}' WHERE entity_id = $category_id" ;
$writeConnection->query($query);
}
catch(Exception $e)
{
echo $e;
}
}
$counter++;
}
?>
<?php
echo "####################################### End ###################################<br>";
?>
The error being generated is:
Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 71 bytes)
Yeah .. Finally get it working, only to find out it needs to run more efficiently!