Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

How to extract POST data in one line with php : extract($_POST);

Here is a quick trick in php that can save you time when you retrieve data from an html form.

Lets say you normally have a form with name, surname, email and telephone number.

You would normally collect those informations in your script as such:


$name = $_POST['name'];
$surname = $_POST ['surname'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];

Then you just use your variables in your php script. But things tend to be more complicated when you have more variables in your forms.

Just imagine that you have a questionnaire of about 50 questions, meaning you would have to write 50 lines of php codes just to get those variables.

The trick here is to use the build in php function extract. It would automatically extract the values and assign it to a variable with the key as its name.

I.e. value in $_POST['something'] would be passed in the variable called $something.

So for the example given above the code would be simplified in only one php line to:

  extract($_POST);

It sure save times if you have many variables, even for 2 it is effective. If you ever add another variable to your html form, you don't need to add another line of code to retrieve the value. This can also be used with $_GET also.

Read more!

Php Frameworks

Read more!

Php and Oochappan


Great photograps of Henk (oochappan)
http://www.flickr.com/photos/oochappan 

3467847689_d088432964_m.jpg


Php Tips and Tutorials

logo.gif






Dev-Tips.com is a site just for web developers and designers to sharpen their skills and keep 
up with the latest development and programming techniques.
Topics covered include: php, MySQL, Javascript, jQuery, mootools, and CSS. You can also
become an author, or register and submit your own website links and tips.



 logo.png
Read more!

Smarty, a PHP templating system (http://www.smarty.net/)


How to separate programmers' code (PHP) from designers' code (HTML), or better phrased, how to separate the content from presentation. Smarty, a PHP templating system, was born to solve this problem.

The basic functionality of a templating system is introducing a way of separating presentation from content with very little interaction between programmers and designers.

With Smarty, the PHP syntax doesn't contain print. Instead, the programmer passes these arrays to
the designer by assigning them to Smarty templates. Then it's the designer's job to make them look
good in the web page without worrying about the PHP code. This is one big benefit about using
Smarty, and we will learn in this book how this is done.

The Smartness of Smarty
Smarty allows designers and programmers to interoperate more effectively and not worry about
each other's work. The designer builds the templates for the web page layout and extracts the data
from the PHP files that the programmer has created. The programmer passes data to the templates
without having to generate HTML code. This way, everyone is happy and more efficient because
they all do the job they are good at.

Let's think about an e-commerce site that sells laptop computers. The manufacturer's name, the
model number, characteristics, and price are content elements that will be stored in a database and
displayed to the visitor.

Smarty makes the job of the designer as well as the programmer very easy. The key tasks
performed by them can be listed as follows.

The Programmer's tasks:
•  Extract database elements with a simple query on the database.

•  Validate and manipulate the data by performing business logic on it.
 
•  If needed, change the data access methods and the business logic without interfering
with the designer's work. For example, the whole system could migrate from
MySQL to PostgreSQL without the designer making a single change.

The Designer's tasks:

•  Create HTML designs without affecting or jeopardizing the programmers PHP code.
The designer only needs to be concerned with placing the content elements that the
programmer has agreed to provide.

•  Make changes to the design without consulting or interfering with the
programmer's work.

•  Stop worrying  about technical changes to the site breaking the way that the site
appears to viewers.


Read more!