Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
fballiano edited this page Aug 30, 2012 · 2 revisions

In this page you can find some useful code snippets to help you thru some common operations with P4A.

Newbie FAQ

If you are just getting started with P4A, here are some suggestions to help you get up and running faster:

  • Put the p4a directory in your web server's document root (avoids potential path issues you don't need to deal with now).
  • Build your first apps in /p4a/applications (avoids potential path issues you don't need to deal with now).
  • Read through all of the tutorials and sample apps to see how things work. Cruise the docs.
  • Don't make copies of files (i.e., poor man's version control) within the hierarchy (causes redeclared class errors).
  • Use all lowercase for database table names (camelCase could produce missing variable errors).
  • Understand that objects are stored in the session.
  • Get Firefox Web Developer toolbar so you can quickly remove session cookies.
  • Participate in the forum!

Due to the fact that P4A 3 makes an heavy usage of AJAX technologies, it could be difficult to debug applications while developing, this happens because if you do some syntax errors in your PHP files the AJAX call will be broken and P4A cannot detect that situation. To avoid this, during development, you can disable AJAX completely (see the FAQ in this page), so errors will be shown on screen. You'll obviously enable AJAX to deploy applications to your customers.

Cannot instantiate non-existent class...

If you receive this error probably you're using aliases in your apache configuration, P4A can work with aliases but cannot autodetect paths, thus if you are simply testing P4A please install it under your document_root without aliases.

If you want to do advanced tests, please open the "index.php" of your application and define the following constants at the beginning of the file

define('P4A_ROOT_PATH', '/fabrizio/p4a'); //the path to the framework root
define('P4A_THEME_DIR', '/home/fabrizio/public_html/themes/default'); //the absolute dir of the selected theme
define('P4A_ICONS_DIR', '/home/fabrizio/public_html/icons/default'); //the absolute dir of the selected icons themes

Cannot redeclare class...

On startup, all of the application's files in objects/masks are read. If you make a copy of a file as a backup, or copy to make a new class and do not change the name of the class, you will get a class redeclare error.

How to disable AJAX

put this line

define("P4A_AJAX_ENABLED", false);

in your application's index.php file

How to handle Radio button (or combo box) values using Array Source

$values = array(); 
$values[] = array("id" => "id1", "desc" => "desc 1");
$values[] = array("id" => "id2", "desc" => "desc 2");
$values[] = array("id" => "id3", "desc" => "desc 3");

$this->build("p4a_array_source", "array_source")
	->load($values)
	->setPk("id"); 

$field
	->setType("radio") // or "select"
	->setSource($array_source)
	->setSourceDescriptionField("id") // optional, should be detected automatically
	->setSourceValueField("desc"); // optional, should be detected automatically

How to integrate PEAR packages or 3rd party PHP software

P4A has "libraries" concept, you have 3 levels of "libraries" in P4A:

  • system libraries (private): p4a_installation_dir/p4a/libraries
  • framework wide libraries: p4a_installation_dir/libraries
  • project wide libraries: project_dir/libraries

You should not touch system libraries, extract your package and copy files in the chosen libraries directory. After that you could simply do a require 'filename.php'; and all libraries dir will automatically loaded in your include path.

I'm inside a mask and I need some data from another mask, how do I do it?

P4A::singleton()->masks->the_mask_you_want->the_object_you_want

the_mask_you_want needs to be already instanced before you access it, otherwise you'll get a PHP error.

P4A says "This Query returned an error", how can I get more details?

put this line

define("P4A_EXTENDED_ERRORS", true);

in your application's index.php file

Some useful PHP config you should include in your htaccess

php_value magic_quotes_gpc 0
php_value mbstring.func_overload 1
php_value mbstring.internal_encoding UTF-8
php_value session.use_trans_sid 0