[Javascript] Print any string

I have a project that requires printing in certain areas. In the past, I would use CSS and a separate stylesheet to determine what should be printed, but this would not work on the scale I am working with.

I set out to create a framework-free function that could be plugged into any site and used to print a string. I did find a few different examples on the web that do something similar, but this is what I wanted so I could reuse it anywhere I wanted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* Javascript String Printer
* Written by Steve Koehler
* www.tiny-threads.com
*/


function print_str(str){
    var print_name = ("printer-" + (new Date()).getTime()); //random printer name
    var elemFrame=document.createElement('iframe'); //the printer iframe
    var printer; //the window instance of the iframe
   
    //set the iframe attributes
    elemFrame.style.cssText = 'position:absolute;left:-9999px;width:1px;height:1px;';
    elemFrame.id=print_name;
    elemFrame.name=print_name;
   
    //add the iframe to the page
    document.body.appendChild(elemFrame);

    //grab the iframe window object, insert the string, and print
    printer = elemFrame.contentWindow;
    printer.document.open();
    printer.document.write(str);
    printer.document.close();
    printer.focus();
    printer.print();

    //remove the iframe after a minute so we don't overload
    setTimeout(function(){elemFrame.remove();},(60 * 1000));
}

This is a relatively clean and efficient function, plus it cleans up after itself! Try it out. If you find it doesn’t work for you, let’s discuss it and figure out what’s up.

You can check out the GitHub Gist here.

Posted in Code, Javascript | Leave a comment

It never fails.

Every semester around this time, we get students who fail to create backups for their important documents. It is hard for me to understand, in this day and age, how a student would not take precaution.

Today I had a student who stated they had saved a file on a public computer, and wanted to see if it’s still there. Our computers are wiped automatically every so often, so there is no gauruntee that it’s there. My first thoughts after this were “why did you not save to a personal device or, at the very least, email it to yourself?”

I am probably insulated in my job, and I can give the person the benefit of the doubt that they were never taught to do this.

To prevent others from having to endure this, there are a few ways you can back up your files to keep this from happening.

dropbox-logo1Dropbox
https://www.dropbox.com/

Dropbox is a good way to keep copies of your files. It will sync your files across different devices, and keep them all up to date. However, the syncing feature also means it’s not the greatest backup solution since an error in a single file will replicate to all your devices. There is, however a restore function that can be useful if done within 30 days of the change.

All in all, Dropbox is a good first line of defense.

 

carbonite-mozyCarbonite – Mozy
http://www.carbonite.com/
https://mozy.com/

Carbonite, Mozy, and other services like that are similar, except they have backups at the heart of their product, unlike Dropbox which has collaboration and syncing at theirs.

With these services you can do much the same as with Dropbox, except when it comes down to restoring large amounts of files, it’s as easy as a few clicks. No waiting for support to do it for you.

Downside is most of them cost money in subscription fees.

 

External-Hard-DriveExternal Hard Drive

The best way, in my opinion, to back up files is with an external hard drive. Plug it in, copy over the files, unplug, and you’re done.

The downside is that you have to remember to only plug it in when you need it. It can be very tempting to leave it plugged in, but if any bit of ransomware hits your computer, it’s just as vulnerable as your other files.

That said, it’s right there when you need it, and you don’t have to rely on a third party service to keep it safe.

 

There are many different options when it comes to backing up important stuff. These are a few of the options available. If you are looking into back up solutions for yourself or business, do your research. Different companies and products offer features that could, sometimes, be specific to your use case.

Posted in Informative | Leave a comment

[PHP] Convert number to Excel column letter

I had a situation where I had to convert an array to an Excel spreadsheet. I decided to use PHPExcel to accomplish this.

One caveat to the way I had to do this was I needed to convert my column numbers to letters. So I did what I always do and wrote a quick function to handle it for me.

I wrote it so it goes on toward infinity, so as to accommodate any future increases in column numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function get_col_letter($num){
    $comp=0;    
    $pre='';
    $letters=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
   
    //if the number is greater than 26, calculate to get the next letters
    if($num > 26){
        //divide the number by 26 and get rid of the decimal
        $comp=floor($num/26);
       
        //add the letter to the end of the result and return it
    if($comp!=0)
        // don't subtract 1 if the comparative variable is greater than 0
        return get_col_letter($comp).$letters[($num-$comp*26)];
    else
            return get_col_letter($comp).$letters[($num-$comp*26)-1];
    }
    else
    //return the letter
        return $letters[($num-1)];
}

Update: Fixed an error after 26 that caused all columns to be returned one column off. Thanks to Scott for this catch!

Posted in Code | 3 Comments

[PDO] Big update

I just finished updating PDO, and I have to say that I made some decently large changes behind the scenes. The changes have more to do with how things function and less with how you interact with them. The biggest thing that I added is MORE ERROR REPORTING! I’ll wait for you to stop applauding.

I have added some more error checks as well as added the abilities to return an array instead of an object and use null variables. This also adds a new option to the class: return_type.

return_type defaults to ‘object’, but can be changed to ‘array’ if you want that instead. It can also be changed at any time so no need to use multiple class instances. I know that’s really how classes should work anyway, but I felt the need to point it out.

Anyway, for those of you keeping track at home, here are the changes:
Added return type and more debug
Update to error checking and issue #8

And here is a link to PDO on Github.

Let me know about any more issues!

Posted in Code, PDO, PHP, Projects | Leave a comment

[Code] New Quick test toolbox

When I’m programming, I tend to have a need to test strings a lot. Making sure that what I’m outputting is correct and stuff like that.

Normally I would write a quick bit of code to test it, but this got tedious for me so I built a small toolbox to help with this. I find it useful, so I think others might too. Feel free to check it out and make use of it where you want.

If you think of a quick bit of code that you test with a lot, and would like it to be added to the toolbox, leave a comment and I will see if I can get it added.

Tiny Threads Toolbox

Posted in Blog Info, Code | Leave a comment