Optimizing Page Load Time in PHP

There are lots of PHP optimization tricks, and all of them are good, they make loading page of Web Site faster, what is very important for end users. For example it can be done by optimizing CSS or HTML code, reducing HTTP requests, and of course optimizing the PHP code with using some function instead of other.

But, what is the solution in case our page contains many forms, and every form has some number of actions. For example:

<form action=“test.php” method=“post”>

<input type=“text” name=“txt” />

<input type=“submit” name=“form1” value=“action1” />

<input type=“submit” name=“form1” value=“action2” />

</form>

<form action=“test.php” method=“post”>

<input type=“text” name=“txt” />

<input type=“submit” name=“form2” value=“action1” />

<input type=“submit” name=“form2” value=“action2” />

</form>

The basically code of processing looks like:

if (isset($_POST[‘form1’]))

{

if ($_POST[‘form1’]==“action1”)

{

do_action1_form1();

}

else if ($_POST[‘form1’]==“action2”)

{

do_action2_form1();

}

else if ($_POST[‘form1’]==“action2”)

{

do_action3_form1();

}

}

This is too redundant, and for a big number of forms it will be too difficult to generate this sequence of statements.

What we are doing?

The simple solution will be to use “eval” function from PHP.

First off all, we must define what forms must be processed.

// List of forms from page

$forms = array(“form1”, “form2”, “form3”);

Of no less importance is to define what method is using forms.

// Which method uses forms for sending data

$method = “POST”;

Now we must get all values, after submission action.

// Get values from submission

eval(“\$values = \$_$method;”);

Now, let’s check if some of form was submitted.

// Get the name of every form

foreach($forms as $form_name)

{

// check if any form was submitted

if (isset($values[$form_name]))

{

// Processing code

}

}

If there has been found any submitted forms then we should get the action name of this event.

// Get action name

$action_name = $values[$form_name];

Well at least after collecting all necessary information, the necessary function have to be called.

// Get function name

$function_name = $form_name.“_”.$action_name;

//Check if function exists

if (function_exists($function_name))

{

// call function

eval(“$function_name();”);

}

// quit the foreach statement,

// because only one form from page can be submitted,

// and with one action

break;

Now just define functions of needed action processing. The function for submit action with name action2 at form with name form3, will look like:

function form3_action2()

{

// Code . . .

}

After this type of optimization, the time of loading page will dramatically improve.

Enjoy!

Advertisements

Fatal error: Class ‘PDO’ not found in TDbConnection.php

PRADO is a component-based and event-driven programming framework for developing Web applications in PHP 5. A primary goal of PRADO is to enable maximum reusability in Web programming. To achieve the above goal, PRADO stipulates a protocol of writing and using components to construct Web applications.

So I start to learn how to use it. I am using WAMP server and Windows Vista. Everything was perfect; I make some pages with end-user activities, and good looking. And I was as happy as a baby seeing his mother, because the most code is object-oriented and it use component-based methodology.

But when I tried to create Active Record Classes, using prado-cli.php, it generates me the next error: Fatal error: Class ‘PDO’ not found in TDbConnection.php.

c:\wamp\www\helloworld>c:\wamp\php\php.exe c:\wamp\www\framework\prado-cli.php shell .

Command line tools for Prado 3.1.1.

Fatal error: Class ‘PDO’ not found in c:\wamp\www\framework\Data\TDbConnection.php on line 169

Although I’ve firstly changed the PHP settings in the WAMP Manager to use PDO, and secondly restarted Apache – it still did not work.

Basically the WAMP, has two PHP configuration files, located at:

\wamp\apache2\bin\php.ini

and

\wamp\php\php.ini

And I changed the file “\wamp\php\php.ini”, by removing the comments at the following lines:

extension=php_pdo.dll

extension=php_pdo_sqlite.dll

extension=php_sqlite.dll

Finally I tried again to use prado-cli.php for creating Active Record Classes, and … it worked perfectly.

Posted in PHP. Tags: , , , , . 8 Comments »

PHP – dynamically getting variables from $_GET or $_POST

One good day I was working for building administration part of a complex Web Site. And like in other administration systems it must have the opportunity to view all users and apply some actions upon them. In my case I had a list of users and a checkbox control near them, and I had to make a delete action for selected users.

How did I find solution? Here is some piece of code.

 

Print list of users:

echo ‘<form action=”‘.$_SERVER[‘REQUEST_URI’].‘” method=”post”>’;

echo ‘<table>’;

while ( )

{

echo ‘<tr>’;

echo ‘<td><input type=”checkbox” name=”selected_user[‘.$user[‘id’].‘]” value=”1″ ></td>’;

echo ‘<td>’.$user[‘username’].‘</td>’;

echo ‘</tr>’;

}

echo ‘</table>’;

echo ‘<input type=”submit” name=”delete_user” value=”Delete”>’;

echo ‘</form>’;

 

When submit button “Delete” was activated:

if ($_POST[“delete_user”])

{

foreach($_POST[“selected_user”] as $id=>$value)

{

// delete user with userid == $id

}

}

So, at last the foreach statement gives to me “id” of selected user, and I can apply the action upon this user. It was interesting for me to find this solution of proceeding variables in PHP.

Posted in PHP. Tags: , , , . 1 Comment »