-1

I found two instructions on angularjs (http://softaox.info/category/angularjs/). However, I would like to combine it together - ie displaying, filtering and additionally editing the database directly from the website. However, I have a problem with connecting two controllers on one page. The database reads, but when I want to edit, delete it, nothing happens. Will you help me solve the problem?

Here is my code `index.php

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>SoftAOX | AngularJS Sorting, Searching and Pagination of Data Table using PHP, MySQL</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div ng-app="myApp" ng-controller="controller">
        <div class="container">
            <br/>
            <h3 align="center">AngularJS Sorting, Searching and Pagination of Data Table using PHP, MySQL</a></h3>
            <br/>
            <div class="row">
                <div class="col-sm-2 pull-left">
                    <label>PageSize:</label>
                    <select ng-model="data_limit" class="form-control">
                        <option>10</option>
                        <option>20</option>
                        <option>50</option>
                        <option>100</option>
                    </select>
                </div>
                <div class="col-sm-6 pull-right">
                    <label>Search:</label>
                    <input type="text" ng-model="search" ng-change="filter()" placeholder="Search" class="form-control" />
                </div>
            </div>
            <br/>
            <div class="row">
                <div class="col-md-12" ng-show="filter_data > 0">
                    <table class="table table-striped table-bordered">
                        <thead>
                            <th>S.No</th>
                            <th>Name&nbsp;<a ng-click="sort_with('name');"><i class="glyphicon glyphicon-sort"></i></a></th>
                            <th>Age&nbsp;<a ng-click="sort_with('age');"><i class="glyphicon glyphicon-sort"></i></a></th>
                            <th>Email&nbsp;<a ng-click="sort_with('email');"><i class="glyphicon glyphicon-sort"></i></a></th>
                            <th>Edit</th>
                            <th>Delete</th>
                        </thead>
                        <tbody>
                            <tr ng-repeat="data in names = (file | filter:search | orderBy : base :reverse) | beginning_data:(current_grid-1)*data_limit | limitTo:data_limit">
                                <td>{{data.id}}</td>
                                <td>{{data.name}}</td>
                                <td>{{data.age}}</td>
                                <td>{{data.email}}</td>
                                <td>
                                    <button class="btn btn-success btn-xs" ng-click="update_data(data.id, data.name, data.email, data.age)">
                                    <span class="glyphicon glyphicon-edit"></span> Edit
                                    </button>
                                </td>
                                <td>
                                    <button class="btn btn-danger btn-xs" ng-click="delete_data(data.id )">
                                <span class="glyphicon glyphicon-trash"></span> Delete
                                </button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="col-md-12" ng-show="filter_data == 0">
                    <div class="col-md-12">
                        <h4>No records found..</h4>
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="col-md-6 pull-left">
                        <h5>Showing {{ searched.length }} of {{ entire_user}} entries</h5>
                    </div>
                    <div class="col-md-6" ng-show="filter_data > 0">
                        <div pagination="" page="current_grid" on-select-page="page_position(page)" boundary-links="true" total-items="filter_data" items-per-page="data_limit" class="pagination-small pull-right" previous-text="&laquo;" next-text="&raquo;"></div>
                    </div>
                </div>
                <div ng-app="myApp" ng-controller="cont1" ng-init="show_data()">
                <div class="col-md-6">
                    <label>Name</label>
                    <input type="text" name="name" ng-model="name" class="form-control">
                    <br/>
                    <label>Email</label>
                    <input type="text" name="email" ng-model="email" class="form-control">
                    <br/>
                    <label>Age</label>
                    <input type="text" name="age" ng-model="age" class="form-control">
                    <br/>
                    <input type="hidden" ng-model="id">
                    <input type="submit" name="insert" class="btn btn-primary" ng-click="insert()" value="{{btnName}}">
                </div>
    </div>
            </div> 
        </div>     
</div>

</div>

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.min.js"></script>
    <script src="myapp.js"></script>
</body>
</html>

myapp.js

var app = angular.module('myApp', ['ui.bootstrap']);
app.filter('beginning_data', function() {
    return function(input, begin) {
        if (input) {
            begin = +begin;
            return input.slice(begin);
        }
        return [];
    }
});
app.controller('controller', function($scope, $http, $timeout) {
    $http.get('fetch.php').success(function(user_data) {
        $scope.file = user_data;
        $scope.current_grid = 1;
        $scope.data_limit = 10;
        $scope.filter_data = $scope.file.length;
        $scope.entire_user = $scope.file.length;
    });
    $scope.page_position = function(page_number) {
        $scope.current_grid = page_number;
    };
    $scope.filter = function() {
        $timeout(function() {
            $scope.filter_data = $scope.searched.length;
        }, 20);
    };
    $scope.sort_with = function(base) {
        $scope.base = base;
        $scope.reverse = !$scope.reverse;
    };
});

app.controller("cont1", function($scope, $http) {
    $scope.btnName = "Insert";
    $scope.insert = function() {
        if ($scope.name == null) {
            alert("Enter Your Name");
        } else if ($scope.email == null) {
            alert("Enter Your Email ID");
        } else if ($scope.age == null) {
            alert("Enter Your Age");
        } else {
            $http.post(
                "insert.php", {
                    'name': $scope.name,
                    'email': $scope.email,
                    'age': $scope.age,
                    'btnName': $scope.btnName,
                    'id': $scope.id
                }
            ).success(function(data) {
                alert(data);
                $scope.name = null;
                $scope.email = null;
                $scope.age = null;
                $scope.btnName = "Insert";
                $scope.show_data();
            });
        }
    }
    $scope.show_data = function() {
        $http.get("display.php")
            .success(function(data) {
                $scope.names = data;
            });
    }
    $scope.update_data = function(id, name, email, age) {
        $scope.id = id;
        $scope.name = name;
        $scope.email = email;
        $scope.age = age;
        $scope.btnName = "Update";
    }
    $scope.delete_data = function(id) {
        if (confirm("Are you sure you want to delete?")) {
            $http.post("delete.php", {
                    'id': id
                })
                .success(function(data) {
                    alert(data);
                    $scope.show_data();
                });
        } else {
            return false;
        }
    }
});

insert.php

<?php
$conn = mysqli_connect("localhost", "root", "", "test");
$info = json_decode(file_get_contents("php://input"));
if (count($info) > 0) {
    $name     = mysqli_real_escape_string($conn, $info->name);
    $email    = mysqli_real_escape_string($conn, $info->email);
    $age      = mysqli_real_escape_string($conn, $info->age);
    $btn_name = $info->btnName;
    if ($btn_name == "Insert") {
        $query = "INSERT INTO insert_emp_info(name, email, age) VALUES ('$name', '$email', '$age')";
        if (mysqli_query($conn, $query)) {
            echo "Data Inserted Successfully...";
        } else {
            echo 'Failed';
        }
    }
    if ($btn_name == 'Update') {
        $id    = $info->id;
        $query = "UPDATE insert_emp_info SET name = '$name', email = '$email', age = '$age' WHERE id = '$id'";
        if (mysqli_query($conn, $query)) {
            echo 'Data Updated Successfully...';
        } else {
            echo 'Failed';
        }
    }
}
?>

display.php

<?php
$conn   = mysqli_connect("localhost", "root", "", "test");
$output = array();
$query  = "SELECT * FROM insert_emp_info";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_array($result)) {
        $output[] = $row;
    }
    echo json_encode($output);
}
?>

fetch.php

<?php
$conn  = new mysqli('localhost', 'root', '', 'test');
$query = "select distinct id, name, age, email from insert_emp_info order by id";
$result = $conn->query($query) or die($conn->error . __LINE__);
$fetch_data = array();
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $fetch_data[] = $row;
    }
}
$jResponse = json_encode($fetch_data);
echo $jResponse;
?>`
  • This is only for a few people in local resources (intranet). This will not be publicly available on the web. I am not a programmer, they do it in their free time for their own use based on materials from the web. – 1shot2killz Jun 28 '19 at 08:49

2 Answers2

0

You can use the same controller for one page. I modified index.php and myapp.js file

index.php

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>SoftAOX | AngularJS Sorting, Searching and Pagination of Data Table using PHP & MySQL</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div ng-app="myApp" ng-controller="controller">
        <div class="container">
            <br/>
            <h3 align="center">AngularJS Sorting, Searching and Pagination of Data Table using PHP & MySQL</a></h3>
            <br/>
            <div class="row">
                <div class="col-sm-2 pull-left">
                    <label>PageSize:</label>
                    <select ng-model="data_limit" class="form-control">
                        <option>10</option>
                        <option>20</option>
                        <option>50</option>
                        <option>100</option>
                    </select>
                </div>
                <div class="col-sm-6 pull-right">
                    <label>Search:</label>
                    <input type="text" ng-model="search" ng-change="filter()" placeholder="Search" class="form-control" />
                </div>
            </div>
            <br/>
            <div class="row">
                <div class="col-md-12" ng-show="filter_data > 0">
                    <table class="table table-striped table-bordered">
                        <thead>
                            <th>Name&nbsp;<a ng-click="sort_with('name');"><i class="glyphicon glyphicon-sort"></i></a></th>
                            <th>Gender&nbsp;<a ng-click="sort_with('gender');"><i class="glyphicon glyphicon-sort"></i></a></th>
                            <th>Age&nbsp;<a ng-click="sort_with('age');"><i class="glyphicon glyphicon-sort"></i></a></th>
                            <th>Email&nbsp;<a ng-click="sort_with('email');"><i class="glyphicon glyphicon-sort"></i></a></th>
                            <th>Phone&nbsp;<a ng-click="sort_with('phone');"><i class="glyphicon glyphicon-sort"></i></a></th>
                            <th>Organization&nbsp;<a ng-click="sort_with('organization');"><i class="glyphicon glyphicon-sort"></i></a></th>
                        </thead>
                        <tbody>
                            <tr ng-repeat="data in searched = (file | filter:search | orderBy : base :reverse) | beginning_data:(current_grid-1)*data_limit | limitTo:data_limit">
                                <td>{{data.name}}</td>
                                <td>{{data.gender}}</td>
                                <td>{{data.age}}</td>
                                <td>{{data.email}}</td>
                                <td>{{data.phone}}</td>
                                <td>{{data.organization}}</td>
                                <td>
                                    <button class="btn btn-success btn-xs" ng-click="update_data(data.id, data.name, data.email, data.age)">
                                        <span class="glyphicon glyphicon-edit"></span> Edit
                                    </button>
                                </td>
                                <td>
                                    <button class="btn btn-danger btn-xs" ng-click="delete_data(data.id )">
                                        <span class="glyphicon glyphicon-trash"></span> Delete
                                    </button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="col-md-12" ng-show="filter_data == 0">
                    <div class="col-md-12">
                        <h4>No records found..</h4>
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="col-md-6 pull-left">
                        <h5>Showing {{ searched.length }} of {{ entire_user}} entries</h5>
                    </div>
                    <div class="col-md-6" ng-show="filter_data > 0">
                        <div pagination="" page="current_grid" on-select-page="page_position(page)" boundary-links="true" total-items="filter_data" items-per-page="data_limit" class="pagination-small pull-right" previous-text="&laquo;" next-text="&raquo;"></div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <label>Name</label>
                <input type="text" name="name" ng-model="name" class="form-control">
                <br/>
                <label>Email</label>
                <input type="text" name="email" ng-model="email" class="form-control">
                <br/>
                <label>Age</label>
                <input type="text" name="age" ng-model="age" class="form-control">
                <br/>
                <input type="hidden" ng-model="id">
                <input type="submit" name="insert" class="btn btn-primary" ng-click="insert()" value="{{btnName}}">
            </div>
        </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.12/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.min.js"></script>
    <script src="myapp.js"></script>
</body>
</html>

myapp.js

var app = angular.module('myApp', ['ui.bootstrap']);
app.filter('beginning_data', function() {
    return function(input, begin) {
        if (input) {
            begin = +begin;
            return input.slice(begin);
        }
        return [];
    }
});
app.controller('controller', function($scope, $http, $timeout) {
    $http.get('fetch.php').success(function(user_data) {
        $scope.file = user_data;
        $scope.current_grid = 1;
        $scope.data_limit = 10;
        $scope.filter_data = $scope.file.length;
        $scope.entire_user = $scope.file.length;
    });
    $scope.page_position = function(page_number) {
        $scope.current_grid = page_number;
    };
    $scope.filter = function() {
        $timeout(function() {
            $scope.filter_data = $scope.searched.length;
        }, 20);
    };
    $scope.sort_with = function(base) {
        $scope.base = base;
        $scope.reverse = !$scope.reverse;
    };
    $scope.btnName = "Insert";
    $scope.insert = function() {
        if ($scope.name == null) {
            alert("Enter Your Name");
        } else if ($scope.email == null) {
            alert("Enter Your Email ID");
        } else if ($scope.age == null) {
            alert("Enter Your Age");
        } else {
            $http.post(
                "insert.php", {
                    'name': $scope.name,
                    'email': $scope.email,
                    'age': $scope.age,
                    'btnName': $scope.btnName,
                    'id': $scope.id
                }
            ).success(function(data) {
                alert("Data Updated Successfully...");
                $scope.name = null;
                $scope.email = null;
                $scope.age = null;
                $scope.btnName = "Insert";
                $scope.show_data();
            });
        }
    }
    $scope.show_data = function() {
        $http.get('fetch.php').success(function(user_data) {
        $scope.file = user_data;
        $scope.current_grid = 1;
        $scope.data_limit = 10;
        $scope.filter_data = $scope.file.length;
        $scope.entire_user = $scope.file.length;
    });
    }
    $scope.update_data = function(id, name, email, age) {
        $scope.id = id;
        $scope.name = name;
        $scope.email = email;
        $scope.age = age;
        $scope.btnName = "Update";
    }
    $scope.delete_data = function(id) {
        if (confirm("Are you sure you want to delete?")) {
            $http.post("delete.php", {
                    'id': id
                })
                .success(function(data) {
                    alert("Data Deleted Successfully...");
                    $scope.show_data();
                });
        } else {
            return false;
        }
    }

});

delete.php

<?php
$conn = mysqli_connect("localhost", "root", "", "test");
$data    = json_decode(file_get_contents("php://input"));
if (count($data) > 0) {
    $id    = $data->id;
    $query = "DELETE FROM insert_emp_info WHERE id='$id'";
    if (mysqli_query($conn, $query)) {
        echo 'Data Deleted Successfully...';
    } else {
        echo 'Failed';
    }
}
?>

insert.php

<?php
$conn = mysqli_connect("localhost", "root", "", "test");
$info = json_decode(file_get_contents("php://input"));
if (count($info) > 0) {
    $name     = mysqli_real_escape_string($conn, $info->name);
    $email    = mysqli_real_escape_string($conn, $info->email);
    $age      = mysqli_real_escape_string($conn, $info->age);
    $btn_name = $info->btnName;
    if ($btn_name == "Insert") {
        $query = "INSERT INTO insert_emp_info(name, email, age) VALUES ('$name', '$email', '$age')";
        if (mysqli_query($conn, $query)) {
            echo "Data Inserted Successfully...";
        } else {
            echo 'Failed';
        }
    }
    if ($btn_name == 'Update') {
        $id    = $info->id;
        $query = "UPDATE insert_emp_info SET name = '$name', email = '$email', age = '$age' WHERE id = '$id'";
        if (mysqli_query($conn, $query)) {
            echo 'Data Updated Successfully...';
        } else {
            echo 'Failed';
        }
    }
}
?>

fetch.php

<?php
$conn  = new mysqli('localhost', 'root', '', 'test');
$query = "select distinct id, name, age, email from insert_emp_info order by id";
$result = $conn->query($query) or die($conn->error . __LINE__);
$fetch_data = array();
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $fetch_data[] = $row;
    }
}
$jResponse = json_encode($fetch_data);
echo $jResponse;
?>
Mangesh Auti
  • 1,123
  • 1
  • 7
  • 12
  • Works correctly. I have one more question. Why, how do I change the email table to email_1 in the database, then in all files (fetch,index,insert,myapp) also an email for email_1 does not want to edit this record? The name and age are displayed for editing, and the email remains empty. – 1shot2killz Jul 01 '19 at 07:30
  • means you want to change email name in database as email_1 or reverse ? or just on edit page don't allow the user to edit his email ? not clear – Mangesh Auti Jul 01 '19 at 07:56
  • It's ok. It's enough to refresh the browser - shift + f5. Thanks for support. Trying to convert so that the editing takes place directly in the table, and not as now in the form outside the table. – 1shot2killz Jul 01 '19 at 16:52
  • Ok no problem . – Mangesh Auti Jul 01 '19 at 17:04
  • Can you still help me why when I add / edit I do not put in Polish dialectic characters? I have everything set to UTF-8. [link]https://imgur.com/a/oTBdDPw – 1shot2killz Jul 02 '19 at 07:33
  • May be Polish dialectic charcters are not supported in UTF-8 try to change UTF-16 Or add more meta tag etc – Mangesh Auti Jul 02 '19 at 09:32
  • add to insert.php [code]$conn->set_charset("utf8"); works :) – 1shot2killz Jul 02 '19 at 11:18
  • Great..means u add charset on conn in php – Mangesh Auti Jul 02 '19 at 11:24
-1

May use $rootScope to pass the info u need across different controller

Difference between $scope and $rootScope

Luco
  • 23
  • 6