Wednesday, April 29, 2009

Paging in PHP

Paging is the concept which will be mostly used in any language like in java or dotnot or php. The main use of the paging will limit the number of records from displaying and make the user more clear about the records displayed. The concept of paging will be same in all the languages. You may take the dotnet or jsp or php the overall concept remains the same.
This may be the new concept but we may need the simpler version in writing the code. Here i am attachingthe code which may feel will be the simpler version, Any one can use this code.

In the downloaded files you will have the 2 php files and 1 database file. Run the script of the MYsql.

$start=$_GET['start']; // To take care global variable if OFF
if(!($start > 0)) { // This variable is set to zero for the first page
$start = 0;
}

$eu = ($start -0);
$limit = 8; // No of records to be shown per page.
$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;

Here we can change the no of records per page by changing the value of limit.



Leia Mais…

Friday, April 24, 2009

How to read html content in PHP and append the text

In php we have code for reading a file. But in some scenarios we may need to rad the html and in the html file we need to append certain variables with the external variables.

For example:
We need to send an email in a particular website. We will design the mail format in the html and save it. In the html file we will declare the dynamic variables with the special characters(like #username#). These are the dynamic variables which will change, depends on the user side functionality.

This is the sample code for obtaining the above task.

$txtsubject="Your new password";
$g_phpMailer_method = 2; // You will define the your mailer method
$g_smtp_host = 'XX.XX.XXX.XXX'; // you will define the host name
$g_smtp_username = 'XXXXX'; // Your SMTP admin name
$g_smtp_password = 'XXXXX'; // Your SMTP admin password
$to="testmail@gmail.com"; // To email address
$email="testmail@gmail.com"; // From email address
$subject="Your test subject";
$myFile = "tempfolder/mailformat.html";
$fh = fopen($myFile, 'r');
$body = fread ($fh, filesize($myFile));
$body= str_replace( '#mysubject#', $subject , $body);
$body = str_replace( '#myemail#', $email , $body);
$from="from email address";
$headers = 'MIME-Version: 1.0' . "\r\n".'Content-type: text/html; charset=iso-8859-1' . "\r\n".
'From:'.$from."\r\n" .'X-Mailer: PHP/' . phpversion();
if(mail($to, $txtsubject, $body, $headers))
{
echo "Mail sent";
}

Here we use fopen to open the html file and fread to read the html content into some variable. str_replace method is used to replace the ## between variables with the dynamic variables.

Leia Mais…

Saturday, April 18, 2009

Top 10 PHP Frameworks

Development of PHP scripts without a preconceived plan to manage them is asking for a headache. The goal of a framework is to make the process of writing web-based applications simpler.

An application designed around MVC is easier to manage because it is split into tiers, which allow for independent development. This promotes code reusability by building models, which are reusable throughout the application.

What makes a good framework?

A good framework is easy to learn, simple to use, intuitive to work with, easy to extend or to modify, rapid to build (maintain) applications with and of course stable.

Top 10 PHP MVC Frameworks:


1. YII



Yii is a component-based high-performance PHP framework for developing large-scale Web applications. Yii is written in strict OOP and comes with thorough class reference and comprehensive tutorials. From MVC, DAO/ActiveRecord, widgets, caching, hierarchical RBAC, Web services, to theming, I18N and L10N, Yii provides nearly every feature needed by today’s Web 2.0 application development. And all these come without incurring much overhead. As a matter of fact, Yii is one of the most efficient PHP frameworks around.

2. CodeIgniter



CodeIgniter is an Application Development Framework - a toolkit - for people who build web sites using PHP. Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries. CodeIgniter lets you creatively focus on your project by minimizing the amount of code needed for a given task.

3. CakePHP



CakePHP is a rapid development framework for PHP which uses commonly known design patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC. Our primary goal is to provide a structured framework that enables PHP users at all levels to rapidly develop robust web applications, without any loss to flexibility.

4. PHPDevShell



PHPDevShell is an Open Source (GNU/LGPL) Rapid Application Development framework written using only PHP with no Javascript and comes with a complete GUI admin interface. It is aimed at developing admin based applications as plugins, where speed, security, stability and flexibility are essentials. It is designed to have a very easy learning curve without complicated new terms to learn. The need for a light, fully functional GUI with limitless configuration brought forward PHPDevShell. We strive to keep direction and focus in our development according to our moto.

5. Akelos



The Akelos PHP Framework is a web application development platform based on the MVC (Model View Controller) design pattern. Based on good practices, it allows you to:

* Write views using Ajax easily
* Control requests and responses through a controller
* Manage internationalized applications
* Communicate models and the database using simple conventions.

Your Akelos based applications can run on most shared hosting service providers since Akelos only requires that PHP be available at the server. This means that the Akelos PHP Framework is the ideal candidate for distributing standalone web applications as it does not require any non-standard PHP configuration to run.

6. Symfony



Symfony is a web application framework for PHP5 projects.

It aims to speed up the creation and maintenance of web applications, and to replace the repetitive coding tasks by power, control and pleasure.

The very small number of prerequisites make symfony easy to install on any configuration; you just need Unix or Windows with a web server and PHP 5 installed. It is compatible with almost every database system. In addition, it has a very small overhead, so the benefits of the framework don’t come at the cost of an increase of hosting costs.

Using symfony is so natural and easy for people used to PHP and the design patterns of Internet applications that the learning curve is reduced to less than a day. The clean design and code readability will keep your delays short. Developers can apply agile development principles (such as DRY, KISS or the XP philosophy) and focus on applicative logic without losing time to write endless XML configuration files.

Symfony is aimed at building robust applications in an enterprise context. This means that you have full control over the configuration: from the directory structure to the foreign libraries, almost everything can be customized. To match your enterprise’s development guidelines, symfony is bundled with additional tools helping you to test, debug and document your project.

7. Prado



The PRADO group is a team of PRADO enthusiasts who develop and promote the PRADO framework and the related projects.
Team Members

* Qiang Xue - founder of PRADO framework, core development
* Xiang Wei Zhuo - core development (javascripts, active controls, DB controls, tests)
* Jason Ragsdale - site and forum administration
* Knut Urdalen - test, marketing
* Carl G. Mathisen - design and document comment system
* Christophe Boulain - component development, test
* Michael Hartl - component development, test
* Eirik Hoem - core development
* Yves Berkholz - core development

Past Team Members

Alex Flint, Brian Luft, John Teague, Todd Patrick, Pim van der Zwet, Tim Evans, Alban Hanry, Marcus Nyeholt
History of PRADO

The very original inspiration of PRADO came from Apache Tapestry. During the design and implementation, I borrowed many ideas from Borland Delphi and Microsoft ASP.NET. The first version of PRADO came out in June 2004 and was written in PHP 4. Driven by the Zend PHP 5 coding contest, I rewrote PRADO in PHP 5, which proved to be a wise move, thanks to the new object model provided by PHP 5. PRADO won the grand prize in the Zend contest, earning high votes both from the public and from the judges’ panel.

In August 2004, PRADO was hosted on SourceForge as an open source project. Soon after, the project site xisc.com was announced to public. With the fantastic support of PRADO developer team and PRADO users, PRADO evolved to version 2.0 in mid 2005. In this version, Wei Zhuo contributed to PRADO with the excellent I18 and L10N support.

In May 2005, we decided to completely rewrite the PRADO framework to resolve a few fundamental issues found in version 2.0 and to catch up with some cool features available in Microsoft ASP.NET 2.0. After nearly a year’s hard work with over 50,000 lines of new code, version 3.0 was finally made available in April 2006.

Starting from version 3.0, significant efforts are allocated to ensure the quality and stability of PRADO. If we say PRADO v2.x and v1.x are proof-of-concept work, we can say PRADO 3.x has grown up to a serious project that is suitable for business application development.

8. Zend



Extending the art & spirit of PHP, Zend Framework is based on simplicity, object-oriented best practices, corporate friendly licensing, and a rigorously tested agile codebase. Zend Framework is focused on building more secure, reliable, and modern Web 2.0 applications & web services, and consuming widely available APIs from leading vendors like Google, Amazon, Yahoo!, Flickr, as well as API providers and cataloguers like StrikeIron and ProgrammableWeb.
Expanding on these core themes, we have implemented Zend Framework to embody extreme simplicity & productivity, the latest Web 2.0 features, simple corporate-friendly licensing, and an agile well-tested code base that your enterprise can depend upon.

9. ZooP



The Zoop Object Oriented Php Framework (The Zoop PHP Framework for short). A framework written in and for php.

The Zoop PHP Framework is stable, scalable and portable. It has been in production use for the last 5 years and has been used in many production environments. The Zoop PHP Framework is designed to be fast, efficient and clean. It is easily extendable and you choose to include only the functionality you use.

With Zoop an inexperienced coder can make secure web applications quickly. A more experienced coder will really appreciate how flexible The Zoop PHP Framework is. Both experienced and inexperienced coders alike will appreciate the automations that are at his/her disposal to handle mundane tasks.

The Zoop PHP Framework encourages separation of display, logic and data layers(MVC).

The Zoop PHP Framework is made up of many components and integrates many different projects including smarty (http://smarty.php.net) and the prototype AJAX framework. It also makes use of PEAR modules (http://pear.php.net). The efficient core components handle many of the functions you would have to code yourselves. Zoop’s integrated error handling can be configured to log errors for production environments, and is highly informative and readable which makes bugs easy to find and squash.

Zoop has been designed to make the developers life easier by providing tools to make efficient use of his/her time. On of our more unique and notable features is our implementation of GuiControls, a revolutionary idea in PHP that provides many form widgets with validation completely integrated, as well as a framework to develop your own guiControls extremely easily.

10. QPHP



QPHP stands for Quick PHP and is a MVC framework similar as architecture to ASP.NET.

I, as the author of the project, have spent the last 8 years working on web projects using various Java frameworks, ASP.NET and PHP. This framework tries to get the best of the above platforms as well as to avoid the problematic parts. Basically it:

* Brings the elegance of Java and C#
* Drops all Perl like bizzare statements that other PHP frameworks use
* Relies extensively on OOP concepts

Code-behind approach is used, so every webpage consists of 2 files:

* .PHP - presentation logic
* .PHP.SCRIPT - business/programming logic

FACTS ABOUT QPHP

* Less than 70 classes
* Less than 300K in size
* 3 years of internal use
* Works with PHP4/PHP5
* Easy database access
* Fast and stable
* Event driven, component based
* AJAX compatible
* I18N support
* View/Session/Application states
* Simple input validation
* 5 menu controls

Source taken from http://php.dzone.com

Leia Mais…

Saturday, April 4, 2009

Working with Files in PHP

Reading a file with fopen()

Basic example for reading a file is as

<?php
// store the pathname of the file
$filename = 'C:/testfolder/filetest.txt';
// open the file in read-only mode
$file = fopen($filename, 'r');
// read the file and store its contents
$contents = fread($file, filesize($filename));
// close the file
fclose($file);
// display the contents
echo nl2br($contents);
?>

process. Unlike file_get_contents(), the function fread() needs to know how much of the file to read. So you need to supply a second argument indicating the number of bytes. This can be useful if you want, say, only the first 100 characters of a text file. However, if you want the whole file, you need to pass the file’s pathname to filesize() to get the correct figure.

The nl2br() function in the final line converts new line characters to XHTML
tags.

Other way of reading the File is

$contents = fread($file, filesize($filename));
with this (the full script is in fopen_readloop.php)
// create variable to store the contents
$contents = '';
// loop through each line until end of file
while (!feof($file)) {
// retrieve next line, and add to $contents
$contents .= fgets($file);
}

fgets() function retrieves one line at a time

Replacing content with fopen()

The first of the write-only modes (w) deletes any existing content in a file, so it’s useful for working with files that need to be updated frequently. You can test the w mode with fopen_write.php, which has the following PHP code above the DOCTYPE declaration:

<?php
// if the form has been submitted, process the input text
if (array_key_exists('putContents', $_POST)) {
// strip backslashes from the input text and save to shorter variable
$contents = get_magic_quotes_gpc() ?
stripslashes($_POST['contents']) : $_POST['contents'];
// open the file in write-only mode
$file = fopen('C:/testfolder/filetest04.txt', 'w');
// write the contents
fwrite($file, $contents);
// close the file
fclose($file);
}
?>

There’s no need to use a loop this time: you’re just writing the value of $contents to the opened file. The function fwrite() takes two arguments: the reference to the file and whatever you want to write to it.

Appending content with fopen()

The append mode is one of the most useful ways of using fopen(), because it adds new
content at the end, preserving any existing content. The main code in fopen_append.php is the same as fopen_write.php, apart from those elements highlighted here in bold:

// open the file in append mode
$file = fopen('C:/testfolder/filetest.txt', 'a');
// write the contents after inserting new line
fwrite($file, "\r\n$contents");
// close the file
fclose($file);

If this file is loaded into a browser and insert some text, it should now be added
to the end of the existing text.

Notice that I have enclosed $contents in double quotes and preceded it by carriage return and new line characters (\r\n). This makes sure that the new content
is added on a fresh line. When using this on Mac OS X or a Linux server, omit the carriage return, and use this instead:

fwrite($file, "\n$contents");

Writing a new file with fopen()
Although it can be useful to have a file created automatically with the same name, it may be exactly the opposite of what you want. To make sure you’re not overwriting an existing file, you can use fopen() with x mode. The main code in fopen_exclusive.php looks like this (changes are highlighted in bold):

// create a file ready for writing only if it doesn't already exist
$file = fopen('C:/testfolder/filetest01.txt', 'x');
// write the contents
fwrite($file, $contents);
// close the file
fclose($file);

If you try it more than once, you should get a series of error messages telling you that the file already exists.

Combined read/write operations with fopen()

By adding a plus sign (+) after any of the previous modes, the file is opened for both reading and writing. You can perform as many read or write operations as you like—and in any order—until the file is closed. The difference between the combined modes is as follows:

r+: The file must already exist; a new one will not be automatically created. The internal pointer is placed at the beginning, ready for reading existing content.

w+: Existing content is deleted, so there is nothing to read when the file is first opened.

a+: The file is opened with the internal pointer at the end, ready to append new material, so the pointer needs to be moved back before anything can be read.

x+: Always creates a new file, so there’s nothing to read when the file is first opened.

Leia Mais…