Friday, 2 August 2013

PHP Tutorial

Show Records Using Gridview

My table named users have three field (roll,name,city)

<style>

.font_color
{
color:#666655;
font-size:15px;
text-align:justify;
}



</style>

<?php
            if(isset($_POST['submit']))

              {
           $con=mysql_connect("localhost","root","");
  if(!$con)
 {
die("Could not connect to database! ".mysql_error($conn));
 }
  mysql_select_db("bsw",$con);         $quary=mysql_query("select * from users");
$c="#E6FFFF";
echo "<table cellpadding='5' border=0 class='font_color'>";
echo "<tr style='background-color:#00FFFF'><th>User id</th><th>Name </th><th> Type </th><th>Course</th><th>Email</th><th>User Name</th><th>Password</th></tr>";
while($r=mysql_fetch_array($quary))
{
$c=$c=='#E6FFFF'?'#FFCCFF':'#E6FFFF';
echo "<tr style='background-color:$c' onmouseover=this.style.backgroundColor='#FFFF66' onmouseout=this.style.backgroundColor='$c'> <td align=center > ". $r[6]. "</td>". "<td>" .$r[2]. "</td>"."<td>" .$r[3]."</td>"."<td>".$r[5]."</td>". "<td>".$r[4]."</td>"."<td>".$r[0]."</td>"."<td>".$r[1]."</td>
</tr>
";

}
echo "</table>";
}
?>

Ajax implementation using PHP

AJAX stands for Asynchronous JavaScript and XML. AJAX is a new technique for creating better, faster, and more interactive web applications with the help of XML, HTML, CSS and Java Script.Conventional web application trasmit information to and from the sever using synchronous requests. This means you fill out a form, hit submit, and get directed to a new page with new information from the server.With AJAX when submit is pressed, JavaScript will make a request to the server, interpret the results and update the current screen. In the purest sense, the user would never know that anything was even transmitted to the server.XML is commonly used as the format for receiving server data, although any format, including plain text, can be used.AJAX is a web browser technology independent of web server software.A user can continue to use the application while the client program requests information from the server in the backgroundIntuitive and natural user interaction. No clicking required only Mouse movement is a sufficient event trigger.Data-driven as opposed to page-driven

Retrieve  record  from  MySQL

  • Save Following script with Ajax.php
  • Make a table named stu(roll,name,city,course)


<!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=iso-8859-1" />
<title>Ajax demo</title>
<script>
function showInfo(str)
{

if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","t.php?arg="+str,true);
xmlhttp.send();
}
</script>
</head>

<body>
<form>
<select name="users" onchange="showInfo(this.value)">
<option value="">Select a person:</option>
<option value="bca">bca</option>
<option value="mca">mca</option>
<option value="mba">mba</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>


  • Now Save the following script in t.php 
<?php
$q=$_GET["arg"];
$con = mysql_connect('localhost','root','');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysql_select_db("bsw",$con);
$sql="SELECT * FROM stu WHERE course = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Roll</th>
<th>Name</th>
<th>City</th>
<th>Course</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['roll'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['city'] . "</td>";
  echo "<td>" . $row['course'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

?>

Show record in Column wise

There are many time when we want to show records column wise
PHP has a rich way to interact with table column you don’t need to track of column and their name just use some powerful  php function
In the following code 
$cols=mysql_num_fields($q);
Cols will assign no of column in stu table . you can use this number to process records using loop.
$row=mysql_fetch_row($q)
$row will assign with row from stu table with all field I use row for print each record using loop. This way of printing gave me option to show record in tabular form. I hope you’ll enjoy this code..


<?php
require('mylib.php');
if(isset($_GET['column']))
{
connect();
$q=mysql_query("select * from stu");
$cols=mysql_num_fields($q);
echo "<table border=1><tr>";
for($i=0;$i<$cols;$i++)
echo "<th>". mysql_field_name($q,$i) ."</th>";
echo "</tr>";
while($row=mysql_fetch_row($q))
{
echo "<tr>";
for($i=0;$i<$cols;$i++)
echo "<td>".$row[$i]."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
<!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=iso-8859-1" />
<title>Select Statement Demo</title>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="get">
<input type="submit" value="Show With Counting Column" name="column" />
</form>
</body>
</html>