Wednesday, November 20, 2013

Drop Down List in PHP

Create file Index.php and Paste the following code :
<?php
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('prem');
$query="SELECT * FROM country";
$result=mysql_query($query);
?>
<html>
<head>
<title>Country State City Dropdown Using Ajax</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
function getXMLHTTP() { //fuction to return the xml http object
                                var xmlhttp=false;          
                                try{
                                                xmlhttp=new XMLHttpRequest();
                                }
                                catch(e)               {                             
                                                try{                                        
                                                                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
                                                }
                                                catch(e){
                                                                try{
                                                                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                                                                }
                                                                catch(e1){
                                                                                xmlhttp=false;
                                                                }
                                                }
                                }
                                               
                                return xmlhttp;
    }
               
                function getState(countryId) {                  
                               
                                var strURL="findState.php?country="+countryId;
                                var req = getXMLHTTP();
                               
                                if (req) {
                                               
                                                req.onreadystatechange = function() {
                                                                if (req.readyState == 4) {
                                                                                // only if "OK"
                                                                                if (req.status == 200) {                                                                                  
                                                                                                document.getElementById('statediv').innerHTML=req.responseText;
                                                                                                document.getElementById('citydiv').innerHTML='<select name="city">'+
                                                                                                '<option>Select City</option>'+
                                                                        '</select>';                                                                                
                                                                                } else {
                                                                                                alert("Problem while using XMLHTTP:\n" + req.statusText);
                                                                                }
                                                                }                                                             
                                                }                                             
                                                req.open("GET", strURL, true);
                                                req.send(null);
                                }                             
                }
                function getCity(countryId,stateId) {                     
                                var strURL="findCity.php?country="+countryId+"&state="+stateId;
                                var req = getXMLHTTP();
                               
                                if (req) {
                                               
                                                req.onreadystatechange = function() {
                                                                if (req.readyState == 4) {
                                                                                // only if "OK"
                                                                                if (req.status == 200) {                                                                                  
                                                                                                document.getElementById('citydiv').innerHTML=req.responseText;                                                                                      
                                                                                } else {
                                                                                                alert("Problem while using XMLHTTP:\n" + req.statusText);
                                                                                }
                                                                }                                                             
                                                }                                             
                                                req.open("GET", strURL, true);
                                                req.send(null);
                                }
                                                               
                }
</script>

<style type="text/css">
body {font-family: Arial, "Trebuchet MS";font-size: 17px;color: #52B6EB; }
a{font-weight: bold;letter-spacing: -1px;color: #52B6EB;text-decoration:none;}
a:hover{color: #99A607;text-decoration:underline;}
#top{width:43%;margin-top: 25px; height:60px; border:1px solid #BBBBBB; padding:10px;}
#tutorialHead{width:43%;margin-top: 12px; height:30px; border:1px solid #BBBBBB; padding:11px;}
.tutorialTitle{width:95%;float:left;color:#99A607}
.tutorialTitle  a{float:right;margin-left:20px;}
.tutorialLink{float:right;}
table
{
margin-top:70px;
border: 1px solid #BBBBBB;
padding:25px;
height: 35px;
}
</style>
</head>
<body>
<form method="post" action="insert.php" name="form1">
<center>
<div id='top'>
         <a href="http://www.technaitra.com" title="Technaitra Solutions" target="blank">
             <img src="image/mainlogo.png" alt="Technaitra Solutions" title="Technaitra Solutions" border="0"/>
         </a>
</div>

    <div id='tutorialHead'>
         <div class="tutorialTitle"><b>Country State City Dropdown Using Ajax</b>
          <a href="http://phpwithsmile.blogspot.in" title="Country State City Dropdown Using Ajax">Tutorial Link</a>
    </div>
</div>


<table width="45%"  cellspacing="0" cellpadding="0">
  <tr>
    <td width="75">Country</td>
     <td width="50">:</td>
    <td  width="150"><select name="country" onChange="getState(this.value)">
                <option value="">Select Country</option>
                <?php while ($row=mysql_fetch_array($result)) { ?>
                <option value=<?php echo $row['id']?>><?php echo $row['country']?></option>
                <?php } ?>
                </select></td>
  </tr>
  <tr style="">
    <td>State</td>
    <td width="50">:</td>
    <td ><div id="statediv"><select name="state" >
                <option>Select State</option>
        </select></div></td>
  </tr>
  <tr style="">
    <td>City</td>
    <td width="50">:</td>
    <td ><div id="citydiv"><select name="city">
                <option>Select City</option>
        </select></div></td>
  </tr>
 
</table>
</center>

<input name="Submit" type="Submit" value="Submit">

</form>
</body>
</html>

Create file findstate.php and Paste the following code :


<?php
error_reporting(0);

$country=intval($_GET['country']);
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('prem');
$query="SELECT id,statename FROM state WHERE countryid='$country'";
$result=mysql_query($query);

?>
<select name="state" onchange="getCity(<?php echo $country?>,this.value)">
<option>Select State</option>
<?php while ($row=mysql_fetch_array($result)) { ?>
<option value=<?php echo $row['id']?>><?php echo $row['statename']?></option>
<?php } ?>
</select>

Create file findcity.php and Paste the following code :

<?php
error_reporting(0);

$countryId=intval($_GET['country']);
$stateId=intval($_GET['state']);
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('prem');
$query="SELECT id,city FROM city WHERE countryid='$countryId' AND stateid='$stateId'";
$result=mysql_query($query);

?>
<select name="city">
<option>Select City</option>
<?php while($row=mysql_fetch_array($result)) { ?>
<option value=<?php echo $row['id']?>><?php echo $row['city']?></option>
<?php } ?>
</select>

Create file insert.php and Paste the following code :

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<?php
error_reporting(0);

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("prem", $con);

$sql="INSERT INTO result (country,state,city)
VALUES
('$_POST[country]','$_POST[state]','$_POST[city]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>

</body>
</html>


Database File:
Create Database with name prem and paste the following in SQL :
/* Create table country */

CREATE TABLE `country` (
  `id` tinyint(4) NOT NULL auto_increment,
  `country` varchar(20) NOT NULL default '',
  PRIMARY KEY  (`id`)
)



/*create table state*/

CREATE TABLE `state` (
  `id` tinyint(4) NOT NULL auto_increment,
  `countryid` tinyint(4) NOT NULL,
  `statename` varchar(40) NOT NULL,
  PRIMARY KEY  (`id`)
)



/* Create table city */

CREATE TABLE `city` (
  `id` tinyint(4) NOT NULL auto_increment,
  `city` varchar(50) default NULL,
  `stateid` tinyint(4) default NULL,
  `countryid` tinyint(4) NOT NULL,
  PRIMARY KEY  (`id`)
)





/* Insert records into country table */

INSERT INTO `country` VALUES (1, 'USA');
INSERT INTO `country` VALUES (2, 'Canada');




/* Insert records into state table */

INSERT INTO `state` VALUES (1, 1, 'New York');
INSERT INTO `state` VALUES (2, 1, 'Los Angeles');
INSERT INTO `state` VALUES (3, 2, 'British Columbia');
INSERT INTO `state` VALUES (4, 2, 'Toranto');




/* Insert records into city table */

INSERT INTO `city` VALUES (1, 'Los Angales', 2, 1);
INSERT INTO `city` VALUES (2, 'New York', 1, 1);
INSERT INTO `city` VALUES (3, 'Toranto', 4, 2);
INSERT INTO `city` VALUES (4, 'Vancovour', 3, 2);


/* Create table Result*/

CREATE TABLE `result` (
  `id` tinyint(4) NOT NULL auto_increment,
  `city` varchar(50) default NULL,
   `state` varchar(50) default NULL,
 `country` varchar(50) default NULL,
  PRIMARY KEY  (`id`)
)




Friday, November 15, 2013

Log In Form

Simple Login, Logout In PHP

Step :1. Make database of sessions in phpmyadmin.

Step:2. Create table sessions and users

Step:3. Create columns in sessions (session_id, userid, username, ip_addr, session_start, session_end).
                Create columns in users (id,username,password).


Step:4. Make file connect.php.
<?php
                $mysql_host = 'localhost';
                $mysql_user = 'root';
                $mysql_password = '';
                $mysql_database = 'sessions';

                mysql_connect($mysql_host, $mysql_user, $mysql_password);
                mysql_select_db($mysql_database);

?>


Step:5 Make file login.php.
<?php
                error_reporting(0);
                session_start();
                $sid = session_id();


                $userid = $_SESSION['userid'];
                $username = $_SESSION['username'];
?>

<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>Login</title>
</head>
<body>
<?php
if ($userid && $username) {
echo "You are already logged in as <b>$username</b>. Do you wish to
 <a href ='./logout.php'>logout</a>?";
}
 Else
 {
$form = "<form action = './login.php' method = 'POST'>
<table>
<tr>
<td>Username:</td>
<td><input type = 'text' name = 'user' /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type = 'password' name = 'password' /></td>
</tr>
<tr>
<td></td>
<td><input type = 'submit' name = 'loginbtn' value = 'Login'/></td>
</tr>
</table>
</form>";
if ($_POST['loginbtn']) {
$user = $_POST['user'];
$password = $_POST['password'];
if ($user) {
if ($password) {
require("connect.php");
$query = "SELECT * FROM users WHERE username = '$user'";
$query_run = mysql_query($query);
$numrows = mysql_num_rows($query_run);
if ($numrows == 1) {
$row = mysql_fetch_assoc($query_run);
$dbid = $row['id'];
$dbuser = $row['username'];
$dbpass = $row['password'];
if ($password == $dbpass) {                                       
$_SESSION['userid'] = $dbid;
$_SESSION['username'] = $dbuser;
$http_client_ip = $_SERVER['HTTP_CLIENT_IP'];
$http_x_fowarded_for = $_SERVER['HTTP_X_FOWARDED_FOR'];
$remote_addr = $_SERVER['REMOTE_ADDR'];
if (!empty($http_client_ip)) {
$ip = $http_client_ip;
}
 elseif (!empty($http_x_fowarded_for)) {
$ip = $http_x_fowarded_for;
}
else
 {
$ip = $remote_addr;
}
$time = time();
$actual_time = date('d M Y @ H:i:s', $time);
mysql_query("INSERT INTO sessions VALUES ('$sid', '$dbid', '$dbuser', '$ip', '$actual_time', '')");
echo "You have been logged in as <b>$dbuser</b>. Click <a href = './logout.php'>here<a> to logout.";
                                               
}
else
 {
echo "You did not enter the correct password. $form";
}
                                                                               
}
else
 {
echo "The username: <b>$user</b> was not found. $form";
}
                               
mysql_close();
}
else
 {
echo "You must enter your password. $form";
}
                                               
}
else
{
echo "You must enter your username. $form";
}
}
else
{
echo $form;
}
}
?>
</body>
</html>


Step:6 Make file logout.php.
<?php
                error_reporting(0);
                session_start();
                $sid = session_id();
                $userid = $_SESSION['userid'];
                $username = $_SESSION['username'];

?>

<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>Logout</title>
</head>
<body>
<?php
$time = time();
$actual_time = date('d M Y @ H:i:s', $time);

if ($userid && $username) {
session_destroy();

require("./connect.php");
mysql_query("UPDATE sessions SET session_end = '$actual_time' WHERE session_id = '$sid' AND session_end = ''");

header( 'Location: ./login.php' ) ;

mysql_close();

}
else
{
echo "You are not logged in.";
}

?>
</body>
</html>


Modify Data into Database ( Fetch Edit and Delete ) code also included i this tutorial


Basic Fetch, Edit, Update, Delete commands in PHP.

Step :1. Make database of college in phpmyadmin.

Step:2. Create table student

Step:3. Create columns  s_id, s_name, s_class.
(Note:- Make auto increment for s_id)

Step:4. Make a form in HTML.

//  Make a file student.php

<!DOCTYPE html>
<html>
<body>

<form action="insert.php" method="post">  //*Attached insert.php file with this form
Name: <input type="text" name="name"><br>
Class: <input type="text" class="class"><br>
<input type="submit" value="submit" />
</form>
</body>
</html>

Coding for insert data dynamically in this form:-

Make file insert.php.


<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("college", $con);

$sql="INSERT INTO student (name, class)
VALUES
('$_POST[name]','$_POST[class]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>


Coding for Fetch data dynamically in this form:-

Step:1. Make a file config.php.


<?php
error_reporting(0);
$con = mysql_connect("localhost","root","");
mysql_select_db("college");
?>

Step:2. Make a file fetchdata.php.
<?php
include("config.php");
$select = mysql_query("select * from student");
?>
<table border="2">
<tr>
<td>s_id</td>
<td>s_name</td>
<td>s_class</td>
<td colspan="2">Operation</td>
</tr>
<?php
while($row=mysql_fetch_array($select))
{
?>
<tr> <td> <?php               echo $row['s_id']."<br>"; ?> </td>
 <td> <?php       echo $row['s_name ']."<br>"; ?> </td> 
 <td> <?php       echo $row['s_class']."<br>"; ?> </td>
 <td>  <a href="edit.php?
 's_id =<?php echo $row[''s_id ']?>&
 s_name =<?php echo $row[s_name ']?>&
  s_class =<?php echo $row[' s_class ']?>"> Edit </a></td>
 <td> <a href="delete.php?s_id=<?php echo $row[s_id]?>">Delete</a> </td> <?php

 </tr>
<?php
}
?>
(Note:-  We attached edit.php and delete.php files in Edit and Delete for Edit and Delete in this form)



// Coding for Edit data dynamically in this form:-

// Make file edit.php.


<?php
$s_id=$_GET[' s_id '];
$s_name=$_GET[' s_name '];
$s_class =$_GET['s_class'];
?>

<form action="update.php" method="post">  //*Attached update.php file to update in this form.
<table border="2">
<tr>
<td>s_id</td>
<td> s_name </td>
<td> s_class </td>
</tr>

<tr>
<input type="hidden" name="s_id" value="<?php echo $ s_id; ?>" >
<td><input type="text" > s_name ="id" value="<?php echo $> s_name; ?>" ></td>
<td><input type="text" s_class ="class" value="<?php echo $ s_class; ?>" ></td>
</tr>

<tr>
<td colspan="3" align="center"><input type="submit" ></td>
</tr>

</table>
</form>



Coding for Update data dynamically in this form:-

Make file update.php.

<?php
include("config.php");


$s_id =$_GET[' s_id '];
$s_name =$_GET[' s_name '];
$s_class=$_GET[s_class];

if(!empty($s_name))
{
                $update=mysql_query("update student set s_name ='$ s_name where s_ id='$s_id'");
}

if(!empty($s_class))
{
                $update=mysql_query("update student set s_class ='$ s_class where s_ id='$s_ id'");
}
header("location:fetchdata.php");//*//*Attached fetchdata.php file here to show files after update.   
?>


  
Coding for Delete data dynamically in this form:-

 Make delete.php

<?php include("config.php");

$s_id=$_GET[' s_id '];
$delete = mysql_query("delete from student where s_id ='$ s_id '");
if($delete)
{
header("location:fetchdata.php");  //*Attached fetchdata.php file here to show files after delete
}
else
{
echo "error";
}
?>










































Print Command in PHP

// Print Command in PHP

<input type="button" onClick="window.print()" value="Printe values"/>

Thursday, November 7, 2013

Tips on how to Install This new Font in a Few Simple steps in a Web page


// Tips on how to Install This new Font in a Few Simple steps in a Web page

<html>
<head>
<style> 

@font-face
{
font-family: myFirstFont;
src: url(sansation_light.woff);
}

div
{
font-family:myFirstFont;
}
</style>
</head>
<body>

<div>
With CSS3, websites can finally use fonts other than the pre-selected "web-safe" fonts.
</div>

<p><b>Note:</b> Internet Explorer 8 and earlier, do not support the @font-face rule.</p>

</body>
</html>

Increase Size of import File in phpmyadmin

Try these different settings in C:\wamp\bin\apache\apache2.2.8\bin\php.ini

Find:
post_max_size = 8M
upload_max_filesize = 2M
max_execution_time = 30
max_input_time = 60
memory_limit = 8M

Change to:
post_max_size = 750M
upload_max_filesize = 750M
max_execution_time = 5000
max_input_time = 5000
memory_limit = 1000M

Then restart wamp to take effect

Have fun,

Thursday, October 24, 2013

Functions

-----------------------------------------------------------------
Average

1. SELECT AVG(fee) FROM student;

2. SELECT id, name FROM student
WHERE fee>(SELECT AVG(fee) FROM student);
------------------------------------------------------
Count

1. SELECT COUNT(*) FROM student;


Primary Key

// How we can create a Primary Key

CREATE TABLE student3
(
rollno int NOT NULL PRIMARY KEY,
name varchar(255) NOT NULL,
class varchar(255)
)

INNER JOIN


-------------------------------------------------------------------------------
//Make database and table

-- Database: `inventory`

-- Table Name -  `orders`
--

CREATE TABLE IF NOT EXISTS `orders` (
  `order_id` int(11) NOT NULL,
  `customer_id` int(11) NOT NULL,
  `order_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `orders`
--

INSERT INTO `orders` (`order_id`, `customer_id`, `order_date`) VALUES
(1, 1, '2013-10-25'),
(2, 2, '2013-10-26'),
(3, 3, '2013-10-27'),
(4, 4, '2013-10-28'),
(5, 5, '2013-10-29'),
(6, 6, '2013-10-30'),
(7, 7, '2013-10-31');

--------------------------------------------------------------------------------

//Make database and table

-- Database  Name  -  : `inventory`
-- Table Name  -   `customers`
--

CREATE TABLE IF NOT EXISTS `customers` (
  `customer_id` int(11) NOT NULL,
  `customer_name` varchar(222) NOT NULL,
  `contact_name` varchar(222) NOT NULL,
  `country` varchar(222) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `customers`
--

INSERT INTO `customers` (`customer_id`, `customer_name`, `contact_name`, `country`) VALUES
(1, 'smile Mittal', 'smile', 'india'),
(2, 'honey jindal', 'honey', 'india'),
(7, 'neha', 'n', 'n'),
(8, 'vipin', 'v', 'v'),
(9, 'l', 'l', 'l'),
(10, 'p', 'p', 'p');

---------------------------------------------------------------------------------------------

Querry of mysql for INNER JOIN

SELECT orders.order_id, customers.customer_name, orders.order_date
FROM orders
INNER JOIN customers
ON orders.customer_id=customers.customer_id;

--------------------------------------------------------------------------------------------
Querry of mysql for LEFT JOIN

SELECT customers.customer_name, orders.order_id
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id

ORDER BY customers.customer_name;

--------------------------------------------------------------------------------------------
Querry of mysql for RIGHT JOIN
SELECT customers.customer_name, orders.order_id
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id
ORDER BY customers.customer_name;

Wednesday, October 23, 2013

The best way to Create a Web page

// Make a file 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>Web Page</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>
<div class="main"> <!--Main Start-->

<div class="header"> <!--Header Start-->
<h1>Header</h1>
</div> <!--Header End-->

<div class="contentarea"> <!--Contentarea Start-->

<div class="leftside_contentarea">  <!--Leftside Content Area Start-->
<h1>Leftside Content Area</h1>
</div>  <!--leftside Content Area End-->

<div class="rightside_contentarea"> <!--Rightside Content Area Start-->
<h1>Rightside Content Area</h1
></div> <!--Rightside Content Area End-->

</div> <!--Contentarea End-->


<div class="footer"> <!--Footer Start-->
<h1>Footer</h1
></div> <!--Footer End-->

</div> <!--Main End-->
</body>
</html>


//Make a file css.php


* {
margin:0px;
padding:0px;
}
img {
border: 0px;
}
body {
background-color:#deddc2;
}
.main {
margin:0 auto;
height:960px;
width: 1060px;
background-color:#f2f2e9;
-moz-box-shadow: 0 0 20px #454545;
-webkit-box-shadow: 0 0 20px #454545;
box-shadow: 0 0 20px #454545;
}
.header {
height:168px;
background:#c52f38;
}
.contentarea {
height:590px;
background:#d2d3d5;
margin-top:10px;
}
.leftside_contentarea {
width:270px;
float:left;
height:590px;
background:#C03;
}
.rightside_contentarea {
height:590px;
float:right;
width:790px;
background:#030;
}
.footer {
height:170px;
width:1060px;
background:#FF0;
margin-top:10px;
float:left;
}