Kurinchi Blogger Scribbles …


Jun 14
2009

cakePHP, codeIgniter: simple method to display array data

Last updated: June 17th, 2009

var_dump(), print_r() are commonly used functions to display data that is passed across controllers or views when playing in MVC frameworks. As the printed data stretches as a long string, it may be difficult to manipulate the resultset in most cases.

When programming in CakePHP or CodeIgniter or other MVC frameworks when you do not want to turn on the debugger routine and want to have a clean display of Array Data you can try adding a simple method in the Controller (appController in cakePHP) as follows

function testdata($passed_data_array){

    echo "<pre>";
    print_r($passed_data_array);
    echo "</pre>";

}

Usage of “preHTML tag preserves the spaces, line breaks and renders a neat display of array data.

You can also use var_dump instead of print_r depending on your need in the above method.

CakePHP’s wrapper method: pr(mixed $var) is the equivalent of the above method.

Tags: , , , , , ,

2 Responses to “cakePHP, codeIgniter: simple method to display array data”

  1. Mosselman says:

    CakePHP has the ‘debug()’ function which displays the line at which it was called and styling in addition to the dump of objects, vars, arrays, etc.

    Instead of building your own functions, first try to find framework solutions to your problems.

  2. kurinchilamp says:

    Personally, I prefer using pr() or print_r() for quick debugging rather than debug().

Leave a Reply


Valid HTML 4.01 Strict  Valid HTML 4.01 Strict