Scripting

Ramblings and links to interesting snippets


Try Ruby

I’m trying to come to grips with Ruby on Rails, and the website Try Ruby was suggested to me.

It was a really impressive online tutorial set-up as a user prompt.  I really appreciate that it’s one Ruby tutorial that doesn’t require me to be download and installing packages willy-nilly to get started 😛  It’s extremely newbie-friendly and is well written and utilizes some light humor to help you get through it 🙂  I think the last chapter was a bit clumsy…but may have just had one too many typos along the way.

Regardless, I’d say the “15 minute” bit is no lie and well worth the time if you’re trying to get better acquainted with Ruby or RoR such as myself.

http://tryruby.org/

Permalink » No comments

350s gallery

I randomly made this little webpage to showcase a lot of 350×200 openers I made for online articles 🙂

It’s a pretty simple script built on top of jquery:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<style type="text/css">
body {
    margin:0;
    overflow:hidden;
}
</style>
<script type="text/javascript">
$(function(){
    $("div").mousemove(function(e){
      height = $(window).height();
        width = $(window).width();
        percent = e.clientY * 100 / height;
        move = ($("#box").height()-height) * percent / 100;
        $("body").scrollTop(move);
 
        percent2 = e.clientX * 100 / width;
        move2 = ($("#box").width()-width) * percent2 / 100;
        $("body").scrollLeft(move2);
    });
});
</script>

The CSS hides the scrollbars and removes the default margins from the body.

Then the Javascipt adds an event handler that will fire every time the mouse moves on top of a div – which is always since the page is pretty much just one giant div.  After that it determines what percent of the window you cursor is currently sitting at, and then translates that to the same percentage of the size of that giant div and scrolls the window accordingly.  So regardless of the size of the window you’ll be able to move it around to see all the contents of the div.

Scroll around the box below to see the random goodness:

Permalink » No comments

Pure CSS GUI icons

Tags:

While I was listening to Sitepoint’s 118th Podcast the other day and they mentioned a very interesting CSS 3 experiment.  Sadly this won’t be working with IE just yet so it’s not something we can put to use right away, but the idea of Pure CSS icons is quite neat 🙂  The use of pseudo elements to create the icons out of pure CSS is truly innovative and makes me start to think about all the code that could be cleaned up by taking advantage of pseudo elements as opposed to adding in more DIVs to get certain background and border effects…

Regardless, it’s an interesting peek in to what we’ll be able to achieve once CSS 3 is fully supported

http://nicolasgallagher.com/pure-css-gui-icons/demo/

Permalink » No comments

PHP database backup

Tags: , , ,

I recently added in a way for admin users to download a backup of the database to a CMS I work on.  I came up with something between this solution and this one.  Add this little snippet after a database connection and change “table_1” and “table_2” to whichever tables you’d like to output, and add as many tables as you need.

What I like about this script is that it doesn’t save a copy to the server so folks can’t nab it if they know the entire URL, but it can be easily integrated to a web based interface.  It also uses “create table if not exists” which was missing from the other two scripts.  I think a backup should create tables in case you loose the table structure, but it shouldn’t delete the existing data.

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
31
32
33
34
35
<?php
function datadump ($table) {
    $result .= "# Dump of $table \n";
    $result .= "# Dump DATE : " . date("d-M-Y") ."\n\n";
    
    $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
    $result .= "\n\n".str_replace('CREATE TABLE','CREATE TABLE IF NOT EXISTS',$row2[1]).";\n\n";
    
    $query = mysql_query("select * from $table");
    $num_fields = @mysql_num_fields($query);
    $numrow = mysql_num_rows($query);
    
    for ($i =0; $i<$numrow; $i++) {
        while($row = mysql_fetch_row($query)) {
            $result .= "INSERT INTO ".$table." VALUES(";
            for($j=0; $j<$num_fields; $j++) {
                $row[$j] = addslashes($row[$j]);
                $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                
                if (isset($row[$j])) $result .= "\"$row[$j]\"" ; else $result .= "\"\"";
                if ($j<($num_fields-1)) $result .= ",";
            }
            $result .= ");\n";
        }
    }
    return $result . "\n\n\n";
}
 
$content = datadump('table_1').datadump('table_2');
 
$file_name = "database_backup.sql";
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=$file_name");
echo $content;
?>

Permalink » 2 Comments