Wednesday, August 29, 2012

Client side data storage

Lots of ways to do it, but I am favoring HTML 5's sessionStorage. Its clean and does not stick around on peoples machines. I hate it when stuff sticks to my machine so I try not to let it stick on others. sessionStorage will expire when the browser closes. Here are a few ways to set it in javascript.

sessionStorage.someKey = 'someValue';

 or if you prefer...

 sessionStorage.setItem('someKey', 'someValue');

 And to retrieve it.

sessionStorage.getItem('someKey')
 or
sessionStorage.someKey

You can display the info with the Document Object Model like so...

document.getElementById("myvalue").innerHTML = sessionStorage.someKey;

You should get this in your HTML

<div id="myvalue">someValue</div>

Monday, August 27, 2012

Linux

I have decided that I am favoring CentOS over Ubuntu. I used Ubuntu for a long while as a desktop, and it was great for that. I use CentOS at work on our servers. When I installed the server addition of Ubuntu I found that in order to be able to SSH to the server to update files I had to jump threw a lot of hoops, like moving the web directory to a home folder and then pointing Apache to the folder to let it write to it. Yes its more secure that way. But what I really want right now is ease of use. Perhaps I will use Ubuntu if I decide to store Credit Cards, when I finally get my game up and running, but beyond that I think its a bit overkill and cumbersome for my personal sites.

Wednesday, August 22, 2012

PHP Email crazyness

Today I built a simple email class to send out emails.


/**
 * Description of Email Class
 *
 * @author Joseph Tveter
 */
class Email
{
    public function __construct()
    {}
   
    public function Email($to, $subject, $message, $from = 'admin@yoursite.com', $cc = '', $bcc = '', $MIME = 'MIME-Version: 1.0', $Content = 'Content-type: text/html; charset=iso-8859-1', $Reply = "")
    {
            $headers  = $MIME . "\r\n";
            $headers .= $Content . "\r\n";
            $headers .= 'From: '. $from . "\r\n";
            if($cc != "")
            {
                $headers .= 'Cc: '. $cc . "\r\n";
            }
            if($bcc != "")
            {
                $headers .= 'Bcc: '. $bcc . "\r\n";
            }
            if($Reply != "")
            {
                $headers .= 'Reply-To: '. $Reply . "\r\n";
            }
           
        if (mail($to, $subject, $message, $headers) == false)
        {
            //send email on fail
            $s = "Email Failed to ".$to;
            $m = $s.":"."\r\n".$message;
            $h  = $MIME . "\r\n";
            $h .= $Content . "\r\n";
            $h .= 'From:'. '$from' . "\r\n";
            mail('you@yoursite.com', $s, $m, $h);
        }
    }
}

Tuesday, August 21, 2012

Multiple CSS Classes in HTML Tag

This was a major help for me.  If your a  control freak like me you want to keep your CSS from getting out of control. I found its good to keep the layout and text formatting CSS seperate.  Lots of ways to do it. Here is a few.

You can separate out the layout and formatting by putting some info in the id, and some in the Class.

#mytag
{
    position:absolute;
    width:15px;
    top:0px;
    left:30px;
    padding-top: 6px;
    padding-left:3px;
}

.MyDiv
{


    color: #000000;
    text-decoration: underline;
    font-weight: bold;


}

<div id="mytag" class="MyDiv">Bla Bla Bla</div>


But whats really cool is that you can put multiple classes in one tag to keep your CSS more reusable.

<div id="mytag" class=" Bold Underline BlackText">Bla Bla Bla</div>

.Bold
{
    font-weight: bold;
}

.Underline
{
    text-decoration: underline;
}

.BlackText
{
     color: #000000;
}

Monday, August 20, 2012

PHP Quotes and HTML

For those who don't know, I was one of you till about a month ago, you can have PHP write double quotes into your html.  You just need a slash in front of each " to keep it part of the string.

$mydiv = "<div class=\"myclass\"></div>";
echo $mydiv;
//<div class="myclass"></div>

This is really handy if you need to pass stuff to a javascript function.

$PassThis = "Passed";
$btn = "<button class='btn' onclick=\"MyFun('$PassThis')\">My Button</button>";

Friday, August 17, 2012

To all database developers

To all database developers:

Please when your building your database please make sure that your structures makes some sort of sense.  Please assign primary keys.  And make sure that they mean something.  Remember that one to many relationships are the goal.  And Please Please Please do not make stupid tables that you have to query 3 fields to get any meaningful data out of it. If for no other reason to preserve the sanity of the developers that come after you.

Thursday, August 16, 2012

Cleaning phone number data from database

I have been working with a database where the phone number data is very dirty.  People have been imputing numbers any old way so I wrote this to clean the data as it comes into the form. It strips out anything that is not a number and removes the '1' at the beginning of the number.
<?php

    private function CleanPhone($ph)
    {
        //break the string into an array
        $phSplit = str_split($ph);
        //Initialize the return value
        $nph = "";
        //Check each item in the array
        foreach($phSplit as $k => $n)
        {
            //if its numeric add it to the return value
            if(is_numeric ($n))
            {
                //if the first number is not a 1 add it to the retun value
                if($k == '0')
                {
                    if($n != '1')
                    {
                        $nph = $nph.$n;
                    }
                }
                else
                {
                    $nph = $nph.$n;
                }
            }
        }
        return $nph;
    }


$nph = CleanPhone($phoneFromDatabase)


$ph1code = substr($nph,0,3);
$ph1pre = substr($nph,3,3);
$ph1suf = substr($nph,6,4);
                   
$ph = "
<div>
          <div>Phone Number</div>
          (<input id='ph1code' name='ph1code' size='3' maxlength='3' type='text' value='$ph1code' form=' Form' />)
           <input id='ph1pre' name='ph1pre' size='3' maxlength='3' type='text' value='$ph1pre' form=' Form' /> -
            <input id='ph1suf' name='ph1suf' size='4' maxlength='4' type='text' value='$ph1suf' form='Form' />
             </div>
                    ";
?>

And here's how it fits on the site.

<html>
<body>

<?php echo $ph; ?>
<form name='Form' action='index.php' method='post'></form>
</body>
</html>