Home

Sunday, August 21, 2011

In Datagrid Pagination and Sorting with Ajax in PHP

                                                                         Index.php
<style>
.selected{
    color:#F00;
    font-weight:bold;
    background-color:#09F;
}
.selected a{
    color:#FFF;
}
.not_selected{
    color:#000;
    background-color:#F9F;
}
.not_selected a{
    color:#FFF;
}
th{
    background-color:#FFC;
    text-align:center;
}
</style>
<script>
window.onload= function()
{
    Ajax("1","id","DESC");
}

function Ajax(Page,Field,Ord)
{
    var xmlhttp;
    if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }else
    {
        alert("Browser Not Support");
        return false;
    }
   
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState==4)
        {
            document.getElementById("data").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST","data.php?page="+Page+"&fld="+Field+"&ord="+Ord,true);
    xmlhttp.send();
}
</script>
<div id="data"></div>
                                                                data.php
<?php

mysql_connect("localhost","root","") or die("Connection Error".mysql_error());
mysql_select_db("exam") or die("Database Connection Error".mysql_error());


$sql = "SELECT * FROM admin";
$rs = mysql_query($sql) or dir("Select Error.".mysql_error());
$count = mysql_num_rows($rs);

$per_page = 2;
if($_GET['page'])
{
    $page = $_GET['page'];
}else
{
    $page = 1;
}
$pages = $count;
$pages = ceil($pages/$per_page);

if($_GET['ord']=="DESC" && $_GET['fld']!="id")
{
    $ord = "ASC";
}else
{
    $ord = "DESC";
   
}
if($_GET['fld']=="uname")
{
    $order_by = $_GET['fld'];
}else
{
    $order_by = "id";
}

$sql2 = "SELECT * FROM admin order by ".$order_by." ".$ord." LIMIT " . (($page - 1) * $per_page) . ", $per_page";
$rs2 = mysql_query($sql2) or dir("Select Error.".mysql_error());

?>
    <table width="40%" cellpadding="2" cellspacing="2" border="1">
        <tr>
            <th><a href="Javascript:Ajax(<?=$_GET['page'];?>,'uname','<?=$ord;?>')">Firstname</a></th>
            <th>Lastname</th>
            <th>Password</th>
           </tr>
<?
while($row=mysql_fetch_array($rs2))
{
    extract($row);
    ?>
    <tr>
        <td><?=$uname;?></td>
        <td><?=$lname;?></td>
        <td><?=$pass;?></td>
    </tr>
    <?
}
?>
</table>
<?php
if($count>$per_page)
{
    ?>
    <table width="5%" cellpadding="2" cellspacing="2" border="1"><tr><?
for($i=1;$i<=$pages;$i++)
{
    ?>
    <td <?php if($i==$page){ ?>class="selected"<? }else{ ?>class="not_selected"<? } ?>  ><a href="Javascript:Ajax(<?=$i;?>,'<?=$order_by;?>','<?=$ord;?>')"><?=$i;?></a></td>
    <?
}
?>
</tr></table>

    <?
}
?> 

Friday, August 19, 2011

Submit a Form without Refreshing page with jQuery and Ajax.

  jQuery and Ajax script take a look at dataString
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" >
$(function() {
$(".submit").click(function() {
var name = $("#name").val();
var username = $("#username").val();
var password = $("#password").val();
var gender = $("#gender").val();
var dataString = 'name='+ name + '&username=' + username + '&password=' + password + '&gender=' + gender;

if(name=='' || username=='' || password=='' || gender=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "join.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
</script>
HTML Code
<form method="post" name="form">
<ul><li>
<input id="name" name="name" type="text" />
</li><li>
<input id="username" name="username" type="text" />
</li><li>
<input id="password" name="password" type="password" />
</li><li>
<select id="gender" name="gender">
<option value="">Gender</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</li></ul>
<div >
<input type="submit" value="Submit" class="submit"/>
<span class="error" style="display:none"> Please Enter Valid Data</span>
<span class="success" style="display:none"> Registration Successfully</span>
</div></form>
join.php
<?php
if($_POST)
{
$name=$_POST['name'];
$username=$_POST['username'];
$password=$_POST['password'];
$gender=$_POST['gender'];
mysql_query("SQL insert statement.......");
}else { }

?>

Jquery Submit() method Example

$('#formid').submit(function() {
  alert('I am submitted');
  return false;
});
 
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
 
</head>
<body>
<div id="container">
 
  <form action="submit.php">
    <div>
      <input type="submit" />
    </div>
  </form>
  <span></span>
<script>
 
    $("form").submit(function() {
      alert("This is a submit");
       return false;
    });
</script>
</div>
</body>
</html> 

Sunday, August 14, 2011

Very simple PHP pagination

$per_page = 1;
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$pages = implode(mysql_fetch_assoc(mysql_query("SELECT COUNT(*) FROM register")));
$pages = ceil($pages / $per_page);
$querystring = "";
foreach ($_GET as $key => $value) {
    if ($key != "page") $querystring .= "$key=$value&amp;";
}
echo "Pages: ";
for ($i = 1; $i <= $pages; $i++) {
    echo "<a " . ($i == $page ? "class=\"selected\" " : "");
    echo "href=\"?{$querystring}page=$i";
    echo "\">$i</a> ";
}
$result = mysql_query("SELECT * FROM register LIMIT " . (($page - 1) * $per_page) . ", $per_page");


echo "<table border='1'>
<tr>
<th>Username</th>
<th>Address</th>
<th>Area</th>
<th>Email</th>
<th>City</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['uname'] . "</td>";
  echo "<td>" . $row['address'] . "</td>";
  echo "<td>" . $row['area'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['city'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

Monday, August 8, 2011

How to get time from seconds in php


 <?php 
function Sec2Time($time){
  if(
is_numeric($time)){
    
$value = array(
      
"years" => 0"days" => 0"hours" => 0,
      
"minutes" => 0"seconds" => 0,
    );
    if(
$time >= 31556926){
      
$value["years"] = floor($time/31556926);
      
$time = ($time%31556926);
    }
    if(
$time >= 86400){
      
$value["days"] = floor($time/86400);
      
$time = ($time%86400);
    }
    if(
$time >= 3600){
      
$value["hours"] = floor($time/3600);
      
$time = ($time%3600);
    }
    if(
$time >= 60){
      
$value["minutes"] = floor($time/60);
      
$time = ($time%60);
    }
    
$value["seconds"] = floor($time);
    return (array) 
$value;
  }else{
    return (bool) 
FALSE;
  }
}


/* How To Use */
$array = Sec2Time(185);

echo "<pre>";
print_r($array); 


?>
///////// OUTPUT ///////

Array
(
    [years] => 0
    [days] => 0
    [hours] => 0
    [minutes] => 3
    [seconds] => 5
)
rathoddhirendra.blogspot.com-Google pagerank and Worth