Skip to main content

Posts

Showing posts with the label PHP

Get Specific Zone Time

Get Specific Zone DateTime from server time function getdateTimeByZone($serverZone , $userZone = 'UTC', $serverDateTime){     //$serverDateTime must be with time     $serverDateTime = date('Y-m-d H:i:s',strtotime($serverDateTime));     $schedule_date = new DateTime($serverDateTime, new DateTimeZone($serverZone));     $schedule_date->setTimeZone(new DateTimeZone($userZone));     $convertedTime =  $schedule_date->format('Y-m-d H:i:s');     return $convertedTime; } echo getdateTimeByZone('America/Los_Angeles' , 'Asia/Karachi', '2015/08/20 04:27 AM');

AJAX BANNED MODULES

In SugarCRM if you want to ban ajax only for particular modules then go to your config_override.php file and add this code $sugar_config['addAjaxBannedModules'][] = “KReports”; //disable ajaxgui for specific module in config override. Now you have done

SUGARCRM ACL ROLES

If you want to get roles for particular user one of the easiest way is Get user roles global $current_user; require_once(“modules/ACLRoles/ACLRole.php”); $acl_role_obj = new ACLRole(); $user_roles = $acl_role_obj->getUserRoles($current_user->id); // grab a list of the current user’s roles foreach ($user_roles as $role) { $role_to_check=$role; }

CONDITIONAL STATEMENTS

Conditional statement syntax is almost same as in other langauges If condition syntax if (condition) { //Code to let user access the site here; } if else statement syntax if(condition){ //code that should be executed if true }else{ //code that should be executed if condition false } ifelse statement syntax if(condition1){ //code }elseif(condition2){ //code } Switch statement in php switch(variable){ case ‘one’: //code break; case ‘two’: //code here break; default: //default code here break; } note: break statement is very important

LOOPS

for loop: for($start=0; $start < 11; $start++) { echo "for loop test";//text will be printed 11 times } while loop: while (condition) { //statement } do while loop: do statement while (condition) The difference is that your statement gets executed at least once. In a normal while loop, the condition could be met before your statement gets executed.

CONTINUE & BREAK

continue and break statements in php are very importants these are very usefull in loops if you want to skip at any point in loop and you want that after this condition below code should not be executed then use continue example: for($i=0;$i<10;$i++){ if($i==6) break;// loop will be terminated and after 6 it will be outside from loop scope echo "before continue"; continue; echo "after continue";//this will never be executed }

SORTING PHP ARRAY VALUES

There may be times when you want to sort the values inside of an array. For example, suppose your array values are not in alphabetical order. Like this one: $full_name = array(); $full_name["Roger"] = "Waters"; $full_name["Richard"] = "Wright"; $full_name["Nick"] = "Mason"; $full_name["David"] = "Gilmour"; To sort this array, you just use the assort( ) function. This involves nothing more complex than typing the word asort, followed by round brackets. In between the round brackets, type in the name of your Associative array: asort( $full_name ); The letter “a” tells PHP that the array is an Associative one. (If you don’t have the “a” before “sort”, your key names will turn in to numbers!). The “a” also tells PHP to sort by the Value, and NOT by the key. In our script above, the surnames will be sorted. If you want to sort using the Key, then you can use ksort() instead. If you have a Scalar ar...

FOR EACH LOOP AND ASSOCIATIVE ARRAY

You can iterate over an associative array using for each loop like below $arr=array("one"=>1,"two"=>2,"three"=>3); foreach ($arr as $key => $value) { print "Key = " . $key . " Value = " . $value . " "; } you can get array keys by using $keys=array_keys($arr); now $keys contain array of keys;//array(“one”,”two”,”three”);

HOW TO SEARCH AN ELEMENT IN MULTIDIMENTIONAL ARRAY

How to search in multidimensional array function in_multiarray($elem, $array,&$path) { foreach($array as $key => $value) { if($array[$key] == $elem){ $path[$key]=$key;return true; } else if(is_array($array[$key])){ $path[$key]=$key; if(in_multiarray($elem, ($array[$key]),$path)){ return true; } } } return false; } $path=array(); if(in_multiarray(1212,$arr,$path)){ echo "TRUE"; print_r($arr); }else{ echo "FALSE"; }

LOGGING IN PHP

Simple and easy way for logging in php First, install Apache log4php. create a configuration file named config.xml containing: 1.<configuration xmlns="http://logging.apache.org/log4php/"> 2.    <appender name="myAppender"> 3.        <param name="file" value="myLog.log" /> 4.    </appender> 5.    <root> 6.        <level value="WARN" /> 7.        <appender_ref ref="myAppender" /> 8.    </root> 9.</configuration> This configuration file does the following: line 2: Creates an appender named myAppender using appender class LoggerAppenderFile which is used for logging to a file. line 3: Sets the file parameter, which tells the appender to which file to write. line 6: Sets the root logger level to WARN. This means that logging requests with the level lower than WARN will ...

PHP DATABASE CONNECTION

//connecting to db $conn = mysql_connect("localhost","peter","abc123"); if (!$conn) { die('Could not connect: ' . mysql_error()); } // some code mysql_close($conn); // closing connection //query db $data=$conn->query("SELECT * FROM myTable "); foreach($data as $row) { print_r($row); }

Remove Index.php in CodeIgniter

If you are using  Codeigniter  you are noticed that by default  index.php  will be included with your  URL . But you can easily remove index.php from your CodeIgniter’s URL so that your URL should be like: impegnotech.com/site/contact To do this just follows the following steps: Open  config.php  from  system/application/config  directory and replace $config['index_page'] = “index.php”  by  $config['index_page'] = “” Create a  “.htaccess”  file in the root of CodeIgniter directory (where the system directory resides), open the file using your favorite text editor, write down the following script and save it: RewriteEngine on RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] In some case the default setting for  uri_protocol  does not work properly. To solve this problem just replace ...