Welcome Guest, Not a member yet? Register   Sign In
Quickie code contribution: EXPLAIN queries in profiler.
#2

[eluser]xwero[/eluser]
You can extend the profile class to keep CI updates hassleless (almost any way Smile )
Code:
class MY_Profiler extends CI_Profiler
{
   function MY_Profiler()
   {
       parent::CI_Profiler();
   }

   function _compile_queries()
    {
        $output  = "\n\n";
        $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
        $output .= "\n";
        
        if ( ! class_exists('CI_DB_driver'))
        {
            $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
            $output .= "\n";        
            $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
            $output .="<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_db')."</td></tr>\n";
        }
        else
        {
            $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').' ('.count($this->CI->db->queries).')&nbsp;&nbsp;</legend>';
            $output .= "\n";        
            $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
            
            if (count($this->CI->db->queries) == 0)
            {
                $output .= "<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
            }
            else
            {
                $highlight = array('SELECT', 'FROM', 'WHERE', 'AND', 'LEFT JOIN', 'ORDER BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR');
                
                foreach ($this->CI->db->queries as $key => $val)
                {
                    $explain = $val; // for EXPLAIN bit later. htmlspecialchars breaks queries, and decode isn't an option until PHP5 >
                                        $val = htmlspecialchars($val, ENT_QUOTES);
                    $time = number_format($this->CI->db->query_times[$key], 4);
                    
                    foreach ($highlight as $bold)
                    {
                        $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);    
                    }
                    
                    $output .= "<tr><td width='1%' valign='top' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
                                        // explain select statements
if (strpos($explain, 'SELECT') !== false)
{
    $r = $this->CI->db->query('EXPLAIN ' . $explain);
    $r = $r->result_array();
    
    $output .= "<tr><td width='1%' style='font-weight:normal;'>&nbsp;</td><td style='color:#000;font-weight:normal;'>";
    
    foreach ($r as $explain)
    {
        $output .= "<table cellpadding='3' width='100%' style='border-top: 1px solid #ddd;margin-bottom: 10px'>";
        next($explain); // pop id off, because who cares?
        while (list($key, $val) = each($explain))
        {
            if (!empty($val)) // only display non-empty elements
            {
                $output .= "<tr><td style='color:#990000;' width='100'>" . $key . "</td><td>" . $val . "</td></tr>";
            }
        }
        $output .= "</table>\n";
    }
    
    $output .= "</td></tr>\n";
}
                }
            }
        }
        
        $output .= "</table>\n";
        $output .= "</fieldset>";
        
        return $output;
    }
}
I apologize for the bad indentation.


Messages In This Thread
Quickie code contribution: EXPLAIN queries in profiler. - by El Forum - 03-05-2008, 07:04 AM
Quickie code contribution: EXPLAIN queries in profiler. - by El Forum - 03-05-2008, 07:15 AM
Quickie code contribution: EXPLAIN queries in profiler. - by El Forum - 03-05-2008, 08:40 AM



Theme © iAndrew 2016 - Forum software by © MyBB