raintpl3 icon indicating copy to clipboard operation
raintpl3 copied to clipboard

coverting rain variable coming from db

Open AhmedMElmetwally opened this issue 10 years ago • 11 comments

hey everybody

how can i convert variable coming from database ?

AhmedMElmetwally avatar Jun 17 '14 23:06 AhmedMElmetwally

Use PDO to get the query in array format and assign that variable into the template.

You can also use a DB wrapper such as RainFramework DB: http://www.rainframework.com/User-Guide/Library/DB/get_row/

On Tue, Jun 17, 2014 at 7:28 PM, Luffy-php [email protected] wrote:

hey everybody

how can i convert variable coming from database ?

— Reply to this email directly or view it on GitHub https://github.com/rainphp/raintpl3/issues/156.

feulf avatar Jun 22 '14 03:06 feulf

thx for ur reply , but i want to convert for example i have two columns in my mysql id , content content = {$site_name} i want to get the content and assign it , then assign the value of {$site_name}

AhmedMElmetwally avatar Jun 22 '14 16:06 AhmedMElmetwally

All you've to do is to get the value of $site_name from the database:

// I'm using RainFramework DB but you can use PDO or any other MySQL library $site_name = Db::getField("site_name", "SELECT site_name FROM your_table WHERE id=':id'", array('id'=>'site_name'));

// then you assign $site_name to your template: $t = new Rain\Tpl(); $t->assing('site_name', $site_name); $t->draw('your_template');

and into the template you simply use {$site_name}.

{$site_name}

On Sun, Jun 22, 2014 at 12:07 PM, Luffy-php [email protected] wrote:

thx for ur reply , but i want to convert for example i have two columns in my mysql id , content content = {$site_name} i want to get the content and assign it , then assign the value of {$site_name}

— Reply to this email directly or view it on GitHub https://github.com/rainphp/raintpl3/issues/156#issuecomment-46784974.

feulf avatar Jun 22 '14 17:06 feulf

screenhunter_01 jun 23 15 12 thanks rain i will explain , i dont want to assign value directly because i dont control the variable as i said the variable is stored in mysql i first fetch the column content , then assign the content in the content there is variable i want to complie variable coming from database , compile variable then compile variable coming from the compiled variable please take a shot http://s10.postimg.org/igp3ed9h5/Screen_Hunter_01_Jun_23_15_12.jpg

AhmedMElmetwally avatar Jun 23 '14 12:06 AhmedMElmetwally

use drawString then, here's an example: https://github.com/rainphp/raintpl3/blob/master/example-draw-string.php

On Mon, Jun 23, 2014 at 8:15 AM, Luffy-php [email protected] wrote:

thanks rain i will explain , i dont want to assign value directly because i dont control the variable as i said the variable is stored in mysql i first fetch the column content , then assign the content in the content there is variable i want to complie variable coming from database , compile variable then compile variable coming from the compiled variable please take a shot http://s10.postimg.org/igp3ed9h5/Screen_Hunter_01_Jun_23_15_12.jpg

— Reply to this email directly or view it on GitHub https://github.com/rainphp/raintpl3/issues/156#issuecomment-46836641.

feulf avatar Jun 23 '14 12:06 feulf

Thanks rain :+1: i will try it then comment

AhmedMElmetwally avatar Jun 23 '14 12:06 AhmedMElmetwally

I could not use drawString :( i am using SQL to save loops or if statement not only variable such as kit-pc02 jul 06 06 32 16 pm could you help plz

AhmedMElmetwally avatar Jul 06 '14 16:07 AhmedMElmetwally

It should work, drawString works like draw with the only difference that the templates is coming from a string. Please explain more in details what you want to do and I'll help you to find a better solution.

feulf avatar Jul 06 '14 16:07 feulf

Thanks a lot friend i want to do blocks system in my script the whole block will be saved in SQL table most of blocks will have template variables, if statements or loops i will extract all blocks from database and assigning it to array width PDO in my template i want to make it work $array = array( "name" => "register_block", "block_content" => "{if='$_SESSION[user] == allow'}do something{/if}" ); $tpl->assign( $array ); $tpl->draw( "blocks" ); i want {if='$_SESSION[user] == allow'}do something{/if} to be compiled in my template

AhmedMElmetwally avatar Jul 06 '14 20:07 AhmedMElmetwally

You're passing variables to the template, those variables are rendered as strings why they're not executed, the only way to execute that code is to parse it with drawString first:

$block_content = $this->drawString("your string here");

and then: $array = array("name" => "register_block", "block_content" => $register_block);

My recommendation is to move from your database the business logic and the presentation logic, into the controller and specific templates, keep your code, template and database as less entangled as possible and as simple as possible!

On Sun, Jul 6, 2014 at 4:29 PM, Luffy-php [email protected] wrote:

Thanks a lot friend i want to do blocks system in my script the whole block will be saved in SQL table most of blocks will have template variables, if statements or loops i will extract all blocks from database and assigning it to array width PDO in my template i want to make it work $array = array( "name" => "register_block", "block_content" => "{if='$_SESSION[user] == allow'}do something{/if}" ); $tpl->assign( $array ); $tpl->draw( "blocks" ); i want {if='$_SESSION[user] == allow'}do something{/if} to be compiled in my template

— Reply to this email directly or view it on GitHub https://github.com/rainphp/raintpl3/issues/156#issuecomment-48125661.

feulf avatar Jul 06 '14 21:07 feulf

Thank you so much :)

AhmedMElmetwally avatar Jul 06 '14 22:07 AhmedMElmetwally