Posts Tagged ‘PHP’
20
Apr

Disk access is slow. Network access is slow. Databases typically use both.

Memory is fast. Using a local cache avoids the overhead of network and disk access. Combine these truths and you get memcached, a “distributed memory object caching system” originally developed for the Perl-based blogging platform LiveJournal.

If your application isn’t distributed across multiple servers, you probably don’t need memcached. Simpler caching approaches — serializing data and storing it in a temporary file, for example — can eliminate a lot of redundant work on each request. In fact, this is the sort of low-hanging fruit we consider when helping our clients tune their apps.

One of the easiest and most universal ways to cache data in memory is to use the shared memory helpers in APC, a caching system originally developed by our colleague George Schlossnagle. Consider the following example:

<?php
$feed = apc_fetch('news');

if ($feed === FALSE) {

$feed = file_get_contents('http://example.org/news.xml');

// Store this data in shared memory for five minutes.

apc_store('news', $feed, 300);

}

// Do something with $feed.

?>

With this type of caching, you don’t have to wait on a remote server to send the feed data for every request. Some latency is incurred — up to five minutes in this example — but this can be adjusted to as close to real time as your app requires.

,

30
Mar

We can randomly collect one or more element from an array by using array_rand() function in PHP. We can specify the number of random elements required by specifying an optional parameter in side the function.

$value= array("Rabin","Reid","Cris","KVJ","John");

$rand_keys=array_rand($value,2);
echo "First random element = ".$value[$rand_keys[0]];
echo "<br>Second random element = ".$value[$rand_keys[1]];

30
Mar
<?php

$to = "recipient@example.com";

$subject = "Hi!";

$body = "Hi,\n\nHow are you?";

if (mail($to, $subject, $body)) {

echo("<p>Message successfully sent!</p>");

} else {

echo("<p>Message delivery failed...</p>");

}

?>

30
Oct

getMessage() 

gets the exception’s message

 
 

getCode(

returns a numerical code that represents the exception

 
 

getFile()

returns the file where the exception was thrown

 
 

getLine() 

returns the line number in the file where the exception was thrown

 
 

getTrace()

returns the an array of the backtrace() before the exception was thrown

 
 

getPrevious() 

returns the exception thrown before the current one, if any

 
 

getTraceAsString()

returns the backtrace() of an exception as a string instead of an array

 
 

__toString() 

returns the whole exception as a string. Is overrideable.

 
 

 
 

, , , , , , , , ,

19
Oct

A php custom function to check the argument is number or string:

function is_number_or_string($val){

if( ((int) $val + 0 ) > 0 ){
return "Number";
}else{
return "String";
}
}

, , , ,

03
Oct

Assuming you know how to create a custom module, consider a case where you need to create a URL for your module. Follow these steps:

In your module’s config.xml file:

<modules>
.....
</modules>
<global>
....
</global>
<frontend>
	<routers>
		<schurl> --> name that will used in the URL
			<use>standard</use>  ---> standard means its a frontend url; admin will mean it is a backend url
			<args>
				<module>Mage_Schogini</module>  ---> which is the module to be used
				<frontName>schurl</frontName>   ---> name that will used in the URL
			</args>
		</schurl>
	</routers>
</frontend>
<default>
.....
</default>

Create a controller file TestController.php in your module’s controller folder like this (as you may have guessed, my modules name is Schogini):

class Mage_Schogini_TestController extends Mage_Core_Controller_Front_Action
{
	public function showmsgAction()
	{
		echo 'Here';
	}
}

This is what happens when you browse this URL
http://mymagentostore.com/schurl/test/showmsg/

  • schurl tells that the controller to check is Mage_Schogini (the name that is specified in the config file in the section: args > module)
  • test tells that controllers/TestController.php file must be checked
  • showmsg tells that showmsgAction() method must be called.

Hence, it will look for the method showmsg() inside the Mage_Schogini_TestController class

, , , , , , , , , , , , , ,

26
Aug

You need to explicitly specify the x-template for the mask. So, your form definition should look something like this:

{
	xtype: 'form',
	// TEMPLATE OF THE MASK
	waitTpl: '<div class="x-mask-loading"><div class="x-loading-spinner"><span class="x-loading-top"></span><span class="x-loading-right"></span><span class="x-loading-bottom"></span><span class="x-loading-left"></span></div><div class="x-loading-msg">{message}</div></div>'),
	items: [{....]
}

And the message you want to show you can decide in the form submit button handler where you will call the form submit like this:

myFrm.submit({
	url: 'somescript.php',
	method: 'POST',
	waitMsg: 'Processing...', ---> MESSAGE TO DISPLAY IN MASK
	success: function (frm, res)  {
		alert('Form submitted');
	},
	failure: function (frm, res) {
		alert('Form submit error');
	}
});

, , , , , , , , , , , , , ,

29
Jul

The zip_entry_open() function opens a zip archive entry for reading. This function returns TRUE on success, or FALSE on failure.

syntax:zip_entry_open(zip,zip_entry,mode)

zip –> Required. Specifies the zip resource to read (a zip file opened with zip_open() )

zip_entry –>Required. Specifies the zip entry resource to open (a zip entry opened with zip_read() )

mode–>Optional. Specifies the type of access you require to the zip archive.

<?php

$zip = zip_open(“test.zip”);

if ($zip)

{

while ($zip_entry = zip_read($zip))

{

echo “<p>”;

echo “Name: ” . zip_entry_name($zip_entry) . “<br />”;

if (zip_entry_open($zip, $zip_entry))

{

// some code

}

echo “</p>”;

}

zip_close($zip);

}

?>

, , , , , , , , , , , , , ,

29
Jul

There are several options:

imagecopyresampled() using GD
Imagick::resizeImage() using ImageMagick

,

25
Jul

Here is the code for a simple form submit in Sencha Touch:

var myForm = new Ext.Panel({
	layout: 'card',
	dockedItems: [{
		xtype: 'toolbar',
		docked: 'top',
		title: 'Test Form'
	}],
	items: [{
		xtype: 'form',
		title: 'My Form',
		id: 'register',
		items: [{
			xtype: 'fieldset',
			title: 'Personal Info',
			instructions: 'Some instruction for personal info',
			defaults: {
				labelWidth: '40%'
			},
			items: [{
				xtype: 'datepickerfield',
				name: 'dob',
				label: 'Date of birth',
				required: true,
				useClearIcon: true,
				picker: {yearFrom: 1900}
			}, {
				xtype: 'selectfield',
				name: 'gender',
				label: 'Gender',
				required: true,
				useClearIcon: true,
				options: [{
					text: 'Male',
					value: 'male'
				}, {
					text: 'Female',
					value: 'female'
				}]
			}, {
				xtype: 'textfield',
				name: 'phone_number',
				label: 'Phone number',
				placeHolder: 'Enter..',
				required: false,
				useClearIcon: true,
			}]
		},
		{
			xtype: 'fieldset',
			title: 'Login Info',
			instructions: 'Some instruction for login info',
			defaults: {
				labelWidth: '40%'
			},
			items: [{
				xtype: 'emailfield',
				name: 'email',
				label: 'Email',
				placeHolder: 'me@abc.com',
				required: true,
				useClearIcon: true,
			}, {
				xtype: 'passwordfield',
				name: 'password',
				label: 'Password',
				required: true,
				useClearIcon: true,
			}, {
				xtype: 'passwordfield',
				name: 'repeat_password',
				label: 'Re-type password',
				required: true,
				useClearIcon: true,
			}]
		}, {
			xtype: 'button',
			text: 'Register',
			scope: this,
			handler: function (b, e) {
				var form = Ext.getCmp('register');
				form.submit({
					url: 'testform.php',
					method: 'POST',
					success: function (frm, res)  {
						alert('Form submitted: ' + res.customfield);
					},
					failure: function (frm, res) {
						alert('Form no submit!');
					}
				});
			}
		}
	}]
});

 

This is testform.php:
It is very important to send success => true
If, success is not defined or not equal to true then it will be considered as a form submit error.

<?php
$response = array('success' => true, 'customfield' => 'custom value');

echo json_encode($response);

?>

, , , , , , , , , , , , , ,