Nishith Nesdiya Posted July 12, 2016 Share Posted July 12, 2016 hi...i am assign the the variable in java scripts like as <script type="text/javascript"> var my_var = "{$my_var1}"; </script> here $my_var1 is result set of database array then its give me the. PHP Notice: Array to string conversion in how can i assign this result set in java script varibalethanks Link to comment Share on other sites More sharing options...
Nishith Nesdiya Posted July 12, 2016 Author Share Posted July 12, 2016 (edited) thank you for your response My code is something like this public function hookHeader() { $my_var1 = Db::getInstance()->ExecuteS('SELECT * FROM `'._DB_PREFIX_.'mymodule`'); $this->context->smarty->assign(array( 'my_var1' => $my_var1, )); return $this->context->smarty->fetch($this->local_path.'views/templates/hook/header.tpl'); } $my_bar1 return this array Array ( [0] => Array ( [id_mymodule] => 1 [value1] => value1 [value2] => value2 [value3] => value3 [value4] => value4 [value5] => value5 [date_add] => 2016-07-12 08:14:33 [date_upd] => 2016-07-12 08:14:33 ) [1] => Array ( [id_mymodule] => 2 [value1] => value1 [value2] => value2 [value3] => value3 [value4] => value4 [value5] => value5 [date_add] => 2016-07-12 08:15:04 [date_upd] => 2016-07-12 08:15:04 ) ) then after i assign the header.tpl file like this <script type="text/javascript">var my_var = "{$my_var1}";</script> after its give me error PHP Notice: Array to string conversion in thanks Edited March 23, 2017 by Antoine F (see edit history) Link to comment Share on other sites More sharing options...
roja45 Posted July 12, 2016 Share Posted July 12, 2016 Use JSON; encode it in the php, assign it to your variable, decode it in your js. 1 Link to comment Share on other sites More sharing options...
Nishith Nesdiya Posted July 14, 2016 Author Share Posted July 14, 2016 Hi....Thank you for both of reply i using this code an thanks for 'Enrique Engblom' idia for using code. <script type="text/javascript"> var my_var = [ {foreach from=$my_var1 item=var name=my_var1} { id_mymodule: {$var.id_mymodule}, value1: '{$var.value1}', value2: '{$var.value2}', value3: '{$var.value3}', value4: '{$var.value4}', value5: '{$var.value5}', date_add: '{$var.date_add}', date_upd: '{$var.date_upd}' } {if !$smarty.foreach.my_var1.last},{/if} {/foreach} ]; </script> its perfect wrokingthanks 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now