Hello there Here we Will display all the data stored in database table in html.
For this example we use the Example code of

Php Program To Connect With Database, Create Table To Connect with database, create table and insert data to the database. And From this code below we can display the stored data.

Q.Write PHP code that retrieves all the districts from database in tabular format. Pokhara University [2012 -Spring] Course: Web Technology

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!doctype html>
<html>
<head>
 <title>All the datas from Table in Database</title>
</head>
<body>
 <?php
 include "connect.php";
 $sql = "SELECT id,name,email FROM info";
 $result = $conn->query($sql);
 echo "<table border='1'>";
  echo "<tr>";
   echo "<th> Id </th>";
   echo "<th> Name </th>";
   echo "<th> Email </th>";
  echo "</tr>";
 while($row = $result->fetch_assoc()) { 
  echo "<tr>";
   echo "<td>" . $row["id"]. "</td>";
   echo "<td>" . $row["name"]. "</td>";
   echo "<td>" . $row["email"]. "</td>";
  echo "</tr>";
 }
 echo "<table>";
 ?>
</body>
</html>

Here is our info Table in Database:
Here is the Output of the Code Above

Id Name Email
1 Sagar Devkota info@devkotasagar.com.np
2 Ram Hari ram@hari.com.np
3 Mr Bean mr@bean.com.np
4 Cristiano Ronaldo Cristiano@Ronaldo.com.np
5 Tom Cruise tom@cruise.com.np
6 Raffey Cassidy Raffey@Cassidy.com.np

This is all. You can find the code in Github https://github.com/InfoDevkota/phpformmysql/tree/master (Master Branch) Issuers and pull requested are accepted.
If something is wrong or Need more clarification  Please contact me: info[at]devkotasagar.com.np.
Change the font size and color of the text using java script We use onmouseover in this example code. You can use onClick and onmouseout too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
 <title>Display a table</title>
</head>
<body>
 <p id="Hello" onmouseover="change(this)">Hello How are you doing</p>
 <script>
 function change(obj){
  obj.style.margin = "90px";
  obj.style.fontSize = obj.style.fontSize == "200%" ? "300%" : "200%";
  obj.style.color= obj.style.color=="green" ? "red" : "green"; 
 }
 </script>
</body>
</html>


Here is a Java Script to display a table. To display a table always remember to set table row in first loop and table data in second loop. The loop will horizontal as per in the table. So make the inner loop in such way that it will display data in next column. Though here is only one loop.


JavaScript Display Possibilities
  • JavaScript can "display" data in different ways:
  • Writing into an HTML element, using innerHTML.
  • Writing into the HTML output using document.write().
  • Writing into an alert box, using window.alert().
  • Writing into the browser console, using console.log().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<html>
<head>
 <title>Display a table</title>
</head>
<body>
 <script>
 var i = 0;
 var j = 0;
 document.write("<table border='1'>");
  document.write("<tr>");
   document.write("<th> S.No </th>");
   document.write("<th> Input </th>");
   document.write("<th> Output </th>");
  document.write("</tr>");
 for(i=1;i<6;i++)
 {
  document.write("<tr>");
   document.write("<td>" + i + "</td>");
   document.write("<td>" + (i+4) + "</td>");
   document.write("<td>" + (i+4)*(i+4) + "</td>");
  document.write("</tr>");
 }
 document.write("</table>");
 </script>
</body>

For Better Understanding of the loops for table  in Java Script There is another Multiplication table below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<html>
<head>
 <title>Display a table</title>
</head>
<body>
 <script>
 var i = 0;
 var j = 0;
 document.write("<table border='1'>");
        for(i=1;i<11;i++)
        {
          document.write("<tr>");
          for(j=1;j<11;j++)
          {
            document.write("<td>"+j+"*"+i+"="+(j*i)+"</td>");
          }
          document.write("</tr>");
        }
        document.write("</table>");
 </script>
</body>

As in the above loop we Use j*i Which makes the inner loop * outer loop as outer loop will changed as 10 inner loop so will display as 1*1=1 then in next row 2*1=2 ..... 10*1=10 Got it?
If something is wrong or Need more clarification  Please contact me: info[at]devkotasagar.com.np.
Hello There Here is a simple PHP Program to connect with MySQL database. Create a Table in the database and insert data into the database through the form.

Course: Web Technology
University: Pokhara University

Check The Internet and The Web – Introduction

Here is a connect.php file. We need Server, Database Name, Username and Password for that database.
new mysqli(server, username,password,database)
Will establish a database connection.

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$server = "localhost";
$username ="test2";
$password ="test2";
$dbname ="test2";
$conn =new mysqli($server,$username,$password,$dbname);
if ($conn->connect_error)
{
  die("connection error " .$conn->connect_error);
}

 ?>

Here is createdb.php Which will create a table Named info with element name and email.
Here we save the SQL query in variable sql then run it in the Connection made above as
$conn -> query($sql)

1
2
3
4
5
6
7
8
<?php
include "connect.php";
$sql ="CREATE TABLE info(
  id INT(6) AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(60) NOT NULL,
  email VARCHAR(40) NOT NULL)";
$conn-> query($sql);
 ?>

Finally we are here in index.html, Here is a form with action test2.php. Whatever we send through this form will be send to test2.php (that is what action for)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form class="" action="test2.php" method="post">
      Name: <input type="text" name="name" value="name"/><br>
      Email: <input type="email" name="email" value="email"/><br>
      <input type="submit" name="" value="send">

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

And here is test2.php which will get the values passed from the form and store them in the Database.
$_POST["name"] will get the value from the form with name name. The SQL query

INSERT INTO info(name, email) VALUES ('$name','$email')
Will insert the value of name and email to the table info and element name and email.
1
2
3
4
5
6
7
8
<?php
include "connect.php";
$name= $_POST["name"];
echo $_POST["name"];
$email = $_POST["email"];
$sql = "INSERT INTO info(name, email) VALUES ('$name','$email')";
$conn-> query($sql);
 ?>

This is all. You can find the code in Github https://github.com/InfoDevkota/phpformmysql/tree/master (Master Branch) Issuers and pull requested are accepted.
If something is wrong or Need more clarification  Please contact me: info[at]devkotasagar.com.np.
Aello This is a Simple JS Validation example Only to understand how it works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!doctype html>
<html>
 <head>
  <title>This is a Test JavaScript Validation page</title>
  <script>
   function check() {
    var x = document.forms["test"]["firstname"].value;
    var y = document.forms["test"]["email"].value;
    if (x == "") {
     document.getElementById('error').innerHTML = "Name must be Filled!";
     return false;
    }
    if (y == "") {
     document.getElementById('error').innerHTML = "Email must be Filled!";
     return false;
    }
    var at = y.indexOf("@");
    var dot = y.lastIndexOf(".");
    if (at < 1 || dot < 4 || at > dot ) {
     document.getElementById('error').innerHTML = "Enter a valid email";
     return false;
    }
   }
  </script>
 </head>
 <body>
  <form name="test" method="post" onsubmit = 'return check()'>
   <input type="text" name="firstname" placeholder="Enter a name"/>
   <input type="text" name="email" placeholder="Enter your mail"/>
   <input type="submit" value="Send"/>
  </form>
  <p id="error"></p>
 </body>
</html>

Hey that's not Aello! its Hello..
<Pokhara University CPP>
Functions and all types.
Constructor and Destructor
Friend Function
Operation Overloading
Type Conversion (All 3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<iostream>
using namespace std;
const double pi = 3.14; //If we try to alter the vaule of Pi compiler will generate a error.
class Test2;
class Test{
  int num1; //This will be in private section
  int num2; //This will be in private section
  public:
    //**************************Functions***************************//
    void add3(int a, int b, int c=5){ //Default arguments If only two args are pass last will take 5 if  all 3 are passed it will add those 3 numbers
      int add = a+b+c;
      cout<<"Sum = "<<add<<endl;
    }
    void add3(double a, double b, double c){ //Funtion Overloaded This function and above function name are same but if we pass int as arguments the above will be executed if float are passed this one will be executed
      double add = a+b+c;
      cout<<"Sum = "<<add<<endl;
    }
    void ifunction(){
      cout<<"I am a inline function"<<endl;
    } // FUnction definition inside class will be a inline function
    void anotherIFunc();// this is not a inline function as it is not defined here
    //**************************Constructor Destructor***************************//
    Test(){ //I am a default constructor
      num1=0;
      num2=0;
    }
    Test(int a, int b){ //I am a parameterised constructor
      num1=a;
      num2=b;
    }
    Test(Test &a){ //I am a copy constructor as i coppy the value of passed object to new object
      num1=a.num1;
      num2=a.num2;
    }
    //Here we use A function name Test to preform different taks, which is overload hence a constructor can be Overloaded
    ~Test(){ // Will be called automaticly Cannot be Overloaded as they takes no arguments
      cout<<"I am a Destructor"<<endl;
    }
    //**************************Friend Function***************************//
    friend void iaccess(Test a){//This is a friend function as there is keyword friend
      cout<<a.num2<<endl; // this can access the mamber data at private with a object.
      //no object is required to call a friend function.
    }
    friend void friendoftwo(Test a, Test2 b);
    //**************************Operator Overloading***************************//
    void operator -(){ //TO change the sign so a unary operator As this is unary operator we dont need to pass any args, as we will get a oject when it call
      num1 = num1*(-1);
      num2 = num2*(-1);
    }
    Test operator -(Test a){// TO subtract
      Test tmp;
      tmp.num1 = num1 - a.num1;
      tmp.num2 = num2 - a.num2;
      return tmp;
    }
    void display(){
      cout<<num1<<" and "<<num2<<endl;
    }
    //**************************Type Conversion***************************//
    Test(int a){ //Basic Type to Object Type Conversion
      num1=a;
      num2=a;
    }
    operator int (){ // Object to Basic Type conversion
      return num1+num2;
    }
    /* ******* DO YOU UNDERSTAND ******/ //YOU can OBtain the Object to Object Conversion by Defining constructor in Destination class or Operator conversion function in Source Class.
};
//*****************************************************//
class Test2{
  int age; //This will be in private section
  public:
    Test2(){
      age = 0;
    }
    friend void friendoftwo(Test a, Test2 b);
};
//*****************************************************//

inline void Test :: anotherIFunc(){ //Keyword "inline" makes it a inline function.
  cout<<"I am also a inline function"<<endl;
}

void friendoftwo(Test a, Test2 b){ //We dont need the friend keyword when defining the function.
  int tmp;
  cout<<a.num1<<" and "<<b.age<<endl;
  tmp = a.num1;
  a.num1= b.age;
  b.age = tmp; //Here we swap the private data of two classes using a friend function.
  cout<<a.num1<<" and "<<b.age<<endl;
}
int main()
{
  //Constructor and Function
  Test anum(2,10);
  anum.ifunction();
  anum.anotherIFunc();
  {
    //Destructor
    Test bnum;
    //Distructor will be automaticlly called when the program goes out of this "}"
  }
  //Friend Function
  iaccess(anum);
  Test2 bnum;//bnum.age is 0 because of the default constructor
  friendoftwo(anum, bnum);
  //Operator Overloading
  Test cnum(1,2);
  -cnum;
  cnum.display();
  Test add, dnum(7,15);
  add = dnum - anum;// Here the - FUnction is called by dnum and anum is passed as argument, as num1 is of dnum and a.num1 is of anum UNDERSTND THIS :) BE suer while using Greater then Operator.
  add.display();
  // Type conversion
  Test eanum;
  eanum = 9; //Both num1 and num2 gets the value of 9.
  eanum.display();
  int f = eanum; //f will get the value of num1+ num2 of eanum;
  cout<<f<<endl;

  return 0;
}

Is there anything Please do Comment, We will Update our Post Accordingly.