Posts tagged: PHP Tips

Solution for PHP Smarty three columns

Example 1 Create a three-column table. (Display x items in one colum. For example, list 50 US states in three columns, that is, 17, 17 and 16 in each column.) <table border='0' cellpadding='0' cellspacing='0'> <tr> <td width='33%'> <{foreach name=states from=$data.states key=key item=state}> <{capture name='column'}><{math equation="x % 17" x=$smarty.foreach.states.index}><{/capture}> <{if (($smarty.capture.column == "0" ) && (($smarty.foreach.states.index != "0" )))}> </td><td width='32%' align='left'> <{/if}> <a href="<{$rootDir}>state.php?pid=<{$}>"><{$}></a><br /> <{/foreach}> </td> </tr> </table> Example 2 Create a three-column table. (Display each item in a cell and there are 3x times items.) <table border="0" cellpadding="0" cellspacing="0"> <tr> {foreach name=products from=$products item=product key=key} {capture name="column"}{math equation="x % 3" x=$key}{/capture} {if ($smarty.capture.column == "0")} </tr><tr> {/if} <td width="33%" align="left"> {$} </td> {/foreach} </tr> </table>

PHP - Single Quotes and Double Quotes

PHP allows you to define string values using single quotes or double quotes. The most significant difference between these two is that PHP parses a string enclosed in double quotations. Any variable in double quotes are expanded. Example: $var = 'tips'; echo 'Online Business' . $var; // Results in 'Online Business Tips' echo "Online Business $var"; // Results in 'Online Business Tips' echo 'Online Business $var'; // Results in 'Online Business $var' ($var in single quotes is not parsed.) There are a couple of tips to remember when deciding to use single or double quotations. 1. Using single quotations results in slightly faster execution of PHP scripts. 2. Consider ...