Confirm before Deleting post in CakePHP


In the controller, create a delete function:

<?php
class EmployeeController extends AppController
{
public function delete($id)
{
$this->Employee->delete($id);
$this->Session->setFlash(‘The Employee is deleted.’);
$this->redirect(array(‘action’=>’index’));
}
}

In the view page, write the following code:

<a href=”#” onclick=”return confirm(‘Are you sure you want to delete this Employee?’);”>Delete</a>

Advertisements

Pagination in CakePHP


In the controller file like I have Employeecontroller.php, write the following code:

<?php
class EmployeeController extends AppController
{
public $paginate = array(
‘limit’ => 2,
‘order’ => array(
‘Employee.id’ => ‘asc’
)
);

public function index()
{
$data = $this->paginate(‘Employee’);
$this->set(’employeeList’, $data);
}
}
?>

Now in the view file like I have index.ctp, write the following code:

<div align=”center”>
<!– Shows the page numbers –>
<?php echo $this->Paginator->numbers(); ?>
<!– Shows the next and previous links –>
<?php echo $this->Paginator->prev(‘« Previous ‘, null, null, array(‘class’ => ‘disabled’)); ?>
<?php echo $this->Paginator->next(‘ Next »’, null, null, array(‘class’ => ‘disabled’)); ?>
<!– prints X of Y, where X is current page and Y is number of pages –>
<?php echo $this->Paginator->counter(); ?>
</div>

Web Service


What are Web Services?

  • Web services are application components
  • Web services communicate using open protocols
  • Web services are self-contained and self-describing
  • Web services can be discovered using UDDI
  • Web services can be used by other applications
  • XML is the basis for Web services

How Does it Work?

The basic Web services platform is XML + HTTP.

XML provides a language which can be used between different platforms and programming languages and still express complex messages and functions.

The HTTP protocol is the most used Internet protocol.

Web services use XML to code and to decode data, and SOAP to transport it (using open protocols).

Web services platform elements:

  • SOAP (Simple Object Access Protocol)
  • UDDI (Universal Description, Discovery and Integration)
  • WSDL (Web Services Description Language) Continue reading

What is the default Maximum file upload size and how to change it?


By default Maximum file upload size is 128MB.

php_value upload_max_filesize 128M
php_value post_max_size 128M

If you want to change the default Maximum file upload size from 128MB to 1GB, then it can be done by changing in the these files..

1.) In .htaccess file (Apache + mod_php) add the following two lines to a .htaccess in the installed directory:

php_value upload_max_filesize 1G
php_value post_max_size 1G

2.) In php.ini file, which is located in the php folder (like C:/xampp/php/php.ini)

upload_max_filesize = 1G
post_max_size = 1G

After modifying the global php.ini you will need to restart the webserver to have changes take effect.

In MySQL, what are the differences between primary, foreign, and unique keys?


The one thing that primary, unique, and foreign keys all have in common is the fact that each type of key can consist of more than just one column from a given table. In other words, foreign, primary, and unique keys are not restricted to having just one column from a given table – each type of key can cover multiple columns. Of course, the database programmer is the one who will define which columns are covered by a foreign, primary, or unique key. That is one similarity all those keys share, but there are some major differences that exist between primary, unique, and foreign keys. Let’s go over those differences. We also give a thorough explanation of why foreign keys are necessary in some situations.

Can a table have multiple unique, foreign, and/or primary keys?

A table can have multiple unique and foreign keys. However, a table can have only one primary key.

Can a unique key have NULL values? Can a primary key have NULL values?

Unique key columns are allowed to hold NULL values. The values in a primary key column, however, can never be NULL.

Can a foreign key reference a non-primary key?

Yes, a foreign key can actually reference a key that is not the primary key of a table. But, a foreign key must reference a unique key.

Can a foreign key contain null values?

Yes, a foreign key can hold NULL values. Because foreign keys can reference unique, non-primary keys – which can hold NULL values – this means that foreign keys can themselves hold NULL values as well. Continue reading

What is the differences between InnoDB and MyISAM?


InnoDB

MyISAM

InnoDB implements row-level locking. MyISAM implements table-level locking.
Automatic Crash Recovery. No Crash Recovery
InnoDB doesn’t have FULLTEXT search indexes. MyISAM have FULLTEXT search indexes.
InnoDB also implements transactions, foreign keys and relationship constraints. MyISAM does not support transactions, foreign keys and relationship constraints.
InnoDB stores both data and indexes in one file. MyISAM stores indexes in one file and data in another.
InnoDB uses a buffer pool to cache both data and indexes. MyISAM uses key buffers for caching indexes and leaves the data caching management to the operating system.

In InnoDB the COUNT(*)s (when WHERE, GROUP BY, or JOIN is not used) execute slower than in MyISAM because the row count is not stored internally.

Overall I would recommend InnoDB for most purposes and MyISAM for specialized uses only. InnoDB is now the default engine in new MySQL versions.

What is Polymorphism in PHP? How to use Polymorphism?


Polymorphism is derived from two Greek words. Poly (meaning many) and morph (meaning forms). Polymorphism means many forms. In C you have two methods with the same name that have different function signatures and hence by passing the correct function signature you can invoke the correct method.

This is how polymorphism is achieved in languages like C where in a function sum(int, int) differs from sum(float, float). Therefore the method sum() has many forms depending on the parameters being passed to it.

The meaning with Object Oriented languages changes. With Object Oriented language polymorphism happens:
When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism

Why method polymorphism cannot be achieved
The reason why polymorphism for methods is not possible in PHP is because you can have a method that accepts two parameters and call it by passing three parameters. This is because PHP is not strict and contains methods like func_num_args() and func_get_arg() to find the number of arguments passed and get a particular parameter.
Because PHP is not type strict and allows variable arguments, this is why method polymorphism is not possible.

PHP 5 Polymorphism
Since PHP 5 introduces the concept of Type Hinting, polymorphism is possible with class methods. The basis of polymorphism is Inheritance and overridden methods.
Lets look at an example: Continue reading