বৃহস্পতিবার, ২৮ নভেম্বর, ২০১৩

AUTo div Refresh

HTML fille  index.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Time Interval Page Element Refresh using JQuery and a sprinkle of Ajax</title>
<script language="javascript" src="jquery-1.2.6.min.js"></script>
<script language="javascript" src="jquery.timers-1.0.0.js"></script>

<script type="text/javascript">

$(document).ready(function(){
   var j = jQuery.noConflict();
    j(document).ready(function()
    {
        j(".refresh").everyTime(10000,function(i){
            j.ajax({
              url: "refresh.php",
              cache: false,
              success: function(html){
                j(".refresh").html(html);
              }
            })
        })
    });
   j('.refresh').css({color:"green"});
});



</script>
</head>
<body>
<div class="refresh">This will get Refreshed in 10 Seconds</div>
</body>
</html>


include file refresh.php

<?php
echo time();
echo 'yousuf';
?>
 


Javascript file .js 

jQuery.fn.extend({
    everyTime: function(interval, label, fn, times, belay) {
        return this.each(function() {
            jQuery.timer.add(this, interval, label, fn, times, belay);
        });
    },
    oneTime: function(interval, label, fn) {
        return this.each(function() {
            jQuery.timer.add(this, interval, label, fn, 1);
        });
    },
    stopTime: function(label, fn) {
        return this.each(function() {
            jQuery.timer.remove(this, label, fn);
        });
    }
});

jQuery.extend({
    timer: {
        guid: 1,
        global: {},
        regex: /^([0-9]+)\s*(.*s)?$/,
        powers: {
            // Yeah this is major overkill...
            'ms': 1,
            'cs': 10,
            'ds': 100,
            's': 1000,
            'das': 10000,
            'hs': 100000,
            'ks': 1000000
        },
        timeParse: function(value) {
            if (value == undefined || value == null)
                return null;
            var result = this.regex.exec(jQuery.trim(value.toString()));
            if (result[2]) {
                var num = parseInt(result[1], 10);
                var mult = this.powers[result[2]] || 1;
                return num * mult;
            } else {
                return value;
            }
        },
        add: function(element, interval, label, fn, times, belay) {
            var counter = 0;
           
            if (jQuery.isFunction(label)) {
                if (!times)
                    times = fn;
                fn = label;
                label = interval;
            }
           
            interval = jQuery.timer.timeParse(interval);

            if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
                return;

            if (times && times.constructor != Number) {
                belay = !!times;
                times = 0;
            }
           
            times = times || 0;
            belay = belay || false;
           
            if (!element.$timers)
                element.$timers = {};
           
            if (!element.$timers[label])
                element.$timers[label] = {};
           
            fn.$timerID = fn.$timerID || this.guid++;
           
            var handler = function() {
                if (belay && this.inProgress)
                    return;
                this.inProgress = true;
                if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
                    jQuery.timer.remove(element, label, fn);
                this.inProgress = false;
            };
           
            handler.$timerID = fn.$timerID;
           
            if (!element.$timers[label][fn.$timerID])
                element.$timers[label][fn.$timerID] = window.setInterval(handler,interval);
           
            if ( !this.global[label] )
                this.global[label] = [];
            this.global[label].push( element );
           
        },
        remove: function(element, label, fn) {
            var timers = element.$timers, ret;
           
            if ( timers ) {
               
                if (!label) {
                    for ( label in timers )
                        this.remove(element, label, fn);
                } else if ( timers[label] ) {
                    if ( fn ) {
                        if ( fn.$timerID ) {
                            window.clearInterval(timers[label][fn.$timerID]);
                            delete timers[label][fn.$timerID];
                        }
                    } else {
                        for ( var fn in timers[label] ) {
                            window.clearInterval(timers[label][fn]);
                            delete timers[label][fn];
                        }
                    }
                   
                    for ( ret in timers[label] ) break;
                    if ( !ret ) {
                        ret = null;
                        delete timers[label];
                    }
                }
               
                for ( ret in timers ) break;
                if ( !ret )
                    element.$timers = null;
            }
        }
    }
});

if (jQuery.browser.msie)
    jQuery(window).one("unload", function() {
        var global = jQuery.timer.global;
        for ( var label in global ) {
            var els = global[label], i = els.length;
            while ( --i )
                jQuery.timer.remove(els[i], label);
        }
    });







রবিবার, ২৪ নভেম্বর, ২০১৩

Get Memory For Linux

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>memory</title>

  </head>
  <body>

 <?php
        //cpu stat
        $prevVal = shell_exec("cat /proc/stat");
        $prevArr = explode(' ',trim($prevVal));
        $prevTotal = $prevArr[2] + $prevArr[3] + $prevArr[4] + $prevArr[5];
        $prevIdle = $prevArr[5];
        usleep(0.15 * 1000000);
        $val = shell_exec("cat /proc/stat");
        $arr = explode(' ', trim($val));
        $total = $arr[2] + $arr[3] + $arr[4] + $arr[5];
        $idle = $arr[5];
        $intervalTotal = intval($total - $prevTotal);
        $stat['cpu'] =  intval(100 * (($intervalTotal - ($idle - $prevIdle)) / $intervalTotal));
        $cpu_result = shell_exec("cat /proc/cpuinfo | grep model\ name");
        $stat['cpu_model'] = strstr($cpu_result, "\n", true);
        $stat['cpu_model'] = str_replace("model name    : ", "", $stat['cpu_model']);
        //memory stat
        $stat['mem_percent'] = round(shell_exec("free | grep Mem | awk '{print $3/$2 * 100.0}'"), 2);
        $mem_result = shell_exec("cat /proc/meminfo | grep MemTotal");
        $stat['mem_total'] = round(preg_replace("#[^0-9]+(?:\.[0-9]*)?#", "", $mem_result) / 1024 / 1024, 3);
        $mem_result = shell_exec("cat /proc/meminfo | grep MemFree");
        $stat['mem_free'] = round(preg_replace("#[^0-9]+(?:\.[0-9]*)?#", "", $mem_result) / 1024 / 1024, 3);
        $stat['mem_used'] = $stat['mem_total'] - $stat['mem_free'];
        //hdd stat
        $stat['hdd_free'] = round(disk_free_space("/") / 1024 / 1024 / 1024, 2);
        $stat['hdd_total'] = round(disk_total_space("/") / 1024 / 1024/ 1024, 2);
        $stat['hdd_used'] = $stat['hdd_total'] - $stat['hdd_free'];
        $stat['hdd_percent'] = round(sprintf('%.2f',($stat['hdd_used'] / $stat['hdd_total']) * 100), 2);
        //network stat
        $stat['network_rx'] = round(trim(file_get_contents("/sys/class/net/eth0/statistics/rx_bytes")) / 1024/ 1024/ 1024, 2);
        $stat['network_tx'] = round(trim(file_get_contents("/sys/class/net/eth0/statistics/tx_bytes")) / 1024/ 1024/ 1024, 2);
        //output headers
        header('Content-type: text/json');
        header('Content-type: application/json');
        //output data by json
        echo   
        "{\"cpu\": " . $stat['cpu'] . ", \"cpu_model\": \"" . $stat['cpu_model'] . "\"" . //cpu stats
        ", \"mem_percent\": " . $stat['mem_percent'] . ", \"mem_total\":" . $stat['mem_total'] . ", \"mem_used\":" . $stat['mem_used'] . ", \"mem_free\":" . $stat['mem_free'] . //mem stats
        ", \"hdd_free\":" . $stat['hdd_free'] . ", \"hdd_total\":" . $stat['hdd_total'] . ", \"hdd_used\":" . $stat['hdd_used'] . ", \"hdd_percent\":" . $stat['hdd_percent'] . ", " . //hdd stats
        "\"network_rx\":" . $stat['network_rx'] . ", \"network_tx\":" . $stat['network_tx'] . //network stats
        "}";
        ?>
   
  
  </body>
</html>
Get memory uses in php


<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>fadeToggle demo</title>

  </head>
  <body>

    <?php

    function Memory_Usage($decimals = 2) {
      $result = 0;

      if (function_exists('memory_get_usage')) {
        $result = memory_get_usage() / 1024;
      } else {
        if (function_exists('exec')) {
          $output = array();

          if (substr(strtoupper(PHP_OS), 0, 3) == 'WIN') {
            exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output);

            $result = preg_replace('/[\D]/', '', $output[5]);
          } else {
            exec('ps -eo%mem,rss,pid | grep ' . getmypid(), $output);

            $output = explode('  ', $output[0]);

            $result = $output[1];
          }
        }
      }

      return number_format(intval($result) / 1024, $decimals, '.', '');
    }
      ?>
<?php
echo Memory_Usage();
?>
  </body>
</html>

বুধবার, ২০ নভেম্বর, ২০১৩

Ajax Login form with jQuery and PHP

 index.php

<div class="loginform-in">
<h1>User Login</h1>
<div class="err" id="add_err"></div>
<fieldset>
    <form action="./" method="post">
        <ul>
            <li> <label for="name">Username </label>
            <input type="text" size="30"  name="name" id="name"  /></li>
            <li> <label for="name">Password</label>
            <input type="password" size="30"  name="word" id="word"  /></li>
            <li> <label></label>
            <input type="submit" id="login" name="login" value="Login" class="loginbutton" ></li>
        </ul>
         </form>   
</fieldset>
</div>

Login.php 

 

require_once 'config.php';

session_start();
$uName = $_POST['name'];
$pWord = md5($_POST['pwd']);
$qry = "SELECT usrid, username, oauth FROM usermeta WHERE username='".$uName."' AND pass='".$pWord."' AND status='active'";
$res = mysql_query($qry);
$num_row = mysql_num_rows($res);
$row=mysql_fetch_assoc($res);
if( $num_row == 1 ) {
    echo 'true';
    $_SESSION['uName'] = $row['username'];
    $_SESSION['oId'] = $row['orgid'];
    $_SESSION['auth'] = $row['oauth'];
    }
else {
    echo 'false';

Login.Ajax

$(document).ready(function(){
    $("#add_err").css('display', 'none', 'important');
     $("#login").click(function(){   
          username=$("#name").val();
          password=$("#word").val();
          $.ajax({
           type: "POST",
           url: "login.php",
            data: "name="+username+"&pwd="+password,
           success: function(html){   
            if(html=='true')    {
             //$("#add_err").html("right username or password");
             window.location="dashboard.php";
            }
            else    {
            $("#add_err").css('display', 'inline', 'important');
             $("#add_err").html("<img src='images/alert.png' />Wrong username or password");
            }
           },
           beforeSend:function()
           {
            $("#add_err").css('display', 'inline', 'important');
            $("#add_err").html("<img src='images/ajax-loader.gif' /> Loading...")
           }
          });
        return false;
    });
});

 

CSS

.loginform-in {
    background: none repeat scroll 0 0 #FFFFFF;
    border: 1px solid #D6E5F5;
    border-radius: 3px 3px 3px 3px;
    height: 345px;
    margin: auto;
    padding: 0;
    width: 460px;
}

.loginform-in fieldset {
    border-bottom: 1px solid #EFEFEF;
    margin: 20px auto;
    padding: 0 0 10px;
    width: 87%;
}

.loginbutton {
    background: -moz-linear-gradient(center top , #F1F1F1, #E0E0E0) repeat scroll 0 0 transparent;
    border: 1px solid #A7A7A7;
    border-radius: 3px 3px 3px 3px;
    color: #444444;
    cursor: pointer;
    font-family: "Helvetica Neue";
    font-size: 13px;
    font-weight: normal;
    height: 29px;
    letter-spacing: 1px;
    width: 92px;
}

.loginform-in fieldset label {
    color: #7D7D7D;
    float: left;
    font-family: "Helvetica Neue";
    font-size: 14px;
    font-weight: bold;
    padding: 8px 0 0;
    width: 140px;
}

.loginform-in fieldset input[type="text"], input[type="password"], fieldset select {
    border: 1px solid #CBC7C5;
    border-radius: 3px 3px 3px 3px;
    float: left;
    height: 33px;
    padding: 1px 1px 1px 3px;
    width: 250px;
}

সোমবার, ১৮ নভেম্বর, ২০১৩

Time Diffrence:

   
    <?php 
$time_one = new DateTime('2013-11-18 04:03:54');
$time_two = new DateTime(date('Y-m-d H:i:s'));
$difference = $time_two->diff($time_one);
echo $difference->format('%h'.':'.'%i'.':'.'%s');
echo '<br/>';

?>
Pagination In codeigniter

The Model:

class Countries extends CI_Model
{
    public function __construct() {
        parent::__construct();
    }
 
    public function record_count() {
        return $this->db->count_all("Country");
    }
 
    public function fetch_countries($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get("Country");
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
}



The Controller:

<?php
class Welcome extends CI_Controller 
{
    public function __construct() {
        parent:: __construct();
        $this->load->helper("url");
        $this->load->model("Countries");
        $this->load->library("pagination");
    }
 
    public function example1() {
        $config = array();
        $config["base_url"] = base_url() . "welcome/example1";
        $config["total_rows"] = $this->Countries->record_count();
        $config["per_page"] = 20;
        $config["uri_segment"] = 3;
 
        $this->pagination->initialize($config);
 
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["results"] = $this->Countries->
            fetch_countries($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
 
        $this->load->view("example1", $data);
    }
}

The View

<body>
 <div id="container">
  <h1>Countries</h1>
  <div id="body">
<?php
foreach($results as $data) {
    echo $data->Name . " - " . $data->Continent . "<br>";
}
?>
   <p><?php echo $links; ?></p>
  </div>
  <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
 </div>
</body>

রবিবার, ১৭ নভেম্বর, ২০১৩

Multiple Checkbox Select/Deselect Using JQuery – Tutorial With Example


The HTML


<HTML>
<HEAD>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <TITLE>Multiple Checkbox Select/Deselect - DEMO</TITLE>
</HEAD>
<BODY>
    <H2>Multiple Checkbox Select/Deselect - DEMO</H2>
<table border="1">
<tr>
    <th><input type="checkbox" id="selectall"/></th>
    <th>Cell phone</th>
    <th>Rating</th>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="1"/></td>
    <td>BlackBerry Bold 9650</td>
    <td>2/5</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="2"/></td>
    <td>Samsung Galaxy</td>
    <td>3.5/5</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="3"/></td>
    <td>Droid X</td>
    <td>4.5/5</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="4"/></td>
    <td>HTC Desire</td>
    <td>3/5</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="5"/></td>
    <td>Apple iPhone 4</td>
    <td>5/5</td>
</tr>
</table>
 
</BODY>
</HTML>

<SCRIPT language="javascript">
$(function(){
 
    // add multiple select / deselect functionality
    $("#selectall").click(function () {
          $('.case').attr('checked', this.checked);
    });
 
    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    $(".case").click(function(){
 
        if($(".case").length == $(".case:checked").length) {
            $("#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }
 
    });
});
</SCRIPT>

বুধবার, ১৩ নভেম্বর, ২০১৩

Very Simple and easy pagination:

<?php
require_once "config.php";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

    <head>

        <title>Pagination || http://www.w3programmers.com</title>

        <link rel="stylesheet" type="text/css" href="styel.css" />

    </head>

    <body>

        <table width="400" cellspacing="2" cellpadding="2" align="center" style="border:1px #000000 solid;">

<?php
$perpage = 5;

if (isset($_GET["page"])) {

    $page = intval($_GET["page"]);
} else {

    $page = 1;
}

$calc = $perpage * $page;

$start = $calc - $perpage;

$result = mysql_query("select * from post Limit $start, $perpage");

$rows = mysql_num_rows($result);

if ($rows) {

    $i = 0;

    while ($post = mysql_fetch_array($result)) {
        ?>

                    <tr style="background-color: #cccccc;">

                        <td style="font-weight: bold;font-family: arial;"><?php echo $post["title"]; ?></td>

                    </tr>

                    <tr>

                        <td style="font-family: arial;padding-left: 20px;"><?php echo $post["detail"]; ?></td>

                    </tr>

                    <?php
                }
            }
            ?>

        </table>

        <br/>

        <table width="400" cellspacing="2" cellpadding="2" align="center" >

            <tr>

                <td align="center">

            <?php
            if (isset($page)) {

                $result = mysql_query("select Count(*) As Total from post");

                $rows = mysql_num_rows($result);

                if ($rows) {

                    $rs = mysql_fetch_array($result);

                    $total = $rs["Total"];
                }

                $totalPages = ceil($total / $perpage);

                if ($page <= 1) {

                    echo "<span id='page_links' style='font-weight:bold;'>Prev</span>";
                } else {

                    $j = $page - 1;

                    echo "<span><a id='page_a_link' href='index.php?page=$j'>< Prev</a></span>";
                }

                for ($i = 1; $i <= $totalPages; $i++) {

                    if ($i <> $page) {

                        echo "<span><a href='index.php?page=$i' id='page_a_link'>$i</a></span>";
                    } else {

                        echo "<span id='page_links' style='font-weight:bold;'>$i</span>";
                    }
                }

                if ($page == $totalPages) {

                    echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";
                } else {

                    $j = $page + 1;

                    echo "<span><a href='index.php?page=$j' id='page_a_link'>Next</a></span>";
                }
            }
            ?>

                    <td>

                        </tr>

                        </table>

                        </body>

                        </html>




And Add this css code:

page_links
 {
  font-family: arial, verdana;
  font-size: 12px;
  border:1px #000000 solid;
  padding: 6px;
  margin: 3px;
  background-color: #cccccc;
  text-decoration: none;
 }
 #page_a_link
 {
  font-family: arial, verdana;
  font-size: 12px;
  border:1px #000000 solid;
  color: #ff0000;
  background-color: #cccccc;
  padding: 6px;
  margin: 3px;
  text-decoration: none;
 }