php mysql compare two rows in the same table - Web Development and Web Design Codes

Latest

Wednesday, May 31, 2017

php mysql compare two rows in the same table

Compare Between Two Row form same Table using PHP MySQL

php mysql compare two rows
php mysql compare two rows

Hi Guys Today we are going to learn How to Compare Table Data using PHP MySQL.
You Can Create Compare System Like Global Firepower Using The Below Codes.
Also Learn
1. PHP MySQL Pagination
2. PHP MySQL Login with SESSION
3. php mysql comment system
4. sort MySQL table column using PHP

5. PHP MySQL Shopping Cart

What you will Need:

DataBase Creation:
Database Name: codenair
table Name: country
Use The Below SQL Codes to Create table country
CREATE TABLE IF NOT EXISTS `country` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `sort` varchar(150) NOT NULL,
  `army` varchar(50) NOT NULL,
  `aircraft` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Now Use Below SQL Codes to Insert Some Row.
INSERT INTO `country` (`id`, `name`, `sort`, `army`, `aircraft`) VALUES
(1, 'Bangladesh', 'BD', '400000+', '220+'),
(2, 'India', 'IN', '1230000+', '1700+'),
(3, 'America', 'USA', '1300000+', '12000+'),
(4, 'United Kingdom', 'UK', '500000+', '500+'),
(5, 'Nepal', 'NP', '230000+', '100+'),
(6, 'Bulgaria', 'BG', 'Unknown', '50+');

Files Creation:
Create 3 files Named.
1.connect.php
2.index.php
3.style.css
php mysql compare two rows
php mysql compare two rows

copy or write the below codes manually for below files..
connect.php
Creating Connection Between Server and Database.
<?php
$conn=new mysqli('localhost','root','','codenair');
?>

index.php
<html>
<head>
<title>Compare Two Row from Table Like as GlobalFirePower</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<div id="form">
<h1>Compare Between Two Country Like as GlobalFirePower</h1>
<form method="POST" action="">
<table>
    <tr>
   <td><span>Country 1:</span></td>
   <td>
      <select type="text" name="country1">
    <option value="America">United States of America</option>
          <option value="India">Republic India</option>
          <option value="Nepal">Nepal</option>
          <option value="United Kingdom">United Kingdom</option>
          <option value="Bangladesh">Bangladesh</option>
   </select>
   </td>
 </tr>
   <tr>
   <td><span>Country 2:</span></td>
   <td>
      <select type="text" name="country2">
    <option value="America">United States of America</option>
          <option value="India">Republic India</option>
          <option value="Nepal">Nepal</option>
          <option value="United Kingdom">United Kingdom</option>
          <option value="Bangladesh">Bangladesh</option>
   </select>
   </td>
 </tr>
 <tr>
 <td></td>
 <td><input type="submit" name="submit" value="Compare"/></td>
 </tr>
  </table>
</form>
</div>
<?php
include("connect.php");
if(isset($_POST['submit'])){
 $country1=$_POST['country1'];
 $country2=$_POST['country2'];
//For Country 1
$result=mysqli_query($conn,"SELECT*FROM country WHERE name='$country1'");
//Fetching Data for country 1
$row1=$result->fetch_assoc();
if($result->num_rows==0){
 $error='Selected Country Not Exist in  Our Database';
}
//Query for 2nd Country
$result2=mysqli_query($conn,"SELECT*FROM country WHERE name='$country2'");
$row2=$result2->fetch_assoc();
 if($result2->num_rows==0){
  $error='Selected Country Not Exist in  Our Database';
 }
 if(!isset($error)){
?> 
<div id="left">
<table id="compare">
  <tr>
  <th>Name</th>
  <th>Sort Name</th>
  <th>Armed Force</th>
  <th>Aircraft</th>
   
  </tr>
  <tr>
 <td><?php echo $row1['name'];?></td>
 <td><?php echo $row1['sort'];?></td>
 <td><?php echo $row1['army'];?></td>
 <td><?php echo $row1['aircraft'];?></td>
  
</tr>
  <tr>
 <td><?php echo $row2['name'];?></td>
 <td><?php echo $row2['sort'];?></td>
 <td><?php echo $row2['army'];?></td>
 <td><?php echo $row2['aircraft'];?></td>
</tr>
</table>
</div>
<?php } }?>
<div id="error"><?php if(isset($error)){echo $error;}?></div>
</html>

style.css
Now add some css to Design our Index Page
#form{
 width:800px;
 height:250px;
 background:#ddd;
 color:black;
 margin:auto;
}
 span{
  font-weight:bold;
  font-size:20px;
  color:green;
  }
  select {
    padding:3px;
    margin: 0;
 font-size:15px;
 width:185px;
    border-radius:4px;
    background: white;
    color:#888;

    -webkit-appearance:none;
    -moz-appearance:none;
    appearance:none;
    cursor:pointer;
}
input[type=submit]{
 padding:5px 8px;
 border-radius:5px;
 background:#080808;
 color:white;
 cursor:pointer;
}
#error{
 font-size:20px;
 color:green;
 font-weight:bold;
}
 
#left{
 background:#ddd;
 width:800px;
 margin:auto;
}
#compare td{
 border:1px solid red;
 background:lighter;
 text-align:left;
}
table{
 width:100%;
 border-collapse:collapse;
 margin:auto
}
td{
 padding:8px;
  
}
#compare th{
 padding:10px;
 font-weight:bold;
 font-size:20px;
 background:#080809;
 color:white;
}
#compare tr:nth-child(odd){background:lightgreen}
#error{
 font-size:20px;
 text-align:center;
 color:red;
}

Now You have Successfully Created PHP MySQL Data Compare System
Thank You for Visiting.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.