Posts by admin

Center Multiple DIVs with CSS

- by admin

At some point, you may have a situation where you want to center multiple elements (maybe <div> elements, or other block elements) on a single line in a fixed-width area. Centering a single element in a fixed area is easy. Just add margin: auto and a fixed width to the element you want to center, and the margins will force the element to center.

There really should be a similar simple way to center multiple elements evenly spaced. It would be nice if CSS had a property called “box-align” which you could set to “center” then the child elements would be centered evenly within their parent.

Well, you can achieve something similar by taking advantage of CSS’s flexibity with “recasting” elements (for lack of a better term). View a demo of what I’ll be describing in this short tutorial.

The Usual Way


Normally, in such a situation, you would just float the boxes, then add left and right margins to space them out accordingly. But that can get a little messy, because IE6 doesn’t like margins on floats, and you always have to have a different id or class for elements on which you don’t want margins (like the last and/or the first).

You can get around the IE6 problem by adding display: inline in an IE6-only declaration, but your code will still be somewhat messy because of the extra code to get the first and/or last item to behave. Also, the last box could fall to the next line in IE.

There’s another solution to this that might work better in certain circumstances.

Use inline-block and control white space


To achieve the same effect as adding floats and margins, you can simply “recast” your block-level elements as inline blocks, and then manipulate the white space between them. Here is how the CSS might look:
#parent {
    width: 615px;
    border: solid 1px #aaa;
    text-align: center;
    font-size: 20px;
    letter-spacing: 35px;
    white-space: nowrap;
    line-height: 12px;
    overflow: hidden;
}
.child {
    width: 100px;
    height: 100px;
    border: solid 1px #ccc;
    display: inline-block;
    vertical-align: middle;
}

In my example above, I’m assuming there are four child boxes, each with the class child, and each 100 pixels by 100 pixels. The boxes are naturally block-level elements, but the CSS changes them to inline-block, which allows them to flow naturally with text and white space. Of course, since we don’t have any text in the parent container, controlling the text and white space will not be a problem.

The parent element (with the id parent in this example) has four key text properties set, and the children have two:

  • text-align makes all inline child elements centered

  • letter-spacing controls the size of each white space unit between boxes

  • white-space: nowrap keeps the last element from potentially dropping to the next line

  • overflow: hidden prevents the box from stretching in IE6

  • vertical-align: middle (on the children) keeps the boxes on the same vertical plane as each other when content is added

  • display: inline-block (obviously)


Internet Explorer Rears its Ugly Head


What would a CSS solution be without an Internet Explorer issue to work around? While this method works exactly the same in every browser (including IE8), IE6 and IE7 don’t cooperate, because they don’t fully support inline-block. To get those browsers to show virtually the same result, you need to add the following CSS:
.child {
    *display: inline;
    *margin: 0 20px 0 20px;
}

The CSS above must apply only to IE6 and IE7, and it has to appear after the other CSS. In my code (and in the code example above) I’ve accomplished this by using the star hack. The asterisk (or star) at the beginning of each line hides both lines from every browser except IE6 and IE7. The margins added here help us get the same visual result, and the new display property is taking advantage of a bug in those browsers that makes a block element work like its inline when you declare display: inline-block followed by display: inline.

Drawbacks / Final Thoughts


Not many drawbacks to this. You just have to make sure the white space and text settings that you apply are reset on any child elements inside the boxes. So, while this may work when you have straight images or other non-text content, it may be more trouble than its worth if your boxes are fully loaded with diverse content.

But nonetheless a good technique to know when you have to center some block elements with equal spacing, and you don’t want to apply extra classes on the end units. And this technique will be even more important when the older versions of IE disappear from general use.

Git: Push and Delete Remote Branches

- by admin

You can push the branch up to a remote very simply:
git push origin newfeature

Where origin is your remote name and newfeature is the name of the branch you want to push up.

Deleting is also a pretty simple task:
git push origin :newfeature

That will delete the newfeature branch on the origin remote, but you’ll still need to delete the branch locally with git branch -d newfeature.

Mac X11 window font size change

- by admin

The most simple way is adding the following new command in the X11-> application -> customize:
xterm -geometry 72x34+100+40 -fn *-fixed-*-*-*-20-* &

Mac OS X Lion: configure: error: C compiler cannot create executables

- by admin

After Xcode installation and trying to build mc I've got:
configure: error: C compiler cannot create executables

Google offers to install Xcode correctly (this is a different story however) or to reinstall it. Hmmm. Good idea but in my case the following simple action solved the issue: symlinks for compilers - that's all ! KISS
sudo ln -s /usr/bin/llvm-gcc-4.2 /usr/bin/gcc-4.2

sudo ln -s /usr/bin/llvm-g++-4.2 /usr/bin/g++-4.2

Voila !

Uncaught exception 'Zend_Mail_Transport_Exception' with message 'Unable

- by admin

Error:

Fatal error: Uncaught exception 'Zend_Mail_Transport_Exception' with message 'Unable to send mail. ' in /.........../Zend/Zend/Mail/Transport/Sendmail.php:137

Postfix is running, PHP can connect to Postfix.

We have got In Apache error_log:
postdrop: warning: uid=48: File too large

FIX:

The default max message size for Postfix is 10240000 bytes. Thus we do:
% /usr/sbin/postconf -e message_size_limit=XXXXXXXXXXX

where XXXXXXXXXX is the new limit.

And then restart Postfix.

FYI: Related issues:

The the default value for a mailbox size according to Postfix is 51200000 bytes. It may be changed like this:
% postconf -e mailbox_size_limit=0

And then restart Postfix.

The settings for all the size related parameters (in main.cf) can be listed like this:
# postconf -d | grep size

Sample output:
berkeley_db_create_buffer_size = 16777216
berkeley_db_read_buffer_size = 131072
body_checks_size_limit = 51200
bounce_size_limit = 50000
header_size_limit = 102400
mailbox_size_limit = 51200000
message_size_limit = 10240000

Voila !

Mac OS X - MySQL Workbench - Error Opening Configuration File my.cnf

- by admin

By default, the OS X installation does not use a my.cnf, and MySQL just uses the default values.

To set up your own my.cnf, you could just create a file straight in /etc, or do the following (excuse me if I say anything which is obvious to you, but this may help complete OS X beginners who are not familiar with the Unix command line):

Log in to OS X using an administrator-level account (to keep things simple lower down)

Open Terminal (in Utilities folder under Applications folder)
cd /usr/local/mysql/support-files/
sudo cp my-huge.cnf /etc/my.cnf

and enter your admin password when prompted. You could do this from a non-admin account by using the su command, but that's probably a bit scary for some people ;)

You will now have a copy of my.cnf in /etc (just in case you don't know, that means the etc folder directly under the root folder, not under MySQL's install folder)

You can edit it with a text-editor such as TextWrangler by using File->Open Hidden, or if you are happy to use the command line, use:
cd /etc
sudo nano my.cnf

To exit without saving, press CTRL+X, to Save then exit it's: CTRL+O then CTRL+X

How to get the Next Auto Increment number in MySQL

- by admin

In order to get the next Auto Increment number in MySQL just run:
SHOW TABLE STATUS LIKE '$tablename';

The result would be in `Auto_Increment`.

Voila !

MySQL Frequently Used Commands

- by admin

Selecting a database:

mysql> USE database;

Listing databases:

mysql> SHOW DATABASES;

Listing tables in a db:

mysql> SHOW TABLES;

Describing the format of a table:

mysql> DESCRIBE table;

Creating a database:

mysql> CREATE DATABASE db_name;

Creating a table:

mysql> CREATE TABLE table_name (field1_name TYPE(SIZE), field2_name TYPE(SIZE));
Ex: mysql> CREATE TABLE pet (name VARCHAR(20), sex CHAR(1), birth DATE);

Generate the create statement of a table in MySQL:

mysql> SHOW CREATE TABLE tblname;

Load tab-delimited data into a table:

mysql> LOAD DATA LOCAL INFILE "infile.txt" INTO TABLE table_name;
(Use \n for NULL)

Inserting one row at a time:

mysql> INSERT INTO table_name VALUES ('MyName', 'MyOwner', '2002-08-31');
(Use NULL for NULL)

Retrieving information (general):

mysql> SELECT from_columns FROM table WHERE conditions;
All values: SELECT * FROM table;
Some values: SELECT * FROM table WHERE rec_name = "value";
Multiple critera: SELECT * FROM TABLE WHERE rec1 = "value1" AND rec2 = "value2";

Reloading a new data set into existing table:

mysql> SET AUTOCOMMIT=1; # used for quick recreation of table
mysql> DELETE FROM pet;
mysql> LOAD DATA LOCAL INFILE "infile.txt" INTO TABLE table;

Fixing all records with a certain value:

mysql> UPDATE table SET column_name = "new_value" WHERE record_name = "value";

Selecting specific columns:

mysql> SELECT column_name FROM table;

Retrieving unique output records:

mysql> SELECT DISTINCT column_name FROM table;

Sorting:

mysql> SELECT col1, col2 FROM table ORDER BY col2;
Backwards: SELECT col1, col2 FROM table ORDER BY col2 DESC;

Date calculations:

mysql> SELECT CURRENT_DATE, (YEAR(CURRENT_DATE)-YEAR(date_col)) AS time_diff [FROM table];
MONTH(some_date) extracts the month value and DAYOFMONTH() extracts day.

Pattern Matching:

mysql> SELECT * FROM table WHERE rec LIKE "blah%";
(% is wildcard - arbitrary # of chars)
Find 5-char values: SELECT * FROM table WHERE rec like "_____";
(_ is any single character)

Extended Regular Expression Matching:

mysql> SELECT * FROM table WHERE rec RLIKE "^b$";
(. for char, [...] for char class, * for 0 or more instances
^ for beginning, {n} for repeat n times, and $ for end)
(RLIKE or REGEXP)
To force case-sensitivity, use "REGEXP BINARY"

Counting Rows:

mysql> SELECT COUNT(*) FROM table;

Grouping with Counting:

mysql> SELECT owner, COUNT(*) FROM table GROUP BY owner;
(GROUP BY groups together all records for each 'owner')

Selecting from multiple tables:

(Example)
mysql> SELECT pet.name, comment FROM pet, event WHERE pet.name = event.name;
(You can join a table to itself to compare by using 'AS')

Currently selected database:

mysql> SELECT DATABASE();

Maximum value:

mysql> SELECT MAX(col_name) AS label FROM table;

Auto-incrementing rows:

mysql> CREATE TABLE table (number INT NOT NULL AUTO_INCREMENT, name CHAR(10) NOT NULL);
mysql> INSERT INTO table (name) VALUES ("tom"),("dick"),("harry");

Adding a column to an already-created table:

mysql> ALTER TABLE tbl ADD COLUMN [column_create syntax] AFTER col_name;

Removing a column:

mysql> ALTER TABLE tbl DROP COLUMN col;
(Full ALTER TABLE syntax available at mysql.com.)

Batch mode (feeding in a script):

# mysql -u user -p < batch_file
(Use -t for nice table layout and -vvv for command echoing.)
Alternatively: mysql> source batch_file;

Backing up a database with mysqldump:

# mysqldump --opt -u username -p database > database_backup.sql
(Use 'mysqldump --opt --all-databases > all_backup.sql' to backup everything.)

Convert a string into an URL safe address

- by admin

/**
 * Convert a string into a url safe address.
 *
 * @param string $unformatted
 * @return string
 */
public function formatURL($unformatted) {

    $url = strtolower(trim($unformatted));

    //replace accent characters, forien languages
    $search = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', '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', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
    $url = str_replace($search, $replace, $url);

    //replace common characters
    $search = array('&', '£', '$');
    $replace = array('and', 'pounds', 'dollars');
    $url= str_replace($search, $replace, $url);

    // remove - for spaces and union characters
    $find = array(' ', '&', '\r\n', '\n', '+', ',', '//');
    $url = str_replace($find, '-', $url);

    //delete and replace rest of special chars
    $find = array('/[^a-z0-9\-<>]/', '/[\-]+/', '/<[^>]*>/');
    $replace = array('', '-', '');
    $uri = preg_replace($find, $replace, $url);

    return $uri;
}

Voila !

Scanning for Viruses Using PHP and ClamAV

- by admin

Here what I found and like most.

PHP Code for directory scanning:
    // Set the allowed types for reading and upload. 
    $types = array ('jpg', 'jpeg', 'txt'); 
     
    // Start a variable. 
    $dir_files = array(); 
     
    // If it is a directory add all the files. 
    if (is_dir ($dir)) { 
        // Open the directory. 
        if ($handle = opendir($dir)) { 
            // Read the file names of all the files available. 
            while (false !== ($file = readdir($handle))) { 
                // Make sure the file is not this directory or its parent and not the .DS_Store file. 
                if ($file != "." && $file != ".." && $file != '.DS_Store') { 
                    // Get the file parts. 
                    $file_parts  = pathinfo($file); 
                    // Make sure the extension is allowed. 
                    if (in_array(strtolower ($file_parts['extension']),$types)) { 
                        // Add the file to the array. 
                        $dir_files[] = array ('original_name'=>$file, 'type'=>$file_parts['extension']); 
                    } 
                } 
            } 
            // Close the handle. 
            closedir ($handle); 
        } 
    } 

    // If any files exist in the upload directory check them for viruses. 
    if (count ($dir_files) > 0) { 
        // Get the dir and prepare it for the command line. 
        $real_path = realpath ($dir); 
        $safe_path = escapeshellarg($real_path); 
        // Set the variables for the cmd. 
        $return = -1; 
        $out =''; 
        $cmd = '/usr/local/clamXav/bin/clamscan ' . $safe_path; 
        // Execute the cmd. 
        exec ($cmd, $out, $return); 
        // If a virus is found loop through each of the files and delete the virus, write the user a message, and add them to a db table. 
        if ($return != 0) { 
            // Loop through the files. 
            foreach ($dir_files as $k=>$v) { 
                // Get the dir and prepare it for the command line. 
                $real_path = realpath ($dir . $v['original_name']); 
                $safe_path = escapeshellarg($real_path); 
                // Reset the values. 
                $return = -1; 
                $out =''; 
                $cmd = '/usr/local/clamXav/bin/clamscan ' . $safe_path; 
                // Execute the command. 
                exec ($cmd, $out, $return); 
                // If the file is clean do nothing. 
                if ($return == 0){} 
                // If the file contains a virus remove it and add a note to the db. 
                else if ($return == 1) { 
                    // Delete the file. 
                    unlink ($dir . $v['original_name']); 
                    // Unset the file from the records. 
                    unset ($dir_files[$k]); 
                    // Notify the user. 
                    $message .= "The file {$v['original_name']} contained a known virus.  It has been deleted from the server.<br />"; 
                    $message_class = 'error'; 
                    // Add the user who uploaded the file to the db and a time and date. 
                    // Query the db to record who uploaded the file, not necessary but fun info to have. 
                } 
                else { 
                    // Delete the file. 
                    unlink ($dir . $v['original_name']); 
                    // Unset the file from the records. 
                    unset ($dir_files[$k]); 
                    // Notify the user. 
                    $message .= "The file {$v['original_name']} caused an unknown error and was removed from the server.<br />"; 
                    $message_class = 'minor_error'; 
                } 
            } 
        } 
    }

« Newer posts

Older posts »