Part of the EllisLab Network
   
 
Debug helper
Posted: 26 September 2009 07:42 AM   [ Ignore ]  
Research Assistant
RankRankRank
Total Posts:  340
Joined  10-02-2006

A helper for debugging during development. Suitable for CI beginners and everyone else.

Save code below to file, name as debug_helper.php and put in your /system/application/helpers folder.

Recommended usage is to autoload this helper in system/application/config/autoload.php.

This makes it easy to turn debugging on and off.

Call these methods anywhere: models, views and controllers.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Outputs an array or variable
*
* @param    $var array, string, integer
* @return    string
*/

    
function debug_var($var '')
    
{
        
echo _before();
        if (
is_array($var))
        
{
            print_r
($var);
        
}
            
else
        
{
            
echo $var;
        
}
        
echo _after();
    
}
    
//------------------------------------------------------------------------------

/**
* Outputs the last query
*
* @return    string
*/

    
function debug_last_query()
    
{
        $CI 
=& get_instance();
        echo 
_before();
        echo 
$CI->db->last_query();
        echo 
_after();
    
}
    
//------------------------------------------------------------------------------

/**
* Outputs the query result
*
* @param    $query object
* @return    string
*/

    
function debug_query_result($query '')
    
{
        
echo _before();
        
print_r($query->result_array());
        echo 
_after();
    
}
    
//------------------------------------------------------------------------------

/**
* Outputs all session data
*
* @return    string
*/
    
function debug_session()
    
{
        $CI 
=& get_instance();
        echo 
_before();
        
print_r($CI->session->all_userdata());
        echo 
_after();
    
}

//------------------------------------------------------------------------------

/**
* Logs a message or var
*
* @param    $message array, string, integer
* @return    string
*/

    
function debug_log($message '')
    
{
        is_array
($message) ? log_message('debug'print_r($message)) : log_message('debug'$message);
    
}

//------------------------------------------------------------------------------

/**
* _before
*
* @return    string
*/
    
function _before()
    
{
        $before 
'<div style="padding:10px 20px 10px 20px; background-color:#fbe6f2; border:1px solid #d893a1; color: #000; font-size: 12px;>'."\n";
        
$before .= '<h5 style="font-family:verdana,sans-serif; font-weight:bold; font-size:18px;">Debug Helper Output</h5>'."\n";
        
$before .= '<pre>'."\n";
        return 
$before;
    
}
    
//------------------------------------------------------------------------------

/**
* _after
*
* @return    string
*/

    
function _after()
    
{
        $after 
'</pre>'."\n";
        
$after .= '</div>'."\n";
        return 
$after;
    
}

//------------------------------------------------------------------------------

 
?> 
 Signature 

Dotted line.

Profile
 
 
Posted: 19 October 2009 08:48 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  749
Joined  12-26-2006

 
I like many of the functions that are used and have started to use them already.
 
I have developed a routine which I use extensively to find the values of any type of variable, integer, string, array or object.
It is easy to use, just type fred(‘whatever variable type you have’);
 
I include it in my index.php with require(‘_fred.php’) and it makes it available to view the CodeIgniter source code.
 
Give fred(...) a try // named because it is very easy to type.
 
_fred.php

<?php

define
('LOCALHOST''localhost' === $_SERVER['SERVER_NAME']);

//==========================================================================  
function fred($data='usage: fred("any type of data")'$data_name='missing_$data_name')
{
    $data_type 
'';
    
// $data objects do not display as an array so...
    
if (is_object($data))
    
{
        $data 
get_object_vars($data); // returns with $data = array();
    
}
    
    
// maybe find the $data type
    
if (empty($data))
    
{
        $data_type     
"empty()";     
    
}else{    
        
switch($data)
        
{
            
case ('' == $data)     :
                
$data_type     "empty()";     
                break;
            case 
is_array($data)     :
                
$data_count    count($data);
                
$data_type     "array($data_count)";     
                break;
            case 
is_bool($data) : 
                
$data_type    'boolean';
                
$data                $data 'TRUE' 'FALSE';
                break;
            case 
is_numeric($data) : 
                
$data_type    'numeric';
                break;
            case 
is_object($data) : 
                
$data_type    'object'
                
$data                =    get_object_vars($my_class);
                break;
            case 
is_resource($data) : 
                
$data_type    'resource';
                
$data_count    mysql_num_rows($data);
                
$tmp                = array();
                while (
$row mysql_fetch_assoc($data))
                
{
                    $tmp[] 
$row;
                
}
                $data 
$tmp;
                break;
            case 
is_string($data) : 
                
$data_type    'string';
                
$data_count    strlen($data);
                break;
            default:       
                
$data_type     'oddball';
                
$data_value    $data;
        
}//end switch
    
}//endif    
        
    // $data must now be an array or a string, numeric, or...
    
$style 'width:96%; margin:1em; overflow:auto;text-align:left; font-family:Courier; font-size:0.86em; background:#efe none; color:#000; text-align:left; border:solid 1px;padding:0.42em';
    echo 
"<fieldset style='$style'>";
            echo    
'<legend>John&rsquo;s Data Determiner:</legend>';        
            echo    
"<br /><b style='color:#f00'>Name &nbsp; ==> "    .$data_name .'</b>';
            echo    
'<br /><b>Type &nbsp; ==> </b>'        .$data_type;
            if (isset(
$data_count))
            
{
                
echo    '<br /><b>Count&nbsp; ==> </b>'        .$data_count;
            
}    
            
echo    '<br /><b>Value &nbsp;==> </b>';
            echo    
"<pre style='width:58.88%; margin:-1.2em 0 1em 9.0em;overflow:auto'>";
                
print_r($data);
            echo 
'</pre>';
    echo 
'</fieldset>';        
}//endfunc 

 
 
 
edit: added screen dump

 Signature 

Joke of the day - Bulletin Board Ideas     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 20 October 2009 11:44 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
RankRankRank
Total Posts:  320
Joined  11-06-2008

Cute and simple smile


thank you very much!

p.s. I like the colors:))

Profile
 
 
Posted: 13 November 2009 12:00 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  15
Joined  10-28-2009

found it useful as for a befinner smile
helps to find a problematic lines in the code if to use correctly.
i like it
thank you.

 Signature 

this thing brings me a little cash (!NSFW):situk. One day i`ll rewrite it using Codeigniter.

Profile
 
 
Posted: 04 January 2010 04:04 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  24
Joined  11-13-2009

Thank you vary much John_Betong
grin

 Signature 

http://sandeepnami.wordpress.com/ cool smile

Profile
 
 
Posted: 02 November 2010 04:05 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  5
Joined  10-27-2010

I really like your helper! Thanks!
But I had to change the echo-statements to put the whole output of each method into a variable and then return this; because if I echo the ouput there will be problems using sessions or “not-default” header.
Now the methods look like this:

/**
* Outputs an array or variable
*
* @param    $var array, string, integer
* @return    string
*/

    
function debug_var($var '')
        
{
        $return 
_before();
            if (
is_array($var))
            
{
                $return 
.= print_r($vartrue);
            
}
                
else
            
{
                $return 
.= $var;
            
}
            $return 
.= _after();
        return 
$return;
        
}

//------------------------------------------------------------------------------

/**
* _before
*
* @return    string
*/
    
function _before()
    
{
        $before 
'<div style="padding:10px 20px 10px 20px; background-color:#fbe6f2; border:1px solid #d893a1; color: #000; font-size: 12px;>'."\n";
        
$before .= '<h5 style="font-family:verdana,sans-serif; font-weight:bold; font-size:18px;">Debug Helper Output</h5>'."\n";
        
$before .= '<pre>'."\n";
        return 
$before;
    
}
    
//------------------------------------------------------------------------------

/**
* _after
*
* @return    string
*/

    
function _after()
    
{
        $after 
'</pre>'."\n";
        
$after .= '</div>'."\n";
        return 
$after;
    

Now I can use this helper with sessions, redirects, etc.
Thanks!

Profile
 
 
Posted: 06 December 2011 12:52 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  19
Joined  09-09-2010

A very simple debug every data type

<?php

if (!defined('BASEPATH'))
    exit(
'No direct script access allowed');
if (!
function_exists('debug')) {

    
function debug($data{
        $dub 
debug_backtrace();
               
        echo 
'<pre><span >';
        echo 
$dub[0]['file'"<br/>On Line Number ";
        echo 
$dub[0]['line'"<br/></span>";
        
print_r($data);

        echo 
'</pre>';
    
}

}
?> 
Profile