Basically I have just successfully installed MongoDB and it's PHP extension. I want to use code completion in my IDE for the MongoDB php library and closest I have gotten to getting an answer is some stuff about PDT with Eclipse. I am not getting anywhere.
Ok after a lot of searching I found some code that helps me do just that! I will include the code here for others to use in case something happens to the git repo. All you have to do is write in the classes and functions you want to stub for code completion into these arrays $functions = array();
$classes = array(); https://gist.github.com/ralphschindler/4757829
<?php
define('T', ' ');
define('N', PHP_EOL);
$functions = array();
$classes = array();
$constant_prefix = 'X_';
$php = '<?php' . N;
$php .= '/**' . N . ' * Generated stub file for code completion purposes' . N . ' */';
$php .= N . N;
foreach (get_defined_constants() as $cname => $cvalue) {
if (strpos($cname, $constant_prefix) === 0) {
$php .= 'define(\'' . $cname . '\', ' . $cvalue . ');' . N;
}
}
$php .= N;
foreach ($functions as $function) {
$refl = new ReflectionFunction($function);
$php .= 'function ' . $refl->getName() . '(';
foreach ($refl->getParameters() as $i => $parameter) {
if ($i >= 1) {
$php .= ', ';
}
if ($typehint = $parameter->getClass()) {
$php .= $typehint->getName() . ' ';
}
$php .= '$' . $parameter->getName();
if ($parameter->isDefaultValueAvailable()) {
$php .= ' = ' . $parameter->getDefaultValue();
}
}
$php .= ') {}' . N;
}
$php .= N;
foreach ($classes as $class) {
$refl = new ReflectionClass($class);
$php .= 'class ' . $refl->getName();
if ($parent = $refl->getParentClass()) {
$php .= ' extends ' . $parent->getName();
}
$php .= N . '{' . N;
foreach ($refl->getProperties() as $property) {
$php .= T . '$' . $property->getName() . ';' . N;
}
foreach ($refl->getMethods() as $method) {
if ($method->isPublic()) {
if ($method->getDocComment()) {
$php .= T . $method->getDocComment() . N;
}
$php .= T . 'public function ';
if ($method->returnsReference()) {
$php .= '&';
}
$php .= $method->getName() . '(';
foreach ($method->getParameters() as $i => $parameter) {
if ($i >= 1) {
$php .= ', ';
}
if ($parameter->isArray()) {
$php .= 'array ';
}
if ($typehint = $parameter->getClass()) {
$php .= $typehint->getName() . ' ';
}
$php .= '$' . $parameter->getName();
if ($parameter->isDefaultValueAvailable()) {
$php .= ' = ' . $parameter->getDefaultValue();
}
}
$php .= ') {}' . N;
}
}
$php .= '}';
}
echo $php . N;
Related
I am rewriting some of my code for PHP 7 compatibility.
While most classes I rewrote work fine, I am having an issue with the extended classes that try to access functions from the original(parent) class, and wondering what I am doing wrong.
This is the main class:
class tableBlock {
var $table_border = '0';
var $table_width = '100%';
var $table_cellspacing = '0';
var $table_cellpadding = '2';
var $table_parameters = '';
var $table_row_parameters = '';
var $table_data_parameters = '';
//function tableBlock($contents) { // modified for php 7 compatibility
function __construct($contents) {
$tableBox_string = '';
$form_set = false;
if (isset($contents['form'])) {
$tableBox_string .= $contents['form'] . "\n";
$form_set = true;
array_shift($contents);
}
$tableBox_string .= '<table border="' . $this->table_border . '" width="' . $this->table_width . '" cellspacing="' . $this->table_cellspacing . '" cellpadding="' . $this->table_cellpadding . '"';
if (tep_not_null($this->table_parameters)) $tableBox_string .= ' ' . $this->table_parameters;
$tableBox_string .= '>' . "\n";
for ($i=0, $n=sizeof($contents); $i<$n; $i++) {
$tableBox_string .= ' <tr';
if (tep_not_null($this->table_row_parameters)) $tableBox_string .= ' ' . $this->table_row_parameters;
if (isset($contents[$i]['params']) && tep_not_null($contents[$i]['params'])) $tableBox_string .= ' ' . $contents[$i]['params'];
$tableBox_string .= '>' . "\n";
if (isset($contents[$i][0]) && is_array($contents[$i][0])) {
for ($x=0, $y=sizeof($contents[$i]); $x<$y; $x++) {
if (isset($contents[$i][$x]['text']) && tep_not_null($contents[$i][$x]['text'])) {
$tableBox_string .= ' <td';
if (isset($contents[$i][$x]['align']) && tep_not_null($contents[$i][$x]['align'])) $tableBox_string .= ' align="' . $contents[$i][$x]['align'] . '"';
if (isset($contents[$i][$x]['params']) && tep_not_null($contents[$i][$x]['params'])) {
$tableBox_string .= ' ' . $contents[$i][$x]['params'];
} elseif (tep_not_null($this->table_data_parameters)) {
$tableBox_string .= ' ' . $this->table_data_parameters;
}
$tableBox_string .= '>';
if (isset($contents[$i][$x]['form']) && tep_not_null($contents[$i][$x]['form'])) $tableBox_string .= $contents[$i][$x]['form'];
$tableBox_string .= $contents[$i][$x]['text'];
if (isset($contents[$i][$x]['form']) && tep_not_null($contents[$i][$x]['form'])) $tableBox_string .= '</form>';
$tableBox_string .= '</td>' . "\n";
}
}
} else {
$tableBox_string .= ' <td';
if (isset($contents[$i]['align']) && tep_not_null($contents[$i]['align'])) $tableBox_string .= ' align="' . $contents[$i]['align'] . '"';
if (isset($contents[$i]['params']) && tep_not_null($contents[$i]['params'])) {
$tableBox_string .= ' ' . $contents[$i]['params'];
} elseif (tep_not_null($this->table_data_parameters)) {
$tableBox_string .= ' ' . $this->table_data_parameters;
}
$tableBox_string .= '>' . $contents[$i]['text'] . '</td>' . "\n";
}
$tableBox_string .= ' </tr>' . "\n";
}
$tableBox_string .= '</table>' . "\n";
if ($form_set == true) $tableBox_string .= '</form>' . "\n";
return $tableBox_string;
}
}
This is the extended class:
class box extends tableBlock {
// function box() { // modified for php 7 compatibility
function __construct() {
$this->heading = array();
$this->contents = array();
}
function menuBox($heading, $contents) {
global $menu_dhtml; // add for dhtml_menu
if ($menu_dhtml == false ) { // add for dhtml_menu
$this->table_data_parameters = 'class="menuBoxHeading"';
if ($heading[0]['link']) {
$this->table_data_parameters .= ' onmouseover="this.style.cursor=\'hand\'" onclick="document.location.href=\'' . $heading[0]['link'] . '\'"';
$heading[0]['text'] = ' ' . $heading[0]['text'] . ' ';
} else {
$heading[0]['text'] = ' ' . $heading[0]['text'] . ' ';
}
$this->heading = $this->tableBlock($heading);
$this->table_data_parameters = 'class="menuBoxContent"';
$this->contents = $this->tableBlock($contents);
return $this->heading . $this->contents . $dhtml_contents;
// ## add for dhtml_menu
} else {
$selected = substr(strrchr ($heading[0]['link'], '='), 1);
$dhtml_contents = $contents[0]['text'];
$change_style = array ('<br>'=>' ','<BR>'=>' ', 'a href='=> 'a class="menuItem" href=','class="menuBoxContentLink"'=>' ');
$dhtml_contents = strtr($dhtml_contents,$change_style);
$dhtml_contents = '<div id="'.$selected.'Menu" class="menu" onmouseover="menuMouseover(event)">'. $dhtml_contents . '</div>';
return $dhtml_contents;
}
// ## eof add for dhtml_menu
}
}
As you can see, I modified the constructors to be __construct, but the extended functions errors when it tries to access $this->contents = $this->tableBlock($heading); and $this->contents = $this->tableBlock($contents);
I tried to modify those lines, using $this->contents = parent::__construct($contents); and $this->contents = parent::__construct($heading); but I am probably writing this wrong as it doesn't work either.
Any help is greatly appreciated.
The __construct function is meant to construct the object, which means it is there to create the object in memory and initialize some properties (if you need this). You have used this correctly in your extended class.
However, you cannot return a value from the constructor: Returning a value in constructor function of a class
I would recommend to rename your __construct function again to something like createTableBlock and call this function from your extended class with parent::createTableBlock($arguments).
Also I would recommend to always call your parent constructor (if there is any). You can accomplish this by calling parent::__construct in the constructor of the extended class.
As request by OP his code rewritten:
class tableBlock {
var $table_border = '0';
var $table_width = '100%';
var $table_cellspacing = '0';
var $table_cellpadding = '2';
var $table_parameters = '';
var $table_row_parameters = '';
var $table_data_parameters = '';
function __construct() {
//empty
}
function tableBlock($contents) {
$tableBox_string = '';
$form_set = false;
if (isset($contents['form'])) {
$tableBox_string .= $contents['form'] . "\n";
$form_set = true;
array_shift($contents);
}
$tableBox_string .= '<table border="' . $this->table_border . '" width="' . $this->table_width . '" cellspacing="' . $this->table_cellspacing . '" cellpadding="' . $this->table_cellpadding . '"';
if (tep_not_null($this->table_parameters)) $tableBox_string .= ' ' . $this->table_parameters;
$tableBox_string .= '>' . "\n";
for ($i=0, $n=sizeof($contents); $i<$n; $i++) {
$tableBox_string .= ' <tr';
if (tep_not_null($this->table_row_parameters)) $tableBox_string .= ' ' . $this->table_row_parameters;
if (isset($contents[$i]['params']) && tep_not_null($contents[$i]['params'])) $tableBox_string .= ' ' . $contents[$i]['params'];
$tableBox_string .= '>' . "\n";
if (isset($contents[$i][0]) && is_array($contents[$i][0])) {
for ($x=0, $y=sizeof($contents[$i]); $x<$y; $x++) {
if (isset($contents[$i][$x]['text']) && tep_not_null($contents[$i][$x]['text'])) {
$tableBox_string .= ' <td';
if (isset($contents[$i][$x]['align']) && tep_not_null($contents[$i][$x]['align'])) $tableBox_string .= ' align="' . $contents[$i][$x]['align'] . '"';
if (isset($contents[$i][$x]['params']) && tep_not_null($contents[$i][$x]['params'])) {
$tableBox_string .= ' ' . $contents[$i][$x]['params'];
} elseif (tep_not_null($this->table_data_parameters)) {
$tableBox_string .= ' ' . $this->table_data_parameters;
}
$tableBox_string .= '>';
if (isset($contents[$i][$x]['form']) && tep_not_null($contents[$i][$x]['form'])) $tableBox_string .= $contents[$i][$x]['form'];
$tableBox_string .= $contents[$i][$x]['text'];
if (isset($contents[$i][$x]['form']) && tep_not_null($contents[$i][$x]['form'])) $tableBox_string .= '</form>';
$tableBox_string .= '</td>' . "\n";
}
}
} else {
$tableBox_string .= ' <td';
if (isset($contents[$i]['align']) && tep_not_null($contents[$i]['align'])) $tableBox_string .= ' align="' . $contents[$i]['align'] . '"';
if (isset($contents[$i]['params']) && tep_not_null($contents[$i]['params'])) {
$tableBox_string .= ' ' . $contents[$i]['params'];
} elseif (tep_not_null($this->table_data_parameters)) {
$tableBox_string .= ' ' . $this->table_data_parameters;
}
$tableBox_string .= '>' . $contents[$i]['text'] . '</td>' . "\n";
}
$tableBox_string .= ' </tr>' . "\n";
}
$tableBox_string .= '</table>' . "\n";
if ($form_set == true) $tableBox_string .= '</form>' . "\n";
return $tableBox_string;
}
}
This is the extended class:
class box extends tableBlock {
function __construct() {
parent::__construct(); //calling parent constructor
$this->heading = array();
$this->contents = array();
}
function menuBox($heading, $contents) {
global $menu_dhtml; // add for dhtml_menu
if ($menu_dhtml == false ) { // add for dhtml_menu
$this->table_data_parameters = 'class="menuBoxHeading"';
if ($heading[0]['link']) {
$this->table_data_parameters .= ' onmouseover="this.style.cursor=\'hand\'" onclick="document.location.href=\'' . $heading[0]['link'] . '\'"';
$heading[0]['text'] = ' ' . $heading[0]['text'] . ' ';
} else {
$heading[0]['text'] = ' ' . $heading[0]['text'] . ' ';
}
$this->heading = $this->tableBlock($heading);
$this->table_data_parameters = 'class="menuBoxContent"';
$this->contents = $this->tableBlock($contents);
return $this->heading . $this->contents . $dhtml_contents;
// ## add for dhtml_menu
} else {
$selected = substr(strrchr ($heading[0]['link'], '='), 1);
$dhtml_contents = $contents[0]['text'];
$change_style = array ('<br>'=>' ','<BR>'=>' ', 'a href='=> 'a class="menuItem" href=','class="menuBoxContentLink"'=>' ');
$dhtml_contents = strtr($dhtml_contents,$change_style);
$dhtml_contents = '<div id="'.$selected.'Menu" class="menu" onmouseover="menuMouseover(event)">'. $dhtml_contents . '</div>';
return $dhtml_contents;
}
// ## eof add for dhtml_menu
}
}
Now call the parent logic with $this->contents = parent::tableBlock($contents);
__construct never return value, you should use method instead of return value in construct.
i have this variable: $this->{$prefix . '_total'} that can equal any number depending on the case but for the time being lets say $this->{$prefix . '_total'} = 4000.
So i use $this->{$prefix . '_total'} in order to get 4,000.
However, i dont know why but i get 4 instead.
Whats happening to the other zeros?? I tried with different values such as 1564.13 for example and in all cases the output its only the very first number. So i tried it like this:
number_format(($this->{$prefix . '_total'}*100000))
and it doesnt work either! I still get only the first digit. Why?? This blows my mind at so many levels. Please help.
Thank you.
Full function:
function render($indent = "", InvoicePayment $payment = null)
{
$prefix = (!is_null($payment) && !$payment->isFirst()) ? 'second' : 'first';
$tm_added = is_null($payment) ? $this->tm_added : $payment->dattm;
$newline = "\r\n";
$price_width = max(mb_strlen(Am_Currency::render($this->{$prefix . '_total'}, $this->currency)), 8);
$column_padding = 1;
$column_title_max = 60;
$column_title_min = 20;
$column_qty = 4 + $price_width;
$column_num = 3;
$column_amount = $price_width;
$space = str_repeat(' ', $column_padding);
$max_length = 0;
foreach ($this->getItems() as $item) {
$max_length = max(mb_strlen(___($item->item_title)), $max_length);
}
$column_title = max(min($max_length, $column_title_max), $column_title_min);
$row_width = $column_num + $column_padding +
$column_title + $column_padding +
$column_qty + $column_padding +
$column_amount + $column_padding;
$column_total = $column_title +
$column_qty + $column_padding;
$total_space = str_repeat(' ', $column_padding + $column_num + $column_padding);
$border = $indent . str_repeat('-', $row_width) . "$newline";
$out = $indent . ___("Invoice") . ' #' . $this->public_id . " / " . amDate($tm_added) . "$newline";
$out .= $border;
$num = 1;
foreach ($this->getItems() as $item) {
$title = explode("\n", $this->wordWrap(___($item->item_title), $column_title, "\n", true));
$out .= $indent . sprintf("{$space}%{$column_num}s{$space}%-{$column_title}s{$space}%{$column_qty}s{$space}%{$price_width}s$newline",
$num . '.', $title[0], $item->qty . 'x' . Am_Currency::render($item->{$prefix . '_price'}, $this->currency), Am_Currency::render($item->{$prefix . '_total'}, $this->currency));
for ($i=1; $i<count($title); $i++)
$out .= $indent . sprintf("{$space}%{$column_num}s{$space}%-{$column_title}s$newline", ' ', $title[$i]);
$num++;
}
$out .= $border;
if ($this->{$prefix . '_subtotal'} != $this->{$prefix . '_total'})
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Subtotal'), Am_Currency::render($this->{$prefix . '_subtotal'}, $this->currency));
if ($this->{$prefix . '_discount'} > 0)
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Discount'), Am_Currency::render($this->{$prefix . '_discount'}, $this->currency));
if ($this->{$prefix . '_shipping'} > 0)
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Shipping'), Am_Currency::render($this->{$prefix . '_shipping'}, $this->currency));
if ($this->{$prefix . '_tax'} > 0)
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Tax'), Am_Currency::render(number_format($this->{$prefix . '_tax'}), $this->currency));
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Total'), Am_Currency::render($this->{$prefix . '_total'}, $this->currency));
$out .= $border;
if ($this->rebill_times) {
$terms = explode("\n", $this->wordWrap(___($this->getTerms()), $row_width, "\n", true));
foreach ($terms as $term_part)
$out .= $indent . $term_part . $newline;
$out .= $border;
}
return $out;
}
And this is the render function
static function render($value, $currency = null, $locale = null)
{
return (string)self::create($value, $currency, $locale);
}
public function __toString()
{
$format = array_key_exists($this->currency, $this->formats) ?
$this->formats[$this->currency] :
'%.2f %s';
return sprintf($format, $this->value, $this->currency);
}
Try this
<?php
echo number_format($number,0,'',',');
?>
Stabbing into the dark here to explain the phenomenon:
echo (int)number_format(1000);
This results in "1". Because number_format produces "1,000", and (int) tries to parse it back into an integer, and since "," is not a valid part of an integer specification that's where it stops and returns only "1". See http://php.net/manual/en/language.types.string.php#language.types.string.conversion.
Something like this must be happening in your Am_Currency::render.
The past couple of hours I am trying to generate an xml file like this
<?xml version="1.0" encoding="UTF-8"?>
<mywebstore>
<created_at>2010-04-08 12:32</created_at>
<products>
<product>
<id>322233</id>
<name><![CDATA[MadBiker 600 Black Polarized]]></name>
<link><![CDATA[http://www.mywebstore.gr/product/322233]]></link>
<image><![CDATA[http://www.mywebstore.gr/product/322233.jpg]]></image>
<category id="23"><![CDATA[Sports > Extreme Sports]]></category>
<price_with_vat>322.33</price_with_vat>
<manufacturer><![CDATA[SuperGlasses]]></manufacturer>
<description><![CDATA[This is the description.....]]></description>
<weight>350</weight>
<mpn>ZHD332</mpn>
<instock>N</instock>
<availability>Pre-order</availability>
</product>
<product>
...
</product>
</products>
</mywebstore>
from opencart.
I have written this piece of code
<?php
class ControllerFeedSkroutzXml extends Controller {
public function index() {
$this->language->load('feed/skroutz_xml');
if ($this->config->get('skroutz_xml_status')) {
$output = '<?xml version="1.0" encoding="UTF-8"?>';
$output .= '<mywebstore>';
$output .= '<created_at>' . date('Y-m-d H:i') . '</created_at>';
$output .= '<products>';
$this->load->model('catalog/product');
$products = $this->model_catalog_product->getProducts();
foreach ($products as $product) {
$attribute_groups = $this->model_catalog_product->getProductAttributes($product['product_id']);
//print_r($attribute_groups);
if (!empty($attribute_groups)) {
foreach ($attribute_groups as $attribute_group) {
if (!empty($attribute_group)) {
foreach ($attribute_group['attribute'] as $attribute) {
$attribute = array_filter($attribute);
if (!empty($attribute)) {
// [attribute_id] => 13, Color
if ($attribute['attribute_id'] == 13 && $attribute['text'] != '') {
$attribute_color = $attribute['text'];
}
// [attribute_id] => 16, Lens Technology
if ($attribute['attribute_id'] == 16 && $attribute['text'] != '') {
$attribute_lens_technology = $attribute['text'];
}
}
}
}
}
}
if ($product['special']) {
$final_price = number_format((float)$product['special'], 2, '.', '');
} else {
$final_price = number_format((float)$product['price'], 2, '.', '');
}
if ($product['quantity'] > 0) {
$instock = $this->language->get('instock_Y');
} else {
$instock = $this->language->get('instock_N');
}
$output .= '<product>';
$output .= '<id>' . $product['product_id'] . '</id>';
$output .= '<name><![CDATA[' . $this->language->get('category_name') . ' ' . $product['name'] . ' ' . $attribute_color . ' ' . $attribute_lens_technology . ']]></name>';
$output .= '<link><![CDATA[' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . ']]></link>';
$output .= '<image><![CDATA['. HTTP_IMAGE . $product['image'] . ']]></image>';
$output .= '<category id="' . $product['manufacturer_id'] . '"><![CDATA[ ' . $this->language->get('category_name') . ' > ' . $product['manufacturer'] . ' ]]></category>';
$output .= '<price_with_vat>' . $final_price . '</price_with_vat>';
$output .= '<manufacturer><![CDATA[' . $product['manufacturer'] . ']]></manufacturer>';
$output .= '<description><![CDATA[' . $product['meta_description'] . ']]></description>';
$output .= '<instock>' . $instock . '</instock>';
$output .= '<availability>' . $product['stock_status'] . '</availability>';
$output .= '</product>';
}
$output .= '</products>';
$output .= '</mywebstore>';
$this->response->addHeader('Content-Type: application/xml');
$this->response->setOutput($output);
}
}
}
?>
But the block of code that generates the attributes it doesn't work as expected.
A lot of my products don't have attributes (at least not yet), so what I want to accomplish is to show attributes right next to the name of the product
Example
Name: MadBiker 600
Attribute - Color: Black
Attribute - Lens Technology : Polarized
All together <name>MadBiker 600 Black Polarized</name>
Only if a product has attributes!
The above php code generates the <name>MadBiker 600 Black Polarized</name> to all empty of attributes products until it finds the next product with an attribute!
Could someone please point out where is the problem?
Thank you!
You aren't resetting the $attribute_lens_technology and $attribute_color with each iteration of the foreach. You need to reset these after the foreach loop definition
New foreach loop:
foreach ($products as $product) {
$attribute_lens_technology = false;
$attribute_color = false;
$attribute_groups = $this->model_catalog_product->getProductAttributes($product['product_id']);
//print_r($attribute_groups);
if (!empty($attribute_groups)) {
foreach ($attribute_groups as $attribute_group) {
if (!empty($attribute_group)) {
foreach ($attribute_group['attribute'] as $attribute) {
$attribute = array_filter($attribute);
if (!empty($attribute)) {
// [attribute_id] => 13, Color
if ($attribute['attribute_id'] == 13 && $attribute['text'] != '') {
$attribute_color = $attribute['text'];
}
// [attribute_id] => 16, Lens Technology
if ($attribute['attribute_id'] == 16 && $attribute['text'] != '') {
$attribute_lens_technology = $attribute['text'];
}
}
}
}
}
}
if ($attribute_lens_technology === false || $attribute_color === false) {
// Code here such as continue; if you want to skip products without both attributes
}
if ($product['special']) {
$final_price = number_format((float)$product['special'], 2, '.', '');
} else {
$final_price = number_format((float)$product['price'], 2, '.', '');
}
if ($product['quantity'] > 0) {
$instock = $this->language->get('instock_Y');
} else {
$instock = $this->language->get('instock_N');
}
$output .= '<product>';
$output .= '<id>' . $product['product_id'] . '</id>';
$output .= '<name><![CDATA[' . $this->language->get('category_name') . ' ' . $product['name'] . ' ' . $attribute_color . ' ' . $attribute_lens_technology . ']]></name>';
$output .= '<link><![CDATA[' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . ']]></link>';
$output .= '<image><![CDATA['. HTTP_IMAGE . $product['image'] . ']]></image>';
$output .= '<category id="' . $product['manufacturer_id'] . '"><![CDATA[ ' . $this->language->get('category_name') . ' > ' . $product['manufacturer'] . ' ]]></category>';
$output .= '<price_with_vat>' . $final_price . '</price_with_vat>';
$output .= '<manufacturer><![CDATA[' . $product['manufacturer'] . ']]></manufacturer>';
$output .= '<description><![CDATA[' . $product['meta_description'] . ']]></description>';
$output .= '<instock>' . $instock . '</instock>';
$output .= '<availability>' . $product['stock_status'] . '</availability>';
$output .= '</product>';
}
It's easier to write an xml file using simplexml than it is to manually try and output your own.
Nevertheless, here's a simple shorthand if statement to fix to your problem though (if attribute color is empty, it will append an empty string instead:
$output .= !empty($attribute_color) ? '<name><![CDATA[' . $this->language->get('category_name') . ' ' . $product['name'] . ' ' . $attribute_color . ' ' . $attribute_lens_technology . ']]></name>' : '';
I what to put a span element for $term['nodes']
I have tried to put after bracket and between but nothing works for me
if (isset($term['nodes'])) {
$term['name'] = $term['name'] . ' (' . $term['nodes'] . ')';
}
here is the all functin
function bootstrap_taxonomy_menu_block($variables) {
$tree = $variables['items'];
$config = $variables['config'];
$num_items = count($tree);
$i = 0;
$output = '<ul class="nav nav-pills nav-stacked">';
foreach ($tree as $tid => $term) {
$i++;
// Add classes.
$attributes = array();
if ($i == 1) {
$attributes['class'][] = '';
}
if ($i == $num_items) {
$attributes['class'][] = '';
}
if ($term['active_trail'] == '1') {
$attributes['class'][] = 'active-trail';
}
if ($term['active_trail'] == '2') {
$attributes['class'][] = 'active';
}
// Alter link text if we have to display the nodes attached.
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
}
// Set alias option to true so we don't have to query for the alias every
// time, as this is cached anyway.
$output .= '<li' . drupal_attributes($attributes) . '>' . l($term['name'], $term['path'], $options = array('alias' => TRUE));
if (!empty($term['children'])) {
$output .= theme('taxonomy_menu_block__' . $config['delta'], (array('items' => $term['children'], 'config' => $config)));
}
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
i what this for the bootstrap cdn class , i have move the function on template.php , of drupal theme , but the span element is in plain text in browser
Try this:
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
echo $term['name']; // To see the output
}
i have a function like this. i want reset the $output var first call.
function create_tree($tree_array, $reset = TRUE, $ul_class = FALSE) {
if($reset) unset($output); // NOT WORK!!!
static $output = '';
$class = '';
if ($ul_class) {
$class = ' class="' . $ul_class . '"';
}
$output .= '<ul' . $class . '>' . PHP_EOL;
foreach ($tree_array as $v) {
$output .= '<li>' . $v['name'] . '' . PHP_EOL;;
if (isset($v['children'])) {
create_tree($v['children'], false);
}
$output .= '</li>' . PHP_EOL;
}
$output .= '</ul>' . PHP_EOL;
return $output;
}
$output doesn't magically exist at that point in the function; it magically retains its value when the declaration is seen again.
if ($reset)
$output = '';