Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For my project, I created a tiny <code>mdl_error</code> model. </p> <p>This model has one public function, <code>throwError</code>, and some private helpers that will show flash notices to the user and send an email to me with current values and session data if need be. The model is autoloaded and is only called if needed. </p> <p>Here is basically what it looks like:</p> <pre><code>&lt;?php class mdl_error extends CI_Model { //types: error, alert, good function throwError($type, $message, $info="", $flash=true, $email=true) { if($flash){ $alert = $type."|".$message; $this-&gt;session-&gt;set_userdata(array("flash" =&gt; $alert)); } if($email){ $problems = $this-&gt;recursivePrintingOfVariables($info); $sessionData = $this-&gt;recursivePrintingOfVariables($this-&gt;session-&gt;userdata); $emailMessage = "Name&lt;br/&gt; &lt;br/&gt;Something has happened. &lt;br/&gt; &lt;br/&gt;"; $emailMessage .= "The type was: {$type}&lt;br/&gt;The message was: {$message}&lt;br/&gt; &lt;br/&gt; &lt;br/&gt;"; $emailMessage .= "Here is the local variables at the time:&lt;br/&gt; &lt;br/&gt;{$problems}&lt;br/&gt; &lt;br/&gt; &lt;br/&gt;"; $emailMessage .= "Here is the session data:&lt;br/&gt; &lt;br/&gt;{$sessionData}&lt;br/&gt; &lt;br/&gt; &lt;br/&gt;"; $emailMessage .= "Please solve this problem or we are all dooooooomed.&lt;br/&gt;&lt;br/&gt;Love,&lt;br/&gt;Website"; $this-&gt;load-&gt;library('email'); $this-&gt;email-&gt;from("my email"); $this-&gt;email-&gt;to("error@whatever.com"); $this-&gt;email-&gt;subject($type.' Message from Website'); $this-&gt;email-&gt;message($emailMessage); $this-&gt;email-&gt;send(); } } function recursivePrintingOfVariables($info) { $keys = array_keys($info); $string = ""; foreach($keys as $key){ $string .= $key." =&gt; "; if(is_array($info[$key])){ $string .= "Inner Array&lt;br/&gt;"; $string .= "&lt;div style='margin-left:15px;'&gt;"; $string .= $this-&gt;recursivePrintingOfVariables($info[$key]); $string .= "&lt;/div&gt;"; }else{ $string .= $info[$key]; } $string .= "&lt;br/&gt;&lt;br/&gt;"; } return $string; } </code></pre> <p>}</p> <p>Then, if there is a spot in my code where an error happens, I just call: </p> <pre><code>$this-&gt;mdl_error-&gt;throwError("error","something happend", get_defined_vars()); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload