Category Archives: Code

PHP Arrays

PHP Arrays are used to associate values to keys; ordered maps.  It is a special type of variable that can hold multiple values.

For instance if you have a list of beers and you wanted to manipulate and sort this data, you would need to assign them variables in PHP.

If you did this with standard variables it would look like this:

$beer1=”guiness”;

$beer2=”chimaye”;

$beer3=”miller”;

If you had 100 beers and you wanted to scroll through them all, this could be quite a hassle.

With a numeric array in PHP you could simply say:

$beer=array(“Guiness”, “Chimaye”, “Miller”);

More simply, arrays assign a value, starting at 0 to each.

So, $array = array(1, 2, 3, 4, 5);  print_r(array); would yield:

array (

[0] = 1

[1] = 2

[2] = 3

[3] = 4

[4] = 5

[5] = 6

)

Also important to note.  String keys should be placed in single quotes if they are a literal array index key but Do not quote keys which are constants or variables, as this will prevent PHP from interpreting them.

<h2>3 types of arrays</h2>

There are three types of arrays.    Our example above was a numeric array where index values where automatically assigned (starting at 0)

1.  Numeric Arrays: Arrays with a numeric index.

2. Associative Arrays: An array where each ID key is associated with a value.

3.  Multidimensional Arrays: An array containing one or more arrays.

 

Cake PHP Models

A model in cake php represents data.  Usually this is a database table and very commonly a MySQL table but can include other types of tables, files or many other forms of data.  Associations can be drawn between various models such as when two groups of data relate to one another.

You can learn more about models here:

http://book.cakephp.org/view/1000/Models

Cake PHP Controllers

Controllers handle the logic in Cake PHP applications.  They are typically used to handle models and are named after the model they are associated with but in plural form:

So if your model is called SugarModel your controller would be SugarsController.

Controllers contain methods, often called actions which are essentially commands such as grabbing or storing data.  You can learn more about controllers here:

http://book.cakephp.org/view/955/Controllers