Archive

Posts Tagged ‘php’

PHP MYSQL Tips and Tricks

September 17th, 2009 R Arun Raj No comments

This is a small article containing tricks and tips I aquired by reading PHP 6 Text Book.

I was used isset() function to check whether these arguements are set or not. And also I need to check this is empty or not.

Here is another function to check both of this requirement at single line.

empty()

It checks if a variable has an empty value: an empty string, 0, NULL,or FALSE.

Example

if ( !empty($_POST[‘name’]) && !empty($_POST[‘comments’]) && !empty($_POST[‘email’]) ) {

echo “Yes”;

}

Categories: PHP, Programming Tags: , , ,

PHP:: How to split a large array in small pieces :: or split array into chunks

April 29th, 2009 R Arun Raj 1 comment

Hello All,
Yesterday i was searching for a solution to split large array into small pieces. Suddenly i noticed the that php have a built in function to split array into small pieces. It was very surprise to me ..
Here it is
Function : array array_chunk ( array $input , int $size [, bool $preserve_keys= false ] )

< ?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>

The output will be like this : –

Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[0] => c
[1] => d
)

[2] => Array
(
[0] => e
)

)
Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[2] => c
[3] => d
)

[2] => Array
(
[4] => e
)

)

Have fun Cheers

Categories: PHP, email Tags: , ,

PHP code to find Duration of an Audio File

March 18th, 2009 R Arun Raj 2 comments

Here is the PHP function to calculated duration of “wav” or “GSM” files

First we open file using fopen. After we calculate the size of that file (inbytes) using filesize().

Then we unpack the audio File .

unpack(‘vtype/vchannels/Vsamplerate/Vbytespersec/valignment/vbits’,$rawheader);

This will return following array

Array (
[type] => 1
[channels] => 2
[samplerate] => 44100
[bytespersec] => 176400
[alignment] => 4 [bits] => 16 )

from this array we get bytepersec.

Now it is very easy to find out the duration.. !!

$size_in_bytes/bytespersec.

Code is here.

public static function getDuration($file) {

$fp = fopen($file, ‘r’);
$size_in_bytes = filesize($file);
fseek($fp, 20);
$rawheader = fread($fp, 16);
$header = unpack(‘vtype/vchannels/Vsamplerate/Vbytespersec/valignment/vbits’,
$rawheader);
$sec = ceil($size_in_bytes/$header['bytespersec']);
return $sec;

}

Have fun.

Beta Version of Zend Application Server released

March 10th, 2009 R Arun Raj No comments

Zend Server

Zend Server is a commercial, enterprise-ready Web Application Server that is designed for running and managing business-critical PHP applications in production. It includes advanced performance optimization, application monitoring and application problem diagnostic capabilities.  Zend Server users receive comprehensive technical support, software updates, hot fixes, and security patches.

Packages for Linux,windows and Mac Avalable

Get a free trial license for Zend Server


Export Database to Excel or CSV File

August 29th, 2008 R Arun Raj 2 comments

The below code help you to export data from database to csv file

exporttoxls

To export a complete database, you should invoke all the tables via below code and apply the above code
$dbname = ‘mysql_dbname’;

if (!mysql_connect(‘mysql_host’, ‘mysql_user’, ‘mysql_password’)) {
echo ‘Could not connect to mysql’;
exit;
}

$sql = “SHOW TABLES FROM $dbname”;
$result = mysql_query($sql);

if (!$result) {
echo “DB Error, could not list tables\n”;
echo ‘MySQL Error: ‘ . mysql_error();
exit;
}

while ($row = mysql_fetch_row($result)) {
echo “Table: {$row[0]}\n”;
}

mysql_free_result($result);
?>

Categories: PHP, Programming Tags: , , , , ,

Simple Mail From PHP

August 3rd, 2008 R Arun Raj 1 comment

Simple Email Program

<?php
$to = ‘nobody@example.com’;
$subject = ‘the subject’;
$message = ‘hello’;
$headers = ‘From: webmaster@example.com’ . “\r\n” .
‘Reply-To: webmaster@example.com’ . “\r\n” .
‘X-Mailer: PHP/’ . phpversion();

mail($to, $subject, $message, $headers);
?>

Email With MIME Content

<?php
// multiple recipients
$to = ‘aidan@example.com’ . ‘, ‘; // note the comma
$to .= ‘wez@example.com’;

// subject
$subject = ‘Birthday Reminders for August’;

// message
$message = ‘
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
‘;

// To send HTML mail, the Content-type header must be set
$headers = ‘MIME-Version: 1.0′ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1′ . “\r\n”;

// Additional headers
$headers .= ‘To: Mary <mary@example.com>, Kelly <kelly@example.com>’ . “\r\n”;
$headers .= ‘From: Birthday Reminder <birthday@example.com>’ . “\r\n”;
$headers .= ‘Cc: birthdayarchive@example.com’ . “\r\n”;
$headers .= ‘Bcc: birthdaycheck@example.com’ . “\r\n”;

// Mail it
mail($to, $subject, $message, $headers);
?>

Categories: PHP, Programming Tags: , ,

How to call url from php using C-url :: alternative to file_get_contents()

July 29th, 2008 R Arun Raj 4 comments

This is a nice and simple substitute to get_file_contents() using curl, it returns FALSE if $contents is empty.

<?php
function curl_get_file_contents($URL)
{
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
curl_close($c);

if ($contents) return $contents;
else return
FALSE;
}
?>

Then another question

How to enable cURL in your server ?

To enable curl library we need to modify the php.ini file.

1) Locate the following files:
apache\bin\php.ini
php\php.ini
php\php4\php.ini or php\php5\php.ini

2) Uncomment the following line on your php.ini file by removing the semicolon.

;extension=php_curl.dll

3) Restart your apache server.

4) Check your phpinfo if curl was properly enabled.

Otherwise you can dynamically enable the library by calling dl(); function. But it is deprecated by new version of PHP

Project Management Tool in PHP under GPL:: DotProject

July 16th, 2008 R Arun Raj No comments

dotProject : the Open Source Project Management tool.

dotProject is built using free open-source applications and is produced and maintained by a small, but dedicated group of volunteers. dotProject is programmed in PHP, and utilises MySQL for a backend database (although other databases such as Postgres could also be used). Our recommended server platform includes Apache 1.3.27, PHP 4.2+, and MySQL. In the spirit of free, peer-reviewed, open source application development, we would also encourage you to use an operating system such as Linux, FreeBSD, or OpenBSD. However, additional operating systems such as Windows, Mac, and other flavours of *nix are also supported.

DOTPROJECT DEMO dotProject Demo can be accessed at http://www.dotproject.net/demo/