I have a page whit ads, and i set the page currency in "RON" and i convert to show also in "Euro" but in the loop is very slow.. I tried to include the script form other php but stil the same... I tried many currency changer but all have the same problem.. slow the page down.. and if i put the code directly in to the loop then tells me an error: that the class could not be repeated.
here is the php currency what i used:
<?php
class cursBnrXML
{
var $currency = array();
function cursBnrXML($url)
{
$this->xmlDocument = file_get_contents($url);
$this->parseXMLDocument();
}
function parseXMLDocument()
{
$xml = new SimpleXMLElement($this->xmlDocument);
$this->date=$xml->Header->PublishingDate;
foreach($xml->Body->Cube->Rate as $line)
{
$this->currency[]=array("name"=>$line["currency"], "value"=>$line, "multiplier"=>$line["multiplier"]);
}
}
function getCurs($currency)
{
foreach($this->currency as $line)
{
if($line["name"]==$currency)
{
return $line["value"];
}
}
return "Incorrect currency!";
}
}
//#an example of using the cursBnrXML class
$curs=new cursBnrXML("http://www.bnr.ro/nbrfxrates.xml");
?>
You can modify the cursBnrXML class to cache parsed currency so that you do not have to loop over entire collection again each look up.
<?php
class cursBnrXML
{
private $_currency = array();
# keep the constructor the same
# modify this method
function parseXMLDocument()
{
$xml = new SimpleXMLElement($this->xmlDocument);
$this->date=$xml->Header->PublishingDate;
foreach($xml->Body->Cube->Rate as $line)
{
$this->currency[$line["currency"]]=array(
"value"=>$line,
"multiplier"=>$line["multiplier"]
);
}
}
# modify this method too
function getCurs($currency)
{
if (isset($this->_currency[$currency]))
{
return $this->_currency[$currency]['value'];
}
return "Incorrect currency!";
}
}
//#an example of using the cursBnrXML class
$curs=new cursBnrXML("http://www.bnr.ro/nbrfxrates.xml");
?>
Related
I am using a translator class i found online. It works phenomenally when I use it to directly echo the message. The problem occurs when I do conditional checks at the beginning of the page and I need to have the translated text in the variable to then send it to other places on the page to be displayed.
My code:
if ($condition_1){
$message = $translate->__('Text 1');
}
elseif ($condition_2){
$message = $translate->__('Text 2');
}
elseif ($condition_3){
$message = $translate->__('Text 3');
}
This code echos the text in the place where this condition is put, not used as the variable $message and then echos when I need it to. Can you help me to figure out how to use the text as a variable.
If I use the text with no translator class. I can easily use it as a variable.
This is the class i use:
class Translator {
private $language = 'sl';
private $lang = array();
public function __construct($language){
$this->language = $language;
}
private function findString($str) {
if (array_key_exists($str, $this->lang[$this->language])) {
echo $this->lang[$this->language][$str];
return;
}
echo $str;
}
private function splitStrings($str) {
return explode('=',trim($str));
}
public function __($str) {
if (!array_key_exists($this->language, $this->lang)) {
if (file_exists($this->language.'.txt')) {
$strings = array_map(array($this,'splitStrings'),file($this->language.'.txt'));
foreach ($strings as $k => $v) {
$this->lang[$this->language][$v[0]] = $v[1];
}
return $this->findString($str);
}
else {
echo $str;
}
}
else {
return $this->findString($str);
}
}
}
The translated text is in the a *.txt file, looking like this:
text 1=text 1 translated
text 2=text 2 translated
text 3=text 3 translated
The problem was in the "echo" in the class. I changed the "echo" with "return" and it works like a charm!
I was trying to use com_event_sink, I came across a problem
Here is a example from php.net
<?php
class IEEventSinker {
var $terminated = false;
function ProgressChange($progress, $progressmax) {
echo "Download progress: $progress / $progressmax\n";
}
function DocumentComplete(&$dom, $url) {
echo "Document $url complete\n";
}
function OnQuit() {
echo "Quit!\n";
$this->terminated = true;
}
}
$ie = new COM("InternetExplorer.Application");
// note that you don't need the & for PHP 5!
$sink = new IEEventSinker();
com_event_sink($ie, $sink, "DWebBrowserEvents2");
$ie->Visible = true;
$ie->Navigate("http://www.example.org");
while(!$sink->terminated) {
com_message_pump(4000);
}
$ie = null;
?>
In the line
com_event_sink($ie, $sink, "DWebBrowserEvents2");
I know the interface used to perform event sink is "DWebBrowserEvents2".
My question is,
Is there anyway that I can view the interface of a COM object?
Since I only got an OCX file with no documentation, I have no idea how to find the interface. Thanks
I'm creating a new widget that allows custom text/HTML to be added to the page. I noticed that if you enter text containing double-quotes, the first occurrence of it and everything after gets cut off, so that you're missing data when you try to edit the widget again.
To make sure I didn't screw something up, I was able to verify this issue on a fresh install of Magento (1.7), by adding a stock widget -- Catalog Product Link -- to the page. If you set the Anchor Custom Text to something with double-quotes, insert, and edit again, you will see the text has been truncated.
I'm not sure where the problem occurs. The data is successfully written to the tinyMCE content element, but somehow gets malformed between there and an Ajax.Request call for the admin/widget/loadOptions route.
I found a related article here:
http://www.behrendt.io/2013/04/12/using-a-wysiwyg-editor-in-a-magento-widget/
The author mentions at the bottom a need for overriding a controller to use base64 encoding when transmitting data for widgets. This seems like it might work for me, but I wanted to be sure.
Here's a visual example of the problem I'm experiencing:
Anyone seen this before? Know where it comes from? How to fix? :) Thanks.
Looks like that article put me in the right direction, by overriding Mage_Widget_Adminhtml_WidgetController:
http://www.behrendt.io/2013/04/12/using-a-wysiwyg-editor-in-a-magento-widget/
I took his solution a step further and decided to encode ALL values when building the widget code:
# Namespace_Module_Adminhtml_WidgetController extends Mage_Widget_Adminhtml_WidgetController
public function buildWidgetAction()
{
$type = $this->getRequest()->getPost('widget_type');
$params = $this->getRequest()->getPost('parameters', array());
$asIs = $this->getRequest()->getPost('as_is');
if($type == 'namespace/module_widget')
{
foreach($params as $key => $value)
{
$params[$key] = base64_encode($value);
}
}
$html = Mage::getSingleton('widget/widget')->getWidgetDeclaration($type, $params, $asIs);
$this->getResponse()
->setBody($html);
}
This meant I also had to decode them when loading the widget for editing:
# Namespace_Module_Adminhtml_WidgetController extends Mage_Widget_Adminhtml_WidgetController
public function loadOptionsAction()
{
try {
$this->loadLayout('empty');
if( ($paramsJson = $this->getRequest()->getParam('widget')) )
{
$request = Mage::helper('core')->jsonDecode($paramsJson);
if(is_array($request))
{
$optionsBlock = $this->getLayout()->getBlock('wysiwyg_widget.options');
if(isset($request['widget_type']))
{
$optionsBlock->setWidgetType($request['widget_type']);
}
if(isset($request['values']))
{
if($optionsBlock->getWidgetType() == 'namespace/module_widget')
{
foreach($request['values'] as $key => $value)
{
$request['values'][$key] = base64_decode($value);
}
}
$optionsBlock->setWidgetValues($request['values']);
}
}
$this->renderLayout();
}
}
catch (Mage_Core_Exception $e)
{
$result = array('error' => true, 'message' => $e->getMessage());
$this->getResponse()
->setBody(Mage::helper('core')->jsonEncode($result));
}
}
And finally, in my widget block, I had to decode all data on-the-fly:
# Namespace_Module_Block_Widget
public function getData($key = '', $index = null)
{
if('' === $key)
{
$data = $this->_data;
foreach($data as $key => $value)
{
if(is_scalar($value))
{
$data[$key] = base64_decode($value);
}
}
}
else
{
$data = parent::getData($key, $value);
if(is_scalar($data))
{
$data = base64_decode($data);
}
}
return $data;
}
It would be nice if a similar encoding mechanism was part of core code.
The script works fine and is setting the data, but the website code is unable to use it and is instead setting its own memcached values. My website code is written in codeIgniter framework. I don't know why this is happening.
My script code :-
function getFromMemcached($string) {
$memcached_library = new Memcached();
$memcached_library->addServer('localhost', 11211);
$result = $memcached_library->get(md5($string));
return $result;
}
function setInMemcached($string,$result,$TTL = 1800) {
$memcached_library = new Memcached();
$memcached_library->addServer('localhost', 11211);
$memcached_library->set(md5($string),$result, $TTL);
}
/*---------- Function stores complete product page as one function call cache -----------------*/
function getCachedCompleteProduct($productId,$brand)
{
$result = array();
$result = getFromMemcached($productId." product page");
if(true==empty($result))
{
//------- REST CODE storing data in $result------
setInMemcached($productId." product page",$result,1800);
}
return $result;
}
Website Code :-
private function getFromMemcached($string) {
$result = $this->memcached_library->get(md5($string));
return $result;
}
private function setInMemcached($string,$result,$TTL = 1800) {
$this->memcached_library->add(md5($string),$result, $TTL);
}
/*---------- Function stores complete product page as one function call cache -----------------*/
public function getCachedCompleteProduct($productId,$brand)
{
$result = array();
$result = $this->getFromMemcached($productId." product page");
if(true==empty($result))
{
// ----------- Rest Code storing data in $result
$this->setInMemcached($productId." product page",$result,1800);
}
return $result;
}
This is saving data in memcached. I checked by printing inside the if condition and checking the final result
Based on the CodeIgniter docs, you can make use of:
class YourController extends CI_Controller() {
function __construct() {
$this->load->driver('cache');
}
private function getFromMemcached($key) {
$result = $this->cache->memcached->get(md5($key));
return $result;
}
private function setInMemcached($key, $value, $TTL = 1800) {
$this->cache->memcached->save(md5($key), $value, $TTL);
}
public function getCachedCompleteProduct($productId,$brand) {
$result = array();
$result = $this->getFromMemcached($productId." product page");
if( empty($result) ) {
// ----------- Rest Code storing data in $result
$this->setInMemcached($productId." product page",$result,1800);
}
return $result;
}
}
Personally try to avoid 3rd party libraries if it already exists in the core framework. And I have tested this, it's working superbly, so that should fix this for you :)
Just remember to follow the instructions at http://ellislab.com/codeigniter/user-guide/libraries/caching.html#memcached to set the config as needed for the memcache server
I am starting to write phpUnit test and faced with such problem. 80% of my functions ending on such lines
$data["res"] = $this->get_some_html($this->some_id);
echo my_json_encode($data);
return true;
How can i make test on such kind of functions in my classes?
You need to isolate your code into testable 'chunks'. You can test that the function returns TRUE/FALSE given specified text, and then test the JSON return data given fixed information.
function my_json_encode($data)
{
return ...;
}
function get_some_html($element)
{
return ...;
}
function element_exists($element)
{
return ..;
}
function display_data($element)
{
if(element_exists($element)
{
$data = get_some_html($element);
$json = my_json_encode($data);
return true;
}
else
{
return false;
}
}
Testing:
public function test_my_json_encode()
{
$this->assertEquals($expected_encoded_data, my_json_encode($text));
}
public function test_get_some_html()
{
$this->assertEquals($expected_html, get_some_html('ExistingElementId'));
}
public function test_element_exists()
{
$this->assertTrue(element_exists('ExistingElementId');
$this->assertFalse(element_exists('NonExistingElementId');
}
function test_display_data()
{
$this->assertTrue(display_data('ExistingElementId'));
$this->assertFalse(element_exists('NonExistingElementId');
}
This is a simple, abstract example of the changes and the testing. As the comments above have indicated, you might want to change the return to be the JSON text, and a FALSE on error, then use === testing in your code to decide to display the text or not.
The next step would be to mock out the Elements, so you can get expected data without the need for a real HTML page.