Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Google Pie Charts with PHP and MySQL

Populating pie charts with google api is one of the simplest way to do. here we are showing 
you a simple example to generate pie chart with real time data. fetching data from database table.
<?php
$DatabaseServer = "localhost";
$DatabaseUsername = "root";
$DatabasePassword = "root";
$DatabaseName = "demo";

$Connection = mysqli_connect($DatabaseServer, $DatabaseUsername, 
    $DatabasePassword, $DatabaseName);

if ($Connection === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$selectsql = "SELECT * FROM graph";
$result = mysqli_query($Connection, $selectsql);
?>

<html>
<head>
    <script type="text/javascript" 
            src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
        google.charts.load('current', {'packages':['corechart']});
        google.charts.setOnLoadCallback(drawChart);

        function drawChart() {

          var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          <?php while($row = mysqli_fetch_array($result)){  ?>
             ["<?php echo $row['name']; ?>", <?php echo $row['count']; ?>],
          <?php } ?>
          ]);

         var options = {
           title: 'My Daily Activities'
            };

         var chart = new google.visualization.PieChart
         (document.getElementById('piechart'));

         chart.draw(data, options);
        }
    </script>
</head>
<body>
<div id="piechart" style="width: 900px; height: 500px;"></div>
</body>
</html>
Run these queries in your database.
//Table structure for table `graph`
CREATE TABLE `graph` (
  `id` int(2) NOT NULL,
  `name` varchar(34) NOT NULL,
  `count` int(12) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

//Dumping data for table `graph`

INSERT INTO `graph` (`id`, `name`, `count`) VALUES
(1, 'Work', 11),
(2, 'Eat', 2),
(4, 'Commute', 2),
(5, 'Watch TV', 2),
(6, 'Sleep', 7);
Result:

Simple User Registration Form Using PHP, MySQL

In this tutorial, I am illustrating a simple user registration form. register form is a common need in any kind of web-based application so here is register form using PHP, MySQL, HTML, and CSS.

<html>

<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <style>
        body{
            padding:40px;
        }
        .container-one{
            width:340px;
            height:320px;
            border:1px solid black;
            padding-left:60px;
        }
        .btn {
            margin: 5px;
        }
    </style>
</head>

<body>

<h3>Register:</h3>

<div class="container-one">
    <form method="POST">
        <div class="col-md-5">
            First Name <input type="text" name="FirstName">
        </div>
        <div class="col-md-5">
            Last Name <input type="text" name="LastName">
        </div>
        <div class="col-md-2">
            Email <input type="text" name="Email">
        </div>
        <div class="col-md-3">
            Password <input type="password" name="Password">
        </div>
        <div class="col-md-2">
            City <input type="text" name="City">
        </div>
        <div class="col-md-2">
     <input type="submit" name="Submit" value="Submit" class="btn">
        </div>
    </form>
</div>

</body>
</html>
<?php
$DatabaseServer = "localhost";
$DatabaseUsername = "root";
$DatabasePassword = "root";
$DatabaseName = "demo";

$Connection = mysqli_connect($DatabaseServer,
 $DatabaseUsername, $DatabasePassword, $DatabaseName);
if ($Connection === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
if (isset($_POST['Submit'])) {
    $FirstName = $_POST['FirstName'];
    $LastName = $_POST['LastName'];
    $Email = $_POST['Email'];
    $Password = $_POST['Password'];
    $City = $_POST['City'];
    $SQL = "INSERT INTO user 
(FirstName, LastName, Email, Password, City) 
VALUES ('$FirstName', '$LastName', '$Email', '$Password', '$City')";
    $result = mysqli_query($Connection, $SQL);
}
?>
Output:

How to delete data in a table using CodeIgniter?

This block of code of will explains about how to delete data from table using CodeIgniter.
Which involves mainly three steps.
  1. View
  2. Controller
  3. Model

View
demodata.php
<html>
<head>
    <style>
        .container {
width: 500px;
height: 100px;
padding: 200px;
        }
table, th, td {
border: 1px solid black;
width:500px;
        }
    </style>
</head>
<body class="container">
<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Number</th>
        <th>Update</th>
    </tr>

<?php
    foreach ($k->result() as $row) {
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>' . $row->number . '</td>';
echo "<td><a href='updatedemodata?id=$row->id'>Update</a>
        <a href='deletedemodata?id=$row->id'> / Delete</a></td>";
    }
?>

</table>
</body>
</html>

Controller
Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {
public function deletedemodata(){
if(isset($_GET['id'])) {
if (($_GET['id'] !== '')) {
$id = $_GET['id'];
$this->levels->deletedemodata($id);
            } else {
echo 'error while deleting';
            }
        }
    }
}
?>

Model
level.php
<?php
class Levels extends CI_Model
{
public function deletedemodata($id)
    {
$this->load->database();
$this->db->where('id', $id);
$this->db->delete('demo');
echo 'Data Deleted successfully';
$this->load->helper('url');
        redirect('/welcome/demodata');
    }
}
?>



How to update data in a table using CodeIgniter?

This code gives you step by step explanation on how to update data in a table with CodeIgniter framework.
Model
Controller
View

Model
level.php
<?php
class Levels extends CI_Model {
public function selectdemodata_ind($id)
    {
$this->load->database();
$query = $this->db->get_where('demo', array('id =' => $id));
return $query;
    }

public function updatedemodata($data, $id)
    {
$this->load->database();
$this->db->where('id', $id);
$this->db->update('demo', $data);
echo 'Data Updated successfully';
$this->load->helper('url');
        redirect('/welcome/demodata');
    }
}
?>

Controller
welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function updatedemodata(){
$id = $_GET['id'];
$data_one['k'] = $this->levels->selectdemodata_ind($id);
$this->load->view('updatedemodata', $data_one);

$this->load->model('levels');
$name = $this->input->post('name');
$number= $this->input->post('number');
$data = array(
'name' => $name,
'number' => $number
);

if(isset($_POST['update'])) {
if ((name !== '')) {
$id = $_GET['id'];
$this->levels->updatedemodata($data, $id);
            } else {
echo 'Please fill fields';
            }
        }
    }
}
?>

View
updatedemodata.php
<html>
<head>
    <style>
        .container {
width: 200px;
height: 100px;
padding: 200px;

        }
label {
padding: 10px;
        }

        .btn {
float: right;
padding: 5px;
        }
    </style>
</head>

<?php foreach ($k->result() as $row) { ?>
<body class="container">
    <form method='POST'>
        <label>Name</label>
        <input type="text" name="name" 
value="<?php echo $row->name; ?>" ><br><br>
        <label>Number</label>
        <input type="text" name="number" 
value="<?php echo $row->number; ?>" >
        <input type="submit" class="btn" 
name="update" value="update">
    </form>
    </body>
<?php } ?>

</html>










Labels

php (35) javascript (31) phpjavascript (30) jquery (23) html (20) mysql (14) database (9) codeigniter (4) json (4) bar chart (2) calendar (2) column chart (2) framework (2) google maps (2) query (2) tables (2) url (2) dropdown (1)

Popular Posts