Welcome Guest, Not a member yet? Register   Sign In
Debug helper
#1

[eluser]Crimp[/eluser]
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.

Code:
<?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;
    }

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

?&gt;
#2

[eluser]John_Betong[/eluser]
&nbsp;
I like many of the functions that are used and have started to use them already.
&nbsp;
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');
&nbsp;
I include it in my index.php with require('_fred.php') and it makes it available to view the CodeIgniter source code.
&nbsp;
Give fred(...) a try // named because it is very easy to type.
&nbsp;
_fred.php
Code:
&lt;?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
&nbsp;
&nbsp;
&nbsp;
edit: added screen dump
#3

[eluser]hugle[/eluser]
Cute and simple Smile


thank you very much!

p.s. I like the colorsSmile)
#4

[eluser]blackgoo[/eluser]
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.
#5

[eluser]sandeep nami[/eluser]
Thank you vary much John_Betong
:-)
#6

[eluser]woeps[/eluser]
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:
Code:
/**
* 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($var, true);
            }
                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!




Theme © iAndrew 2016 - Forum software by © MyBB