php howto unset static var - php

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 = '';

Related

SP Page Builder php errors ( Undefined variable, Undefined property)

i installed a Joomla template and some errors are appearing in homepage, the Errors are related to the Sp Page Builder component.
How can i fix these errors?
Here's a document with the erros - https://drive.google.com/open?id=0B1toGflgmV7fZi1SQ051QWZxZFE
And the site.php
<?php
/**
* Flex 1.0 #package SP Page Builder
* Template Name - Flex
* #author Aplikko http://www.aplikko.com
* #copyright Copyright (c) 2015 Aplikko
* #license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
*/
// no direct access
defined('_JEXEC') or die;
JLoader::register('JHtmlString', JPATH_LIBRARIES.'/joomla/html/html/string.php');
AddonParser::addAddon('sp_latest_posts','sp_latest_posts_addon');
function get_categories($parent=1) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select('*')
->from($db->quoteName('#__categories'))
->where($db->quoteName('extension') . ' = ' . $db->quote('com_content'))
->where($db->quoteName('published') . ' = ' . $db->quote(1))
->where($db->quoteName('parent_id') . ' = ' . $db->quote($parent))
->order($db->quoteName('created_time') . ' DESC');
$db->setQuery($query);
$cats = $db->loadObjectList();
$categories = array($parent);
foreach ($cats as $key => $cat) {
$categories[] = $cat->id;
}
return $categories;
}
function sp_latest_posts_addon($atts){
extract(spAddonAtts(array(
"title" => '',
"heading_selector" => 'h3',
"title_fontsize" => '',
"title_text_color" => '',
"title_margin_top" => '',
"title_margin_bottom" => '',
"show_image" => '',
"show_date" => '',
"show_category" => '',
"show_intro_text" => '',
"show_author" => '',
"item_limit" => '',
"intro_text_limit" => '100',
"column_no" => '3',
"image_alignment" => '',
"category" => '',
"style" => '',
"class" => '',
), $atts));
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
// Database Query
require_once JPATH_SITE . '/components/com_content/helpers/route.php';
// Access filter
$access = !JComponentHelper::getParams('com_content')->get('show_noauth');
$authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select('a.*')
->from($db->quoteName('#__content', 'a'))
->select($db->quoteName('b.alias', 'category_alias'))
->select($db->quoteName('b.title', 'category'))
->join('LEFT', $db->quoteName('#__categories', 'b') . ' ON (' . $db->quoteName('a.catid') . ' = ' . $db->quoteName('b.id') . ')')
->where($db->quoteName('b.extension') . ' = ' . $db->quote('com_content'))
->where($db->quoteName('a.state') . ' = ' . $db->quote(1))
->where($db->quoteName('a.catid')." IN (" . implode( ',', get_categories($category) ) . ")")
->where($db->quoteName('a.access')." IN (" . implode( ',', $authorised ) . ")")
->order($db->quoteName('a.created') . ' DESC')
->setLimit($item_limit);
$db->setQuery($query);
$items = $db->loadObjectList();
// End Database Query
$style == 'flex' ? $flex_style = ' flex' : '';
$style == 'blog' ? $blog_style = ' blog' : '';
$blog_style = $output = '<div class="sppb-addon sppb-addon-latest-posts'.$flex_style.$blog_style.' sppb-row ' . $class . '">';
if ($title) {
$output .= '<div class="sppb-section-title">';
$output .= '<'.$heading_selector.' class="sppb-addon-title" style="' . $title_style . '"> ' . $title . '</' . $heading_selector . '>';
$output .= '</div>'; // END :: title
}
$output .= '<div class="sppb-addon-content">';
$output .= '<div class="latest-posts clearfix">';
foreach(array_chunk($items, $column_no) as $items) {
$output .= '<div>';
foreach ($items as $item) {
$item->slug = $item->id . ':' . $item->alias;
$item->catslug = $item->catid . ':' . $item->category_alias;
$item->user = JFactory::getUser($item->created_by)->name;
if ($access || in_array($item->access, $authorised)) {
// We know that user has the privilege to view the article
$item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catid, $item->language));
$item->catlink = JRoute::_(ContentHelperRoute::getCategoryRoute($item->catslug, $item->catid, $item->language));
} else {
$item->link = JRoute::_('index.php?option=com_users&view=login');
$item->catlink = JRoute::_('index.php?option=com_users&view=login');
}
$tplParams = JFactory::getApplication()->getTemplate(true)->params;
$params = $item->params;
$attribs = json_decode($item->attribs);
$images = json_decode($item->images);
$imgsize = $tplParams->get('blog_list_image', 'default');
$intro_image = '';
if(isset($attribs->spfeatured_image) && $attribs->spfeatured_image != '') {
if($imgsize == 'default') {
$intro_image = $attribs->spfeatured_image;
} else {
$intro_image = $attribs->spfeatured_image;
$basename = basename($intro_image);
$list_image = JPATH_ROOT . '/' . dirname($intro_image) . '/' . JFile::stripExt($basename) . '_'. $imgsize .'.' . JFile::getExt($basename);
if(file_exists($list_image)) {
$intro_image = JURI::root(true) . '/' . dirname($intro_image) . '/' . JFile::stripExt($basename) . '_'. $imgsize .'.' . JFile::getExt($basename);
}
}
} elseif(isset($images->image_intro) && !empty($images->image_intro)) {
$intro_image = $images->image_intro;
}
if($column_no == '1') {
if ($show_image) {
$image_alignment == 'left' ? $img_column = 'sppb-col-sm-4 column-1 pull-left match-height' : $img_column = 'sppb-col-sm-4 column-1 pull-right match-height';
}
if ($show_image) {
$image_alignment == 'right' ? $content_column = 'sppb-col-sm-8 column-1 pull-left match-height' : $content_column = 'sppb-col-sm-8 column-1 pull-right match-height';
} else {
$image_alignment == 'right' ? $content_column = 'sppb-col-sm-12 column-1' : $content_column = 'sppb-col-sm-12 column-1';
}
$h2style = ' style="font-size:180%;line-height:1.4;"';
$img_wrapper_margin = ' style="margin:0;"';
if ($image_alignment == 'left') {
$inner_padding = ' style="padding:0 0 0 30px;"';
} else {
$inner_padding = ' style="padding:0 30px 0 0;"';
}
}
// match-height
$column_no > '1' ? $match_height = ' match-height' : '';
// Flex Style
if($style == 'flex') {
$output .= '<div class="latest-post sppb-col-sm-' . round(12/$column_no) . ' columns-'.$column_no.'">';
$output .= '<div class="latest-post-item">';
if($column_no == '1') {
$output .= '<div class="row-fluid">';
}
if(!empty($intro_image) || (isset($images->image_intro) && !empty($images->image_intro))) {
if ($show_image) {
if($column_no == '1') {
$output .= '<div style="padding:0" class="'.$img_column.'">';
}
$output .= '<div class="img-wrapper">';
$output .= '<img class="post-img" src="' . $intro_image . '" alt="' . $item->title . '" /><div class="caption-content">' . $item->title . '<em class="caption-category"><span class="posted-in">'. JText::_('COM_SPPAGEBUILDER_ADDON_POSTED_IN') .'</span>'. $item->category . '</em></div>';
$output .= '</div>';
if($column_no == '1') {
$output .= '</div>';
}
}
}
if($column_no == '1') {
$output .= '<div'.$inner_padding.' class="'.$content_column.'">';
}
$output .= '<div class="latest-post-inner match-height">';
if (($show_date || $show_intro_text || $show_author) != 1) {
$output .= '<h2 style="margin:0" class="entry-title">' . $item->title . '</h2>';
} else {
$output .= '<h2'.$h2style.' class="entry-title">' . $item->title . '</h2>';
}
if ($show_date) {
$output .= '<div class="entry-meta"><span class="entry-date">' . JHtml::_('date', $item->created, 'DATE_FORMAT_LC1') . '</span></div>';
}
if ($show_intro_text) {
$output .= '<p class="intro-text" >' . JHtml::_('string.truncate', strip_tags($item->introtext), $intro_text_limit) . '</p>';
}
$show_author || $show_category ? $output .= '<hr />' : '';
if ($show_author) {
$output .= '<span class="post-author"><span class="entry-author">' . JText::_('COM_SPPAGEBUILDER_ADDON_POSTED_BY'). '</span> ' . $item->user . '</span>';
}
if ($show_category) {
$show_author ? $posted_in_category = ' cat-inline' : '';
$output .= '<span class="category'.$posted_in_category.'"><span class="posted-in">'. JText::_('COM_SPPAGEBUILDER_ADDON_CATEGORY') .'</span>'. $item->category . '</span>';
}
if($column_no == '1') {
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
if($column_no == '1') {
$output .= '<div class="post-divider"></div>';
}
$output .= '</div>';
// Default & Blog styles
} else {
$output .= '<div class="latest-post sppb-col-sm-' . round(12/$column_no) . ' columns-'.$column_no.'">';
$output .= '<div class="latest-post-inner' . $match_height . '">';
if($column_no == '1') {
$output .= '<div class="row-fluid">';
}
if ($show_image) {
if($column_no == '1') {
$output .= '<div class="'.$img_column.'">';
}
$output .= '<div'.$img_wrapper_margin.' class="img-wrapper">';
$output .= '<img class="post-img" src="' . $intro_image . '" alt="' . $item->title . '" />';
$output .= '</div>';
if($column_no == '1') {
$output .= '</div>';
}
}
if($column_no == '1') {
$output .= '<div class="'.$content_column.'">';
}
if ($show_date) {
$output .= '<div class="entry-meta"><span class="entry-date"> ' . JHtml::_('date', $item->created, 'DATE_FORMAT_LC1') . '</span></div>';
}
$output .= '<h2'.$h2style.' class="entry-title">' . $item->title . '</h2>';
if ($show_intro_text) {
$output .= '<p class="intro-text" >' . JHtml::_('string.truncate', strip_tags($item->introtext), $intro_text_limit) . '</p>';
}
$show_author || $show_category ? $output .= '<hr />' : '';
if ($show_author) {
$output .= '<span class="post-author"><span class="entry-author">' . JText::_('COM_SPPAGEBUILDER_ADDON_POSTED_BY'). ' ' . $item->user . '</span></span>';
}
if ($show_category) {
$show_author ? $posted_in_category = ' cat-inline' : '';
$output .= '<span class="category'.$posted_in_category.'"><span class="posted-in">'. JText::_('COM_SPPAGEBUILDER_ADDON_CATEGORY') .'</span>'. $item->category . '</span>';
}
if($column_no == '1') {
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
}
$output .= '</div>';
}
$output .= '</div>';
}
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$column_no == '1' ? $column_no_1 = '.column-1 {margin:10px auto;padding:0!important;}' : '';
// Add styles #media rulepost-img
if($style == 'flex') {
$custom_style = ''
. '#media screen and (max-width: 768px) {'
. $column_no_1
. '.img-wrapper a {font-size:150%;line-height:1.5;}'
. '}';
$doc->addStyleDeclaration($custom_style);
}
if ($column_no>='3') {
$custom_style_3 = ''
. '#media screen and (min-width: 992px) and (max-width: 1199px){'
. '.columns-'.$column_no.'{width:33.3333%;}'
. '}'
. '#media screen and (min-width: 768px) and (max-width: 991px){'
. '.columns-'.$column_no.'{width:50%}'
. '}';
$doc->addStyleDeclaration($custom_style_3);
}
if($column_no=='5') {
$custom_style_5 = ''
. '.columns-'.$column_no.' {width:20%}'
. '#media screen and (min-width: 992px) and (max-width: 1199px){'
. '.columns-'.$column_no.'{width:33.3333%;}'
. '}'
. '#media screen and (min-width: 768px) and (max-width: 991px){'
. '.columns-'.$column_no.'{width:50%}'
. '}'
. '#media screen and (max-width: 767px){'
. '.columns-'.$column_no.'{width:100%}'
. '}';
$doc->addStyleDeclaration($custom_style_5);
}
return $output;
}
Thanks!
There is many errors, but all of them are variable that have not been declared before using it, as example :
$style == 'flex' ? $flex_style = ' flex' : '';
$style == 'blog' ? $blog_style = ' blog' : '';
$blog_style = $output = '<div class="sppb-addon sppb-addon-latest-posts'.$flex_style.$blog_style.' sppb-row ' . $class . '">';
In this case $flex_style and $blog_style are not declared, you should write this instead :
$flex_style = style == 'flex' ? ' flex' : '';
$blog_style = $style == 'blog' ? ' blog' : '';
That is just an example, but if you search a little you'll find other issue like this one.

PHP extended class

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.

Associate attributes with products in opencart to generate xml file

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>' : '';

how can i make a span element in this line of php

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
}

How to generate .php files from .dll files for code completion?

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;

Categories