Grab the RSS feed

Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

How to create the PDF on the fly using PHP?

Portable Document Format (PDF) is an open standard for document exchange. The file format created by Adobe Systems in 1993 is used for representing two-dimensional documents in a manner independent of the application software, hardware, and operating system. ---From wikipedia.

In my project we mostly making the Reports using PDF. So I got some experience in creating PDF on the fly using PHP and I want to share it with the readers of TT.

Libraries:

At first I used the FPDF then I moved to TCPDF contain some more features on TCPDF.
You can find the advantages of TCPDF over FPDF here and here .

Creating simple PDF:

As the name indicates its simple one. But here we are going to use the query and separate functionality for the content display.

In coding section you can get the entire Code.

Problems:
Here I am trying to put the column name when I am creating the PDF i.e.Instead of $row[0] I want to put $row[‘columnname’]. It throws an error.

Hint:
Don’t start the output In this page like echo,print,or HTML. Because it throws an error like the output already started.
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);

Use the above lines to turn off header and footer.
Code is here:
<?php
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

// extend TCPF with custom functions
class MYPDF extends TCPDF {

    public function ColoredTable($header,$data) {
   $this->SetFillColor(255, 0, 0);
        $this->SetTextColor(255);
        $this->SetDrawColor(128, 0, 0);
        $this->SetLineWidth(0.3);
        $this->SetFont('', 'B');
        // Header
  $this->headerdisplay();
  $this->anotherdetaildisplay();
  $header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');
  $data=array('0'=>array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)'));
        $w = array(40, 35, 40, 45);
        $num_headers = count($header);
        for($i = 0; $i < $num_headers; ++$i) {
            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
        }
  
        $this->Ln();
  
        // Color and font restoration
        $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
   
        // Data
        $fill = 0;
  $connection=mysql_connect("yourhost","username","password") or die("could not connect db");
  $db=mysql_select_db("db",$connection)  or mysql_errno();
  $sql="SELECT * FROM table";
  $result=mysql_query($sql,$connection);
       /* foreach($data as $row) {*/
    while($row=mysql_fetch_array($result))
    {
            $this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
            $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
            $this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill);
            $this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
            $this->Ln();
            $fill=!$fill;
        }
        $this->Cell(array_sum($w), 0, '', 'T');
  
    }
 
 public function headerdisplay()
 {
    $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
  $tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="0">
    <tr>
        <td>Test<br/>Test<br/>Test</td>
    </tr>
</table>
EOD;
$this->writeHTML($tbl, true, false, false, false, '');
 }
 public function anotherdetaildisplay()
 {
  $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
  $tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="0">
    <tr>
        <td></td>
        <td></td>
        <td>Test at Right<br/>P O BOX 11097<br/>CHICAGO, IL 60611</td>
    </tr>
</table>
EOD;
 
   $this->writeHTML($tbl, true, false, false, false, '');
  $tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="0">
    <tr>
        <td>LastName FirstName<br/>Address<br/>CITY STATE ZIP</td>
    </tr>
</table>
EOD;
 
   $this->writeHTML($tbl, true, false, false, false, '');
 }
 
}

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('TechnoTiger');
$pdf->SetTitle('Tutorial');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setLanguageArray($l);
$pdf->SetFont('helvetica', '', 12);
$pdf->AddPage();
$pdf->ColoredTable($header, $data);
$pdf->Output('example_011.pdf', 'I');
?>

Authorize.net Payment gateway Integeration

Hi Friends,

I have recently completed my payment gateway Integeration using authorize.net.In the full of development I refer one website John Conde that helps me a lot.Whenever I post the doubts wherever John conde will answer as soon as possible.
Here I want to really thank him for his helps and his answers.I suggest you whenever you had the doubts go to his website or developer forum of authorize.net.

I just want to share this information.

Calculate four weeks date from current date and time


Hi Friends,

I found one default function in PHP that is reduce our time to calculate the date an time for the weeeks and months and days.

Below I give the simple example to calculate 4 weeks date from current date.



$weekstocalculate[0]='4 Months'

$text=trim($weekstocalculate[0]);
$text1="now +".$text;

echo date("F jS, Y",($text1));
It gives the output like this
Month date,Year 
Ex:October,2010 

Please try it and give your feedback 

Conver video format to flv and play a video like youtube

Hi friends,
This is the program we can upload the video file in any format and it will convert it into the flv format and also it will take the snapshot of video for displaying the video in the player before it's going to be done.

For that here we need to configure the ffmpeg into the local server.This ffmpeg will conver the video file into the flv format and also it will take the snapshot of video and it will store the image.

Here I put the $image_name ie. before we need to store the image we put the name for image.Here I take the name from video and simply append the jpg.
Similarly for the video name and make the video name with the .flv I simply append the video name with .flv.

For storing the image and video I create one folder named 'uploads'.Here I use the Image path and media path for the destination.in this destination its going to be saved after convert the video to flv and took the snapshot.

The source folder is where we upload the video.Once I get the video and snapshot I delete the original video from the source.





< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > 
< html xmlns="http://www.w3.org/1999/xhtml" > 
< head > 
< meta http-equiv="Content-Type" content="text/html; charset=utf-8" / > 
< title > Video Upload< /title > 
< /head > 

< body > 



< form action="" method="post" enctype="multipart/form-data" > 
< table width="400" cellpadding="3"  > 
< tr > 
< td colspan="3" >  < /td > 
< /tr > 
< tr > 
< td width="10" rowspan="2" >  < /td > 
< td width="120" > < strong > Choose a file to upload:< /strong > < /td > 
< td width="242" > < input type="file" name="uploaded_file" / > < /td > 
< /tr > 
< tr > 
< td >  < /td > 
< td >  < /td > 
< /tr > 
< tr > 
< td >  < /td > 
< td >  < /td > 
< td > < input type="submit" name="sendForm" value="Upload File" / > 
< br / > < /td > 
< /tr > 
< tr > 
< td colspan="3" >  < /td > 
< /tr > 
< /table > 
< /form > 
< ?php
error_reporting(E_ALL ^ E_NOTICE); // Show all major errors.

// Check to see if the button has been pressed
if (!empty($_REQUEST['sendForm']))
{
// Assign the name to a variable
$name = $_FILES['uploaded_file']['name'];    /*This is the file name for ex:clock.avi ******************/
// Assign the tmp_name to a variable
$tmp_name = $_FILES['uploaded_file']['tmp_name'];

// Assign the error to a variable
$error = $_FILES['uploaded_file']['error'];
// Assign the size to a variable
$size = $_FILES['uploaded_file']['size'];
// No trailing slash
$uploadFilesTo = 'uploads';
// Create safe filename
$name = ereg_replace('[^A-Za-z0-9.]', '-', $name);
// Disallowed file extensions
//what files you don't want upoad... leave this alone and you should be fine but you could add more
$naughtyFileExtension = array("php", "php3", "asp", "inc", "txt", "wma","js", "exe", "jsp", "map", "obj", " ", "", "html", "mp3", "mpu", "wav", "cur", "ani");  // Returns an array that includes the extension
$fileInfo = pathinfo($name);

// Check extension
if (!in_array($fileInfo['extension'], $naughtyFileExtension))
{
// Get filename
$name = getNonExistingFilename($uploadFilesTo, $name);
// Upload the file
if (move_uploaded_file($tmp_name, $uploadFilesTo.'/'.$name))
{
// Show success message

$fileNameLength = strlen($name);
$counter = 0;
$fileNameTemp = "";



$image_name=substr($name, 0, strrpos( $name, ".")) . ".jpg";
$video_name=substr($name, 0, strrpos( $name, ".")) . ".flv";


$media_path=$uploadFilesTo.'/'.$video_name;

$image_path=$uploadFilesTo.'/'.$image_name;
$sourcefile=$uploadFilesTo.'/'.$name;

$command1 = escapeshellcmd("ffmpeg -i $sourcefile -an -ss 00:00:03 -an -r 1 -vframes 1 -s 100*90 -y $image_path"); 
$output = shell_exec($command1);
$command = escapeshellcmd("ffmpeg -y -i $sourcefile -ar 22050 -f flv $media_path");
$output = shell_exec($command);
unlink($sourcefile);
echo '< center > < p > Your Video File has uploaded successfully< br / > '.$uploadFilesTo.'/'.$name.'< /p > < /center > ';
}
else
{
// Show failure message
echo '< center > < p > File failed to upload to /'.$name.'< /p > < /center > ';
}
}
else
{
// Bad File type
echo '< center > < p > The file uses an extension we don\'t allow.< /p > < /center > ';
}

}

// Functions do not need to be inline with the rest of the code
function getNonExistingFilename($uploadFilesTo, $name)
{
if (!file_exists($uploadFilesTo . '/' . $name))
return $name;

return getNonExistingFilename($uploadFilesTo, rand(100, 200) . '_' . $name);
}


? > 
< /body > 
< /html > 




Leave the comment to improve us...

How to replace the long text with dots

In php we can easily replace the long text with the dots.

Here we use the substr function in PHP.

The below example shows the program for that….

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
&lthtml xmlns="http://www.w3.org/1999/xhtml" >
&lthead >
&ltmeta http-equiv="Content-Type" content="text/html; charset=utf-8" / >
&lttitle &gtUntitled Document</title >
</head >

&ltbody >
<?
$message="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

if(strlen($message) &gt15)
{
$listedword=substr($message,0,15);
echo $listedword."...";
}
else
{
echo $message;
} 
? >
</body >
</html >

The output is
ABCDEFGHIJKLMNO...
The first fifteen letters are printed and the remaining with dots…

For more information visit

For more information

Leave the comments to improve us..

Click Download this as a word document

Import Contacts From LinkedIn,Yahoo,Gmail,Hotmail,etc..


Hi Friends,
Every webdesigner and the programmer dream is,the site who is developed my theirself to get popular.So everyone try work hard to make SEO friendly and get to make the site going to be popular.For that the Invite friends in the social networking site help them a lot.
Because in the single click the site going to reach almost hundred and hundreds of users.So every social networking sites contain the feature of Invite friends.But as i programmer we cand get all the social networking sites API.Then What can we do?

In my experience the best one is Open Inviter.Through this we can connect almost all of the websites those who are providing Email services or Social Networks.

How can we Integerate OpenInviter?

Step 1:Register the OpenInviter site with your domain name.
Step:2They provide the API key associate with your domain name.Now download the OpenInviter.
Step:3Unzip the file and Upload the OpenInviter to your root directory.
Step:4Run the postinstall.php(Like this:http://yourdomainname.com/OpenInviter/postinstall.php
Step:5It tells you how can your website supporting OpenInviter technologey.
Step:6Delete postinstall.php from the OpenInviter Directory.
Step:7Run http://yourdomainname.com/OpenInviter/example.php.

Change the messages and other things depend upon your need.It works fine.

You can Download this post as word document.

Leave the comment to improve us...

Convert the time zone in PHP

In my application the client want to display the timezone in PST.
But default the time is displayed in MST.That is we get the time from server and displayed in aparticular page.
But the client want to show only in the particular page in PST.So I get the Program from PHP http://www.php.net/manual/en/function.date.php and use it.

Here I need to change the America/New_York to America/Los_Angels and it display the PST time

If you want it to display in different format change the argument in $format.Instead of g:i A T use D,h A T etc.
#

< ?php
function date_at_timezone($format, $locale, $timestamp=null){

if(is_null($timestamp)) $timestamp = time();

//Prepare to calculate the time zone offset
$current = time();

//Switch to new time zone locale
$tz = date_default_timezone_get();
date_default_timezone_set($locale);

//Calculate the offset
$offset = time() - $current;

//Get the date in the new locale
$output = date($format, $timestamp - $offset);

//Restore the previous time zone
date_default_timezone_set($tz);

return $output;

}

//Examples
$t = time();

print date("g:i A T", $t); //4:16 PM PDT
print date_at_timezone("g:i A T", "America/New_York", $t); //7:16 PM EDT
print date_at_timezone("g:i A T", "Pacific/Samoa", $t); //12:16 PM SST
print date("g:i A T", $t); //4:16 PM PDT
? > 

Leave the comments to improve us….
Click Here to download as word document...

Page redirection Using PHP

The simplest and easiest way is header redirection in PHP.

The Header redirection has the PHP following syntax.

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

Whenever you need to redirect the page you can use this.

Today I need to solve the issue that contains this problem.

My situation is when the user get the Message it will open in the Jquery Modal box with the button read and delete. When they get the popup and click the button it will redirect to the page with the url of ?MessageID=’something.’

Even if delete the message it will called in the ajax and the URL is NOT cleaned .Then after delete the ID also going to be cleared in Database. IF the user going to refresh after delete the message it gives the SQL error because of there is no such a ID.

In that situation I use the above header redirection. That is I check whether the ID is NULL. If it is NULL then I redirect to the another URL.

Otherwise I can do the Operation as Read a Message.


Leave the comments to improve ourself………….

For more Information visit this site

Click Here to download as a PDF format

Concatenation in PHP

The easiest one operation in PHP is the concatenation operation.

This is need only one dot operator for the concatenation .Whenever we need the concatenation it tooks the simple dot and append the variable we need.

The below code simply illustrates the two methods of Concatenation.

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > 
< html xmlns="http://www.w3.org/1999/xhtml" > 
< head > 
< meta http-equiv="Content-Type" content="text/html; charset=utf-8" / > 
< title > concatenation< /title > 
< /head > 

< body > 
< ?
$var1=' I am';
$var2=' a';
$var3=' boy';
echo $var1.$var2.$var3;
echo "< br/ > ";
/************otherwise we can use like the below operation**************/
$var4=' Hello ';
$var4.='world';
echo $var4;

? > 
< /body > 
< /html > 



Leave the comments to improve ourself…..

Click Here to download as PDF Format

Connect to database using PHP

Connecting to the data base is the very effective and really needed one. Whenever you going to perform the signup or other activities we need to update or insert the user activity to the databse.

So the database connection playing vital role in website operations.

So we need to connect our database for perform operation by using following code.

It is really simple to connect the database,

< ?php

$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name ", $con);

? >

Put your password and database_ name to connect ..

Leave the comments….It will help us to improve

Click Here to download this article as PDF format

Decrypt MD5 using the following code in PHP

Most of us are known about encryption and decryption. Even though we known I give the small introduction about the encryption.

Encryption is used to provide the security to the user. In this way we convert one type of text into another and those who are doesn’t know the decryption they doesn’t read the encryption text.

The encryption is started in the old days. Not find out after the technical era.

In this way we can say MD5 is the one way encryption and oesnt decrypt it.

But the below code provide you to read the encrypted text. But it is available only when small amount of text.

If you find complex words please inform me to improve my knowledge.

The code is shown below.

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< xmlns="http://www.w3.org/1999/xhtml">
<>
< equiv="Content-Type" content="text/html; charset=utf-8">
<> MD5Decryption< /title >
< /head >

<>
< ?
$hash = "202cb962ac59075b964b07152d234b70";
$char[1] = "a";
$char[2] = "b";
$char[3] = "c";
$char[4] = "d";
$char[5] = "e";
$char[6] = "f";
$char[7] = "g";
$char[8] = "h";
$char[9] = "I";
$char[10] = "j";
$char[11] = "k";
$char[12] = "l";
$char[13] = "m";
$char[14] = "and";
$char[15] = "o";
$char[16] = "p";
$char[17] = "q";
$char[18] = "are";
$char[19] = "s";
$char[20] = "t";
$char[21] = "you";
$char[22] = "v";
$char[23] = "w";
$char[24] = "x";
$char[25] = "y";
$char[26] = "z";
$char[27] = "0";
$char[28] = "1";
$char[29] = "2";
$char[30] = "3";
$char[31] = "4";
$char[32] = "5";
$char[33] = "6";
$char[34] = "7";
$char[35] = "8";
$char[36] = "9";
$char[37] = "A";
$char[38] = "B";
$char[39] = "C";
$char[40] = "D";
$char[41] = "E";
$char[42] = "F";
$char[43] = "G";
$char[44] = "H";
$char[45] = "I";
$char[46] = "J";
$char[47] = "K";
$char[48] = "L";
$char[49] = "M";
$char[50] = "and";
$char[51] = "O";
$char[52] = "P";
$char[53] = "Q";
$char[54] = "are";
$char[55] = "S";
$char[56] = "T";
$char[57] = "you";
$char[58] = "V";
$char[59] = "W";
$char[60] = "X";
$char[61] = "Y";
$char[62] = "Z";
$top = count($char);
For ($d = 0; $d < = $top; $d++)
{
$ad = $ae.$char[$d];
for ($c = 0; $c < = $top; $c++)
{
$ac = $ad.$char[$c];
for ($b = 0; $b < = $top; $b++)
{
$ab = $ac.$char[$b];
for ($a = 0; $a < = $top; $a++)
{
$aa = $ab.$char[$a];
if(md5($aa)==$hash)
{
die('Wachtwoord: '.$aa);
}
}
}
}
}
Echo "Geen Result.";

? >
< /body >
< /html >


Please leave the comments…


Click to Download in the word format

Avoid the errors in the PHP

Sometimes we need to avoid the warnings in the PHP. We know very well these warnings doesn’t need to take care. These kind of we need to use one PHP function.

It doesn’t display any Errors on the page. Be careful to using this kind of function.

The below example illustrate how to avoid errors,

Include the below lines into the Container file where the error coming from

php ini_set('display_errors',0)


When using this the errors doesn’t display on the page.

 
Real Time Web Analytics