Friday, 4 March 2011

how to make Alphabet Navigation Menu with PHP?

In this post we are going to discuses about alphabet navigation.before that do you know generating alphabets in a loop.if don’t know look at below example once










1<?php










2










3for ($i=65; $i<=90; $i++) {










4echo chr($i);// chr returns specific character










5//out put :ABCDEFGHIJKLMNOPQRSTUVWXYZ










6}










7?>




In the above example chr() function returns specific character & chr() also accepts negative numbers as an ascii code[char(-100)]

You can make above output with range() function,look the example :














1<?php foreach(range('A','Z') as $i) {










2echo $i; }










3?>




output:ABCDEFGHIJKLMNOPQRSTUVWXYZ

now you can easily understand below alphabet navigation menu














1<?php










2$LetterList = range('A','Z');










3foreach ($LetterList as $litter) {










4echo '<a href="http://example.com/letter.php?letter=' . $litter. '">' .$litter. '</a>'.' ';










5}










6?>




Outpup : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

you can make above output in another way














1<?php










2$LetterList = array_merge(array('0-9'),range('A','Z'));










3foreach ($LetterList as $value) {










4echo '<a href="http://example.com/letter.php?letter=' . $value . '">' .$value . '</a>'.' ';










5}










6?>




out put:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

The description of the functions used in this tutorial are as follows

chr() this function return a spesific character

range() this function crate an array containing range of elements

foreach()foreach is used to loop over all elements of an array

array_merge() this function merges one or more arrays

How to count how many times a certain word is used ,using php?













01<?php










02$count = 0; // count start form zero










03$string = "bugphp -free online tutorials bugphp.com bugphp bugphp.com bugphpbugphp bugphp";










04










05foreach( str_word_count($string ,1) as $a ) {










06 if( strtolower($a) == strtolower('bugphp') )  {










07 $count++;










08 }










09}










10echo "$count";









11?>

How to get absolute path on a web server using PHP ?

We can find absolute path in different ways on a web server using php, i am showing some of that practices here










01<?php










02echo realpath(dirname(__FILE__));










03// realpath() , This function removes all symbolic path and returns the absolute path name , this function return >false an failer










04//dirname() , Returns the parent directory path










05// __FILE__ ,The full path and file name of the file










06?>










07<?php










08echo getcwd();










09//getcwd(), function get the current working directory , returns true on success return >false on failure










10?>










11<?php










12print_r($_SERVER['DOCUMENT_ROOT']);










13//$_SERVER is an array with headers,path,script location information.










14//$_SERVER['DOCUMENT_ROOT'] , returns the current  executing script directory










15?>










16<?php










17// absolute path with file name










18echo $_SERVER['SCRIPT_FILENAME'];










19?>




The description of the functions used in this tutorial are as follows
realpath() , This function removes all symbolic path and returns the absolute path name , this function return false an failure
dirname() , Returns the parent directory path
getcwd(), function get the current working directory , returns true on success return flase on failure
$_SERVER is an array with headers,path,script location information.

Getting visitor IP address with PHP

We can easily get IP Address of visitor with php environment variables .User IP Address was stored in following php Environment variables $_SERVER['REMOTE_ADDR'],$_SERVER['HTTP_CLIENT_IP'] and $_SERVER['HTTP_X_FORWARDED_FOR'].

most common practice for to get IP address of visitor with php is













1<?php










2$ip=$_SERVER['REMOTE_ADDR'];










3echo 'User ip Address'.$ip;










4?>




We can’t trust above script generated ip address if user use proxy sever it simple return proxy IP.So to get user’s real IP address we should check for proxy and shared servers first.i have wrote one php function to get real IP address of visitor.











01<?php










02function GetUserIp() {










03if(isset($_SERVER['HTTP_CLIENT_IP'])) // check for shared servers










04{










05$ip = $_SERVER['HTTP_CLIENT_IP'];










06}










07elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) // check for proxy servers










08{










09$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];










10} else {










11$ip = $_SERVER['REMOTE_ADDR']; // general method










12}










13list($ip) = explode(',',$ip);










14return $ip;










15}










16echo GetUserIp(); // print ip address









17?>

Wednesday, 16 February 2011

PHP: Read Write CSV

In this article, I will be showing you how to read and write CSV file with PHP.

I have used PHP function fgetcsv to read CSV fields and fputcsv to write on CSV file.

fgetcsv — Gets line from file pointer and parse for CSV fields.
fgetcsv() parses the line it reads for fields in CSV format and returns an array containing the fields read.
This works on PHP 4 and PHP 5.

fputcsv — Format line as CSV and write to file pointer.
fputcsv() formats a line (passed as a fields array) as CSV and write it (terminated by a newline) to the specified file handle.
This works on PHP 5 or greater.



Read CSV file

The following code reads data from a CSV file read.csv and displays in tabular form.
<?php
$row = 1;
if (($handle = fopen("read.csv", "r")) !== FALSE) {
?>
<table border='0' cellpadding="4" cellspacing="1">
<?php
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
?>
<tr <?php if($row==1){echo "style='font-weight:bold; background-color:#CCCCCC'";} else {echo "style='background-color:#DDDDDD'";} ?> style="background-color:#DDDDDD">
<?php
for ($c=0; $c < $num; $c++) {
?>
<td><?php echo $data[$c]; ?></td>
<?php
}
?>
</tr>
<?php
$row++;
}
fclose($handle);
?>
</table>
<?php
}
?>

Write CSV file

The following code writes data to a CSV file file.csv. The data will be in the form of array. Each element will be the row of CSV file. The element of array is in comma separated form.
<?php
$list = array (
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"'
);

$fp = fopen('file.csv', 'w');

foreach ($list as $line) {
fputcsv($fp, split(',', $line));
}

fclose($fp);

echo "CSV File Written Successfully!";
?>

Read from one CSV file and write into the other

The following code read data from a CSV file read.csv and writes the same data to another CSV file readwirte.csv
<?php
// open the csv file in write mode
$fp = fopen('readwrite.csv', 'w');

// read csv file
if (($handle = fopen("read.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// write csv file
fputcsv($fp, $data);
}
fclose($handle);
fclose($fp);

echo "CSV File Written Successfully!";
}
?>