Thursday, March 18, 2010

How To Remove the White Space in wp_list_pages

The Problem

The wp_list_pages() generates either a series of
  • elements containing links to all of you blog’s pages, or a full unordered list with a heading at the beginning.
    The output of this list looks like this:

    <ul>
    <li><a href="#">Page name</a></li>
    <li><a href="#">Page name</a></li>
    </ul>


    While some browsers don’t have a problem with elements being generated on new lines, in some, this method causes a white space to show up in front of each list item. This space becomes visible and annoying when trying to create a horizontal menu with background colors and equal horizontal spacing.

    The Fix

    To get rid of the white space, your output would need to look like this:


    <ul><li><a href="#">Page name</a></li><li><a href="#">Page name</a></li></ul>


    This can be achieved by using a small PHP function that will take the code generated by wp_list_pages() and turn it into something similar to the example above. To make this function available for use in your templates you need to edit the “functions.php” file, found in you theme’s directory. If you don’t have such a file, create an empty one and name it “functions.php”.

    In functions.php add the following code at the beginning or at the end of the file. Just be careful not to paste it inside another function and cause the script to break.


    function qbkl_nospace($input) {
    $output = str_replace(array("\n", "\r", "\t"), "", $input);
    echo $output;
    }


  • Check validate HTML and CSS

    http://jigsaw.w3.org/css-validator/#validate_by_uri

    http://validator.w3.org/check?verbose=1&uri=http://thy4u.blogspot.com

    Array Max


    <html>
    <head>
    <title>Array Max</title>
    <script type="text/javascript">
    // From: http://codingforums.com/showthread.php?t=152260

    var v=[10,8,42,50] // So looking to find 50
    function srchMaxV() {
    var maxV = 0; // or value smaller that smallest in array to search, like = -1000;
    for (i=0; i if (v[i] > maxV) { maxV = v[i]; }
    }
    return maxV;
    }

    function NumSort(a,b) { return a-b; } // required for sorting numbers only
    function sortMaxV() {
    var sortedV = new Array();
    sortedV = v.sort(NumSort);
    alert('Max: '+sortedV[sortedV.length-1]+'\nMin: '+sortedV[0]);
    }
    </script>
    </head>
    <body>
    <button onclick="alert('Max: '+srchMaxV())">Search for Max
    <button onclick="sortMaxV()">Sort for Max/Min
    </body>
    </html>

    Friday, March 5, 2010

    Image watermark with PHP

    Steps:

    1. Load both images
    2. Get size of both images
    3. Copy watermark to main image
    4. Print image to screen



    PHP functions:
    imagecreatefromgif
    imagecreatefromjpeg
    getimagesize
    imagecopymerge
    header
    imagejpeg
    imagedestroy

    Watermark image:



    Main image:




    <?php  

    $main_img = "Happy_valentines_day.jpg"; // main big photo / picture
    $watermark_img = "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
    $padding = 3; // distance to border in pixels for watermark image
    $opacity = 100; // image opacity for transparent watermark

    $watermark = imagecreatefromgif($watermark_img); // create watermark
    $image = imagecreatefromjpeg($main_img); // create main graphic

    if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");


    $watermark_size = getimagesize($watermark_img);
    $watermark_width = $watermark_size[0];
    $watermark_height = $watermark_size[1];

    $image_size = getimagesize($main_img);
    $dest_x = $image_size[0] - $watermark_width - $padding;
    $dest_y = $image_size[1] - $watermark_height - $padding;


    // copy watermark on main image
    imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);


    // print image to screen
    header("content-type: image/jpeg");
    imagejpeg($image);
    imagedestroy($image);
    imagedestroy($watermark);
    ?>


    Check password safety with JavaScript while typing

    <script language="javascript">

    function check_password_safety(pwd){

    var msg = "";
    var points = pwd.length;
    var password_info = document.getElementById('password_info');

    var has_letter = new RegExp("[a-z]");
    var has_caps = new RegExp("[A-Z]");
    var has_numbers = new RegExp("[0-9]");
    var has_symbols = new RegExp("\\W");

    if(has_letter.test(pwd)) { points += 4; }
    if(has_caps.test(pwd)) { points += 4; }
    if(has_numbers.test(pwd)) { points += 4; }
    if(has_symbols.test(pwd)) { points += 4; }


    if( points >= 24 ) {
    msg = 'Your password is strong!';
    } else if( points >= 16 ) {
    msg = 'Your password is medium!';
    } else if( points >= 12 ) {
    msg = 'Your password is weak!';
    } else {
    msg = 'Your password is very weak!';
    }

    password_info.innerHTML = msg ;
    }


    </script>

    <input type="text" name="pwd" id="pwd" size="20" onkeyup="return check_password_safety(this.value);" />

    Type your password!

    Monday, March 1, 2010

    How to Send Email from a PHP Script Using SMTP Authentication

    require_once "Mail.php";

    $from = "Sandra Sender ";
    $to = "Ramona Recipient ";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";

    $host = "mail.example.com";
    $username = "smtp_username";
    $password = "smtp_password";

    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
    echo("

    " . $mail->getMessage() . "

    ");
    } else {
    echo("

    Message successfully sent!

    ");
    }
    ?>