Wednesday, November 21, 2012

Move UP and DOWN button using jQuery

Scroll up and scroll down buttons for the div tag using jQuery. You need to add jQuery library to your page before use this code sample. It can done by adding this script tag.


Online jQuery library
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js">
</script>

Downloaded jQuery library
<script src="jquery.js"></script>

Monday, November 19, 2012

Dynamic Font resize using jQuery

You can change font size of your entering text when it display somewhere. Element ids, classes and variable names are declared as my app. You can change them as you wish.

var font_size=40;
$("#front_text").keyup(function(){
   $("#preview_text").text($("#front_text").val());
   var front_length = $("#preview_text").text().length;
   if(front_length < 5){
    $("#preview_text").css("font-size",font_size+"px");    
   }else if(front_length >= 5 && front_length < 10){ 
    $("#preview_text").css("font-size",(font_size-10)+"px");
   }else if(front_length >= 10 && front_length < 15){
    $("#preview_text").css("font-size",(font_size-20)+"px");
   }else if(front_length >= 15 && front_length < 20){
    $("#preview_text").css("font-size",(font_size-25)+"px");
   }else{
    $("#preview_text").css("font-size",(font_size-29)+"px");
   }
});​

Thursday, September 20, 2012

PHP pagination class(pagination script)


Only you need to make and connect database connection and call from your class to getPaginationList() mthod in this class. It will return set of pagination data. $link_per_page must be a even number.



class PaginationQueries {

    private $sql; //sql quary for retrieve data from database
    private $db;
    private $php_self;
    private $min;
    private $max_list;
    private $link_per_page;

    public function __construct() {
        $this->db = new DBConnection(); //database connection
        $this->php_self = htmlspecialchars($_SERVER['PHP_SELF']); //self url in the address bar
    }

    /**
     * pagination list details
     *
     * @param string $sql sql query
     * @param int $max_list maximum number of documents per page
     * @param int $min current page
     *
     * @return array containing link for every page
     */
    function getPaginationList($sql, $min, $max_list, $link_per_page) {
        $this->sql = $sql;
        $this->min = $min; //pagination starting from this number
        $this->max_list = $max_list;//number of links in this page
        $this->link_per_page = $link_per_page;
        return $this->moveFirst() . $this->movePreByBlock() . $this->paginationListDecrease() . '<span class="pagination_list now"><a>&nbsp;' . ($this->min + 1) . '&nbsp;</a></span>' . $this->paginationListIncrease() . $this->moveNextByBlock() . $this->moveLast();
    }

    /**
     * @return decreacing link list from current page
     */
    function paginationListDecrease() {

        $link = '';
        $decreace_link = $this->link_per_page / 2;
        for ($count = $this->min - $decreace_link; $count < $this->min; $count++) {
            if ($count >= 0) {
                $parameters = explode('=', $this->php_self);
                $link.="<span class='pagination_list' id='$count'><a href='" . $parameters[0] . "=$count'>&nbsp;" . ($count + 1) . "&nbsp;</a></span>";
            }
        }
        return $link;
    }

    /**
     * @return increasing link list from current page
     */
    function paginationListIncrease() {
        $numberOfRows = $this->db->query($this->sql, array());
        $increace_link = $this->link_per_page / 2;
        $number = $numberOfRows->fetch();
        $numberOfPages = floor($number[0] / $this->max_list);
        $link = '';
        for ($count = $this->min + 1; $count <= $this->min + $increace_link; $count++) {
            if ($count <= $numberOfPages) {
                $parameters = explode('=', $this->php_self);
                $link.="<span class='pagination_list' id='$count'><a href='" . $parameters[0] . "=$count'>&nbsp;" . ($count + 1) . "&nbsp;</a></span>";
            }
        }
        return $link;
    }

    /**
     * @return link to move first page
     */
    function moveFirst() {
        $parameters = explode('=', $this->php_self);
        return "<span class='pagination_list' id='0'><a href='" . $parameters[0] . "=0'>&nbsp; First &nbsp;</a></span>";
    }

    /**
     * @return link to move last page
     */
    function moveLast() {
        $numberOfRows = $this->db->query($this->sql, array());
        $number = $numberOfRows->fetch();
        $numberOfPages = floor($number[0] / $this->max_list);
        $parameters = explode('=', $this->php_self);
        return "<span class='pagination_list' id='$numberOfPages'><a href='" . $parameters[0] . "=$numberOfPages'>&nbsp; Last &nbsp;</a></span>";
    }

    /**
     * 
     * @param string $sql sql query for populate data(optional parameter)
     * @param int $min current page(optional parameter)
     * @param int $max_list maximum number of documents per page
     *
     * @return link to move next page
     */
    function moveNext($sql = '', $min = 0, $max_list = 0) {
        if ($sql == '') {
            $sql = $this->sql;
            $min = $this->min;
            $max_list = $this->max_list;
        }
        $numberOfRows = $this->db->query($sql, array());
        $number = $numberOfRows->fetch();
        $numberOfPages = floor($number[0] / $max_list);
        $parameters = explode('=', $this->php_self);
        $next_page = $parameters[1] + 1;
        if ($next_page >= $numberOfPages) {
            return "<span class='pagination_list'><a href='#'>&nbsp; &raquo; &nbsp;</a></span>";
        } else {
            return "<span class='pagination_list'id='$next_page'><a href='" . $parameters[0] . "=$next_page'>&nbsp; &raquo; &nbsp;</a></span>";
        }
    }

    /**
     * @return link to move previous page
     */
    function movePrevious() {
        $parameters = explode('=', $this->php_self);
        $previous_page = $parameters[1] - 1;
        if ($previous_page <= 0) {
            return "<span class='pagination_list'><a href='#'>&nbsp; &laquo; &nbsp;</a></span>";
        }
        return "<span class='pagination_list' id='$previous_page'><a href='" . $parameters[0] . "=$previous_page'>&nbsp; &laquo; &nbsp;</a></span>";
    }

    /**
     * @return link to move previous page
     */
    function moveNextByBlock() {
        $numberOfRows = $this->db->query($this->sql, array());
        $number = $numberOfRows->fetch();
        $numberOfPages = floor($number[0] / $this->max_list);
        $parameters = explode('=', $this->php_self);
        $next_page = $parameters[1] + ($this->link_per_page);
        if ($next_page >= $numberOfPages) {
            return "<span class='pagination_list' id='$numberOfPages'><a href='" . $parameters[0] . "=$numberOfPages'>&nbsp; &raquo; &nbsp;</a></span>";
        } else {
            return "<span class='pagination_list' id='$next_page'><a href='" . $parameters[0] . "=$next_page'>&nbsp; &raquo; &nbsp;</a></span>";
        }
    }

    /**
     * @return link to move previous page
     */
    function movePreByBlock() {
        $parameters = explode('=', $this->php_self);
        $previous_page = $parameters[1] - ($this->link_per_page);
        if ($previous_page <= 0) {
            return "<span class='pagination_list' id='0'><a href='" . $parameters[0] . "=0'>&nbsp; &laquo; &nbsp;</a></span>";
        }
        return "<span class='pagination_list' id='$previous_page'><a href='" . $parameters[0] . "=$previous_page'>&nbsp; &laquo; &nbsp;</a></span>";
    }

}

linkwithin

go