PHP Coding Tips

Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Monday, August 20, 2012

PHP Store variable in a session

As you propably know, HTTP protocol is stateless, that means that each request run php parser and doesn't know about variables from previous page displayed.You can mantain variable values betweend numerous requests of the same user by storing this varaibles in session. To do that, you just have to call session_start() to start a session (a ID of session will be stored in browser cookies) and add a variable to $_SESSION array.See example below:session_start(); $_SESSION['myvalue'] = 5; Now, if you get $_SESSION['myvalue'] on other user request, you will get value of 5.

Wednesday, August 8, 2012

How to pass value to included smarty template

You can pass a variable value to a smarty template while including it. Just type name of a variable as parameter and value in quotas:{include file="fielderror.tpl" fieldname="albums"}Now in fielderror.tpl you can access this variable just by {$albums}

Tuesday, August 7, 2012

print_r in smarty - How to print your array contents in smarty template.

You can print a array contents like in php print_r() function in smarty tempalte. The common mistake is to forget about second print_r parametr for returning it's output. This parameter have to be set to true. If you forget to set it to true, your array contents propably will be somewhere at begining of a content. This is the solution:
{$myArray|@print_r:true}
Notice the @ symbol that provides parsing argument as array, not as string.

Smarty variable value in quotation mark

if you want to insert smarty variable value into string that is already in smarty "variable" (function call), just append it with `......` like shown in this example:{$obj->myMethod("This is my value: `$value`")}Usefull for owne link-preprocessor/rewriter