0

I am trying to create a string of values taken from variables in the backend with the following structure:

Before encoding:

transaction_id=0815/2009;transaction_cid=54AB;item_id=402163045080;item_va lue=25.20;item_quantity=1; 
transaction_id=0815/2009;transaction_cid=54AB;item_id=402163045080;item_va lue=25.20;item_quantity=1;

After encoding:

transaction_id%3D0815%2F2009%3Btransaction_cid%3D54AB%3Bitem_id%3D40216304 5080%3Bitem_value%3D25.20%3Bitem_quantity%3D1%3Bitem_id%3D847163029054%3Bi tem_value%3D16.81%3Bitem_quantity%3D2

I have managed to create an array with the necessary data in this form:

'[{"transaction_id":"233684","transaction_cid":"d2871c13c507583048d8ecf4a16f94c0","i tem_id":"3524","item_value":"4915.13","item_quantity":"1"}]',

But what I need is all these elements of the array in a url encoded string.

I am out of ideas since all that I try seems to not work.

Using JSON.stringify keeps the ":" and the """, using alert() or join also keeps the ":" and is not performant.

Example array:

arr : {key1: 'a', key2:'b', key3:'c'}

non encoded result:

str : 'key1=a;key2=b;key3=c'

desired result:

str : 'key1%3Da%3Bkey2%3Db%3Bkey3%3Dc'

Here is my code so far:

[{foreach from=$orderArticles item="currOrderArticle"}]
        [{assign var="currBasePrice2" value=$currOrderArticle->getBasePrice()}]

    products_info.push(
              {
              transaction_id: '[{$order->oxorder__oxordernr->value}]', 
              transaction_cid: '[{$order->oxorder__oxuserid->value}]', 
              item_id: '[{$currOrderArticle->oxorderarticles__oxartnum->value}]',
              item_value: '[{$basket->getDiscountedNettoPrice()}]',
              item_quantity: '[{$currOrderArticle->oxorderarticles__oxamount->value}]'
              });

    [{/foreach}]

Any ideas on how this can be accomplished?

3

1 Answer 1

1

You can combine json_encode (or serialize if you only need to use it in php) and escape:

{$arr|json_encode|escape:'url'}

Also, if you want to make the string shorter you can use compression:

{$arr|json_encode|gzcompress|base64_encode|escape:'html'}

Though that may be a bit overkill for short arrays and you'll have to base64_decode, gzuncompress and json_decode the string when you receive it.

Sign up to request clarification or add additional context in comments.

5 Comments

Hey Borgtex, thanks for your answer. Could you please detail me a bit on how the syntax should be knowing that I'm using smarty? Should I simply assign result : {$arr|json_encode|escape:'url'}? I'm using perhaps a wrong syntax and it is breaking my code
Don't know what smarty version are you using. You also seem to be using custom delimiters ('[{' instead of '{'). So, in smarty 3, if you want to assign it to a variable it would be [{$variable_name=$array_name|json_encode|escape:'url'}]
I think its a different version, the way I do the assignation is a simple "var : array" in the .push statement, but im not sure how to specify the treatment of the array to encode it and so on.
Ummmm I'm not sure what you're trying to do... the push statement is javascript, the code within [{ }] is Smarty, smarty is processed in the server and javascript in the browser. As for your question I supposed you wanted something like www.mypage.com/mycode.php?variable=encoded_array
No, I want to get the values from the backend in the form of an array, and then transform it into an encoded string and pass it into the datalayer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.