Counting More than One Criteria

Recently I needed to count the instances of communications that were recorded to a DB between the lefties, and the righties.  The conversation was over 100 responses long in one conversation, and a simple SQL Count showed me that.  But I wanted a little more detail.

select count(*) as TotalCount from THREADS where conversation_id = ‘123’

TotalCount = 100

I wanted to know who had more input to the conversation, but needed to know a simple query to determine the author of the thread counts in one simple query.  I found a rather simple solution to this topic!

select
count (case when user = ‘lefties’ then 1 end) as LeftiesCount,
count (case when user = ‘righties’ then 1 end) as RightiesCount
from THREADS where conversation_id = ‘123’

LeftiesCount = 63
RightiesCount = 37

With the case we say, user = lefties/righties add 1 to the count.  We have counts for the threads and the two cases.

Clearing HTML Tags from a String Variable

How to remove HTML TAG-LIKE content from your strings…

Suppose you have content submitted from your client to the server, and you want to brute force scrub it out…  It’s simple with the following code:

  • ColdFusion In CFSCRIPT
    variables.TaglessString = REReplace(trim(variables.TaggedString),']*>','','all')
  • PHP – http://php.net/manual/en/function.strip-tags.php
    string strip_tags ( string $str [, string $allowable_tags ] )
  • Javascript / AKA JQUERY
    $('#theText').text()

Send me more web languages languages, and I’ll surely post it here.

php string validation

 

Common entry validation techniques in PHP using REGEX. PHP Version 5 or later!

Three common strings to validate are Email a Password and Phone number.

In PHP5 or later, using the filter_var function to validate an email address is the quickest and most reliable method to validating an email.

    function isEmail ( $Email ) {
         return filter_var($Email, FILTER_VALIDATE_EMAIL);
    }

Passwords can be defined to follow any format you want. For this reason, it is obviously best to use a customer regular expression to perform the trick. In the example below, the regular expression looks for 8-16 characters using a-z case insensitive, the numbers 0-9, and !@#$_ special characters are allowed.

    function isPassword ( $Password ) {
        return (ereg('^[a-zA-Z0-9!@#$_]{8,16}$', $Password)) ? true : false;
    }

To validate a phone number I find it best to first strip all special characters from an input, then review the number for a specific patter. In the example below, I am validating a number with leading number 2-9 and 9 more numbers 0-9.

    function isPhone($Phone){
        $strippedPhone = ereg_replace("[^0-9]", "", $Phone);
        return (ereg('^[2-9]{1}[0-9]{9}$', $strippedPhone)) ? true : false;

Scheduled Tasks

Scheduled Tasks or Chron Jobs should be managed with the utmost importance. When you Administration pages, or you UI pages fail you get reports from your users immediately. When you tasks fail, you may not get immediate reports.

Here are some things that can be done to ensure your tasks are executed…

  1. Run a beefier computer than your web servers… While you websites handle a lot of requests, you scheduler runs some heavy loads. Also, your processes may be required to run so the rest of your day can run. The UX may be rendered useless if you your scheduled tasks don’t get run, or even don’t finish running.
  2. Run multiple computers. This is simple, two pieces of hardware means hardware backup.
  3. With multiple computers, tier your processes. Keep your beefier machine for heavier processes. Segregate your tasks by priority.
  4. Record Process times for you tasks. If you see a process taking excessive amounts of time, time to revise. Always take some time to revise you apps!
  5. Optimize the heck out of your code to make sure that everything keeps running smoothly. Revisit

Here’s some thoughts in my head, what’s in yours?